What : A loosely typed programming languages is typically one that does not require the data type of a variable to be explicitly stated.
Example : As in JavaScript we can declare var count = 1; which will automatically treat variable count as integer.
Advantage: The advantage of loosely typed is that we don’t have to bother about the data type of the variable value and we have the benefit of changing the type of the variable at any instance of time. Usage in C# : ASP.NET 3.5 gives a new keyword “var” which behaves similar to what “var” keyword seems to do in JavaScript. By using the var keyword we can implicitly convert to its originally data type. So,
var firstName = “Aditya”; convert the data type of variable “firstName” to string data type implicitly.
This keyword can be used efficiently in following stages.
1. While assigning variable to values. string firstName = "Aditya"; string lastName = "Acharya"; var name = firstName+lastName; Response.Write(name); object mark1 = 50; object mark2 = 60; var total = (int)mark1 + (int)mark2; Response.Write(total);
2. While looping through:
string[] arrSubject = { "Physics", "English", "ComputerScience" }; foreach (var subject in arrSubject) { Response.Write(subject); }
3. While declaring a Collection:
var total = 0; var arrSubject = new [] { "Physics", "English", "ComputerScience" }; var arrMark = new[] { 60, 70, 80 }; foreach (var mark in arrMark) { total = total + mark; }
(N.B This keyword can also be used for Visual Basic language. And usage of this keyword can be more, Here it is some of the examples out of many)