C #3.0 New Features

Source: Internet
Author: User

• Implicit local variables and Arrays

VaR I = 5;

VaR A = new [] {0, 1, 2 };

• Object Initial Value Setting item

Private class cat
{
Public int age {Get; set ;}
Public string name {Get; set ;}
}
Static void methoda ()
{
Cat cat = new cat {age = 10, name = "Sylvester" };{ g, y };
}

• Set initial values

List <int> digits = new list <int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List & lt; Int & gt; digits2 = new list & lt; Int & gt; {0 + 1, 12% 3, makeint ()};

• Automatically implemented attributes

Class lightweightcustomer
{
Public double totalpurchases {Get; set ;}
Public string name {Get; private set;} // read-only
Public int customerid {Get; private set;} // read-only
}

• Anonymous type

VaR v = new {amount = 108, message = "hello "};

• Extension Method

Namespace extensionmethods
{
Public static class myextensions
{
Public static int wordcount (this string Str)
{
Return Str. Split (New char [] {'', '.', '? '},
Stringsplitoptions. removeemptyentries). length;
}
}
}

Using extensionmethods;
String S = "Hello extension methods ";
Int I = S. wordcount ();

 

• Division method definition

// Definition in file1.cs
Partial void onnamechanged ();
// Implementation in file2.cs
Partial void onnamechanged ()
{
// Method body // et OD body
}

• Lambda expressions

Lambda expressions

VaR Results = people. Where (P => P. lastname = "white ");

Delegate int del (int I );
Del mydelegate = x => X * X;
Int J = mydelegate (5); // J = 25

Lambda statements

Delegate void testdelegate (string S );
... ...
Testdelegate MYDEL = n => {string S = N + "" + "world"; console. writeline (s );};
MYDEL ("hello ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.