Implicitely typed variables in C#.Net


In this post, we will discuss Implicitly typed variables in C#.Net. Also, you can check my previous posts on:
It was a feature added in c#3.0 which allows declaring a variable using “var” keyword.
The datatypes of the variable will be decided depending upon the value we assign to the variables.
We can not declare a variable using “var” without assigning a value. The “var” keyword may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.
Multiple implicitly-typed variables cannot be initialized in the same statement.
Below  are  some examples:
// i is compiled as an integer
var i = 50;
// s is compiled as a string
var s = “aspdotnethelp”
// x is compiled as int[]
var x = new[] { 0, 1, 2 ,3};
// expr is compiled as IEnumerable<Customer>  or IQueryable<Customer>
var expr =
    from c in customers
    where c.Name == “AspDotNetHelp”
    select c;
// list is compiled as List<int>
var list = new List<int>();
// b is compiled as boolean
var b = false;
// anon is compiled as an anonymous type
var anon = new { Name = “Bijay”, Age = 31 };
// invalid
var=a;
Example:
Using System;
Class  ImplicitelyTyped
{
Static Void Main ()
{
Var  a = 200;
Console.WriteLine (a.GetType ());
Var  f = 3.14f;
Console.WriteLine (f.GetType ())
Var  s = “aspdotnethelp”;
Console.WriteLine (s.GetType ());
}
}
Output:
System.int32
System. Single
System. String
NOTE:  GetType () is a predefined method which returns the type of given variable type or object.
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →