The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.
It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.
The below examples show different ways in which local variables can be declared with var:
// i is compiled as an int
var i = 5;
// s is compiled as a string
var s = "Hello";
// a is compiled as int[]
var a = new[] { 0, 1, 2 };
// expr is compiled as IEnumerable
// or perhaps IQueryable
var expr = from c in customers where c.City == "London" select c;
// anon is compiled as an anonymous type
var anon = new { Name = "Terry", Age = 34 };
// list is compiled as List
var list = new List
The var keyword may be used in the following cases as well:
* On local variables (variables declared at method scope) as shown in the previous example.
* In a for initialization statement.
for(var x = 1; x < 10; x++)
* In a foreach initialization statement.
foreach(var item in list){...}
* In a using statement.
using (var file = new StreamReader("C:\\myfile.txt")) {...}
Enjoy the programming! :)
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
ReplyDeletejava training in bangalore
Hello friends, my name is Rajat and I work as the head of digital marketing in Delhi. I am affiliated with many MNC’s Software developers. If you are talking about the best educational institution in Delhi,Webtrackker help me get the best educational institute in Delhi.we are you offering some best services in our institute.with 100% job offers are available .
ReplyDeleteBest Php Training Institute in Delhi
Php Training in delhi
php Training center in delhi
Best Java Training Institute in delhi
Best Java Training in delhi
java Training center in delhi
linux Training center in delhi
Best linux Training Institute in Delhi
linux Training in delhi
Web Designing Training center in delhi
Best Web Designing Training institute in delhi
Web Designing Training in delhi
Oracle Training Institute in delhi
Oracle Training in Delhi
Oracle Training center in Delhi
blue prism Training Institute in delhi
blue prism Training in Delhi
blue prism Training center in Delhi
Automation Anywhere Training center In delhi
Automation Anywhere Training Institute In delhi
rpa Training Institute in delhi
rpa Training in Delhi
rpa Training center in Delhi
hadoop Training center in delhi
Best hadoop Training institute in delhi
hadoop Training in delhi
This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
ReplyDeleteThis trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Thanks For Sharing The Information The Information shared Is Very Valuable Please Keep Updating Us Time Just Went On reading The Article Python Online Training Aws Online Course DataScience Online Course Devops Online Course
ReplyDeleteI can’t imagine that’s a great post. Thanks for sharing.
ReplyDeleteUpgrade your career Learn SAP MM Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.