The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Var vs. Object data type in C#  
 
Var vs. Object data type in C#
 
Please rate:
 
Knowing which data type to use in different situations is generally a basic level decision. I mean you do not need to think very hard about storing regular numbers or storing words. I hope you don't believe that. Actually, choosing the correct data type to store your data can have serious implication on memory, storage, percision and access. So spend some time learning and studying the different data types and try to imagine which situations you would use a string for or perhaps a float.
 
This article will cover 2 data types. First we will discuss the var data type. The var is a nice one because you do not need to decide whether to use a System.String or System.Int32 becuase when you associate a value to the var, the compiler will determine the type for you. NOTE: using var does not remove the responsibility from understanding the memory usage, storage usage, percision, etc..of the data type. It's just makes things a bit eaiser and faster, but use it with caution.
 
 
 
 
You see above that I set str to a string, intg to an integer and dec to a decimal value and the compiler determined which data type to convert var to. With var you will get access to the the data type class properties and members as you see from the drop downs. The same is not true for an object. It is not known at compile time what data type the value stored within the object is. You can store classes or generic lists in an object. I.e. both reference and value types can be stored in an object.
 
 
 
Similar situation if you have a simple class named Person:
 
 
                        
                        public class Person
                        {
                            public string Name { get; set; }
                            public string Age { get; set; }
                        }
                        
                         
 
 
If I use var to contain the class, then the properties within the class are available, however if I use object they are not.
 
When to use an object in C#? I use object variables in situations when I will receive a list of data but a m not certain of the data types within them. For example:
 
 
                        
                        List<object> fields = new List<object>(BuildFieldList(Query));
                        
                         
 
The Query identified above is a string of columns a user has selected from a reporting tool. I am not certain which column or how many the user has selected. Therefore I decided to use an object variable.
 
When to use a var in C#? Following along the same line as the above example, I would use a var to contain the result set from the Query that was executed.
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
Comment posted: 5/10/2011 3:09:07 AM by Jayan
 
AKAIK you've got the anwesr in one!
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben