Today let us discuss some important keywords in C#.
- Var
- Extension Methods
- Object Initializer
- Nullable Types
- Anonymous Methods
Var
We use the var type when we don't know which data type we are using or declaring. It is an implicit type and it aliases any type in C# and the alias can be determined by the C# complier. In a simple way, we can say that var explicitly defines a variable data type.
Examples
The following are some examples:
Var age = 50;// here we are not giving int just we declaring Var age =50 so the complier understands it as int..
Var fruit = apple;// same here..
Extension Methods
Extension methods add methods to existing classes. For example if we have a class library file (with a dll extension). In this file suppose we have the 3 methods add, sub and div. If we declare another new method in addtion to the existing then we can make that new method an extension method. We must declare the extension method as static and even public so we can use it anywhere we want. An extension method must have the "this" keyword in its parameter list.
Examples
The following are some examples.
using System;
public static class ExtensionMethods
{
public static string LowercaseFirstLetter(this string value)
{
//
// Lowercase the first letter in the string this extension is called on.
//
if (value.Length > 0)
{
char[] array = value.ToCharArray();
array[0] = char.ToLower(array[0]);
return new string(array);
}
return value;
}
}
class Program
{
static void Main()
{
//
// Use the string extension method on this value.
//
string value = "SAIRAM";
value = value.LowercaseFirstLetter(); // Called like an instance method.
Console.WriteLine(value);
}
}
Output
sairam
Object Initializer
An Object Initializer is used in place of constructors. It reduces the lines of code, and not only does it reduce the code but also increases the flexibility. Using an Object Initializer we can also overcome the problems of constructor overloading.
Example
The following is an example:
var customer = new customer() { filed1= value1, field2 =value2}// in this we are not creating an instance of a constructor.
If we use a constructor then we must create the instance when we want to declare the values, so that we will not be clean and good to use.
Nullable Types
When we declare a variable as an int the variable should not be null. If we declare that variable as null then the exception occurs as int cannot be null, so by using a nullable type we can avoid that kind of exception. It is specified with a "?" symbol.
Example
The following is an example:
int a;
a=null; // here the exception occur
int? a;
a= null// exception doesn't occur
int? id = 10;
id = 0;
id = null;
DateTime? ds = null;
char? ch = null;
Anonymous Methods
We can dynamically create methods without declaring a class.
Example
The following is an example.
class Class1
{
public static void Main()
{
//Anonyumus type to create objects dynamically without declaring class
string adres = "MA";
var vcustomer = new{ name = "abc", id = 10, addres = adres };
var vstudent=new {id="Adfads",startdate=System.DateTime.Now,addres=new{city="MA",country="USA"}};
}
}