In the previous discussion we learned about the importance of session, cache, what is Http and different types of cache? How session is implemented using cookies.

Now in this part we will discuss a topic which is rather small but it has its own importance. A small discussion about the “var”keyword what is the use of it, when it should be used and when not.

“VAR” Keyword:

We usually see the use of this “VAR” keyword in our program code very often. Now most of the developers use this keyword regularly whether it is needed in the program or not. So the question arises where to use this keyword and when to use it? Is it wrong to use it regularly in our program?

So usually as we see in a program,

int i = 7;

stringstr = “abc”;

here we know that i is an integer and str is an string datatype. Now if we use var keyword it will be like,

var x = 7;

x = 7;

here when we are using var keyword the value of x is not known but as we write some value on the right side it automatically tell us that x is an integer datatype. Similarly if we write,

var x = “abc”;

x = “abc”;

here also looking at the value on the right side it tell us that x is an string datatype.

So from this we come to know that when we don’t know what datatype or data we will be getting we use this var keyword because as we use the var keyword it see what type of data we are getting and according to that it allocates the value. In other words VAR keyword looks at the data from the right and then decides the datatype during Compile time.

But as said developers use this var keyword regularly in their program code whether it is needed or not. Now if we write the program code like this,

var x = 7;

var y = “abc”;

var z = true;

It makes our code unreadable or not understandable because we already know what data we are going to get so we already know what value is allocated to the data.

So as we saw what is the use of var keyword we will now see when to use it and when not.

  • When we use the VAR keyword for the primitive datatypes like int, string, Boolean, double as in the above example code, we see that the code is unreadable because it is difficult to tell that x is integer or y is string or z is Boolean. So make sure that we do not use the VAR keyword for primitive datatypes because without the code looks readable.

int x = 7;

string y = “abc”;

boolean z = true;

  • Also when we know that what type of data we are going to get we will not use the VAR keyword. In short when we know the datatype we should not use the VAR keyword.

So when to use the VAR keyword the question arises,

  • When we cannot guess what kind of data we are going to get from the right side or what kind of datatype can be formed from the data on the right side. At that time we should use the VAR keyword. Here is the example for it,

var

When on the right side something like this is there and here we do not know what kind of datatype will be formed for the data we use the VAR keyword.

So in short we come to know that when to use the VAR keyword and when to not. Also we came to know that what the actual use of VAR keyword is.

As we discussed above the use and importance of VAR keyword, we will now take another topic for discussion which is small like the above topic but has its own importance in program code.

Sometimes it happens that the programmer wants to know whether the type of one object/entity is equal to other object/entity or not. In C# programming language we use Object Oriented Programming vastly, by creating classes and objects as much as possible also using the properties like inheritance, polymorphism,etc.

It happens that sometimes we want to compare those classes and objects with other classes and objects. Now to understand these comparison let’s take an example,

Customer obj1 = new Customer();
Customer obj2 = new Customer ();
Supplier obj3 = new Supplier ();
Class Customer
{
}
Class Supplier
{
}

Here we have two classes Customer & Supplier, so we have created three objects obj1, obj2, and obj3. Now if we want to compare obj1 with obj2 and obj2 with obj3. So now looking at the objects we know that obj1 and obj2 are of same type Customer and obj3 is of different type Supplier. So we write the comparison code and to get the type of any object we use the function called as GetType.

Code for obj1 equal to obj2.
If (obj1.GetType() == obj2.GetType())
{
Console.WriteLine(“They are same”)
}
Else
{
Console.WriteLine (“They are different”)
}
Code for obj2 equal to obj3
If (obj2.GetType() == obj3.GetType())
{
Console.WriteLine(“They are same”)
}
Else
{
Console.WriteLine (“They are different”)
}
Console.ReadLine();

Now if we run this code we can see that obj1 and obj2 are of the same type because both objects are from the Customer class and the output will be shown as “They are same”. Also obj2 and obj3 are not same because one object is of Customer class and other is of Supplier class so the output will be show as “They are different”.

So in this way GetType function is used because the GetType name says get the type. It is used when we have objects and we want to compare the type of those objects.

Now we will see when the function TypeOf is used. Sometime it happens that we have only one class and we want to compare that the class which we are having is equal to the other class or not. For example,

Customer obj = new obj ();

We have an object here of customer type and we want to compare this object with another type Supplier that by any chance this object is of Customer type.

If(typeof(Supplier) == obj.GetType())
{
Console.WriteLine(“They are same”)
}
Else
{
Console.WriteLine (“They are different”)
}
Console.ReadLine();

Now here we will see the output shown as “They are different” because the type of Supplier is different from obj which is of Customer type.

So in other words TypeOf function is used to get the type from a class while the GetType function is used to get the type from an object.

So to summarize we can say that both GetType and TypeOf functions helps us to get the type. But GetType gets us the type of object whereas TypeOf gets us the type of class. So we can say that TypeOf function is static as it takes us a class to get the type and GetType function is more run time because it takes the object to get the type.

Now if we want to get the type of a class or object which is inherited to the main class then we cannot get the type using GetType or TypeOf functions. Here we will use “Is” keyword. We can take an example,

Class customer { }
Class discountcustomer : customer { }
discountcustomerobj = new discountcustomer ()
if(objiscustomer)
{
Console.WriteLine (“true”)
}
else
{
Console.WriteLine (“false”)
}

Here we get the output as “true” because obj is of discountcustomer type and discountcustomer is inherited to the parent class customer. So “is” keyword can also get us the type of an object or class when their instance is inherited or the objects and class are inherited to the parent class.

So in short,

  • GetType gets us the type of an object at runtime.
  • TypeOf gets us the type of a class at compile time.
  • Is keyword gets us the type if the instance of the object or class is in inheritance.

Now we use these functions for comparison of the type of object and class but are they necessary question arises. So in real program our code has multiple polymorphisms and we don’t know what type of object or class we are going to get. Therefore we use these functions.

Advertisements