Introduction:
In this article,i am going to explain about some new features in C# 3.0.There are,
1.Implicitly typed local variables,
2.Extension Methods,
3.Anonymous types,
4.Object Intialisers,
5.Lambda Expressions,
6.Automatic Properties,
Main:
C# 3.0 provides a new keyword , var, which can be be used in place of int, bool or string.
Implicitly typed local variables helps to infer data type as runtime,
var myInt = 0;
var myBool = true;
var myString = "Welcome ...";
sb.Append("myInt is a: " + myInt.GetType().Name + " ");
sb.Append("myBool is a: " + myBool.GetType().Name + " ");
sb.Append("myString is a: " + myString.GetType().Name + " ");
TextBox1.Text = sb.ToString();
var myInt = 0; var myBool = true; var myString = "Welcome ..."; sb.Append("myInt is a: " + myInt.GetType().Name + " "); sb.Append("myBool is a: " + myBool.GetType().Name + " "); sb.Append("myString is a: " + myString.GetType().Name + " "); TextBox1.Text = sb.ToString(); |
Extension methods:
Way to extend the contract of a class by adding new methods without creating in the
derived class or partial class.
It’s strongly typed , compiled environment such as CLR.
Extension methods can be defined for any class, including native classes of the .Net
Framework.
An extension methods is defined as a static method on a static class.
Binding between the method and type is established through the keyword ‘this’ in the
method’s prototype.
for ex,
static class myextension {
public static void DisplayType(this object obj) {
Console.WriteLine(“{0} lives here ? [1] “,obj.GetType().Name,Assembly.GetAssembly(obj.GetType())));
}
int myInt = 1234;
TextBox1.Text=myInt.DisplayDefiningAssembly().ToString();
static class myextension { public static void DisplayType(this object obj) { Console.WriteLine(“{0} lives here ? [1] “,obj.GetType().Name,Assembly.GetAssembly(obj.GetType()))); } int myInt = 1234; TextBox1.Text=myInt.DisplayDefiningAssembly().ToString(); |
Object Initializers:
C# allows to create objects directly,
Create a new object variable and assign properties, public fields and put object initialize
which consists of a comma-delimited list of specified values, enclosed by the { and }.
Anonymous Types:
If you need a temporary type which is not intended to be reused, you can define anonymous Types.
‘var’ keyword used if user does not know at the time of writing code.
The type of the variable is inferred by compiler (not runtime).
var mycar= new {Color=“red”,Make=“maruthi”,Speed=“250”};
mycar.Color=“Black” ; // but Not possible as it is readonly
Console.WriteLine(“My car color {0}”,mycar.Color) ;
var mycar= new {Color=“red”,Make=“maruthi”,Speed=“250”}; mycar.Color=“Black” ; // but Not possible as it is readonly Console.WriteLine(“My car color {0}”,mycar.Color) ; |
Lambda Expressions:
Lambda expressions allows little coding constructs and save lot of typing time and used in LINQ.
Lambda Expressions As a Better Anonymous Method:
List<int> evennos=list.FindAll(delegate(int ) {return I % 2}==0}};
foreach(int n in evennos)
{Console.WriteLine(n);}
List<int> evennos=list.FindAll(delegate(int ) {return I % 2}==0}}; foreach(int n in evennos) {Console.WriteLine(n);} |
Automatic Properties:
Using automatic properties,we can remove set and get body properties,
for ex,
public string Name {
get
{
return name;
}
set
{ name=value; }
using automatic properties we can change the property declaration like below,
public string Name { get; set; }
public class Customer
{
public int CustomerId { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int Id)
{
CustomerId = Id;
}
public override string ToString()
{
return Name + "t" + City + "t" + CustomerId;
}
}
Customer c = new Customer(100);
c.Name = "James";
c.City = "Dallas";
public class Customer { public int CustomerId { get; private set; } public string Name { get; set; } public string City { get; set; } public Customer(int Id) { CustomerId = Id; } public override string ToString() { return Name + "t" + City + "t" + CustomerId; } } Customer c = new Customer(100); c.Name = "James"; c.City = "Dallas"; |
Conclusion:
Hope this helps,
Happy Coding.
I have read couple of articles on your blog and could say it was really interesting, thanks for sharing that.
Super site, and nice text.
Hi just stumbled your blog and have been reading, do you also own another a pet related site that looks exactly like this one?
Hello just came across your blog and have been browsing around, do you also own another a pet related website that looks exactly like this one?
great article, i just finished bookmarking it for future reference. i would like to revisit on future posts. how do i set up the rss reader again? thanks so much!
I got to this blog through Facebook (my friend posted it). After reading it, I then clicked Like and also reshared it. More power to you.
I got to this article from Facebook (someone posted it). After reading, I of course clicked “Like” then reshared it. More power to you.
I just added your blog site to my blogroll, I pray you would give some thought to doing the same.
Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.
It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.