C #3.0 new features quick preview

Source: Internet
Author: User

1. Use the VaR keyword to implement flexible type declaration:
Class implicitlytypedlocals2
{
Static void main ()
{
String [] words = {"apple", "blueberry", "Cherry "};

// If a query produces a sequence of anonymous types,
// Then you must also use VaR in the foreach statement.
VaR upperlowerwords =
From W in words
Select New {upper = W. toupper (), lower = W. tolower ()};

// Execute the query
Foreach (VAR ul in upperlowerwords)
{
Console. writeline ("uppercase: {0}, lowercase: {1}", ul. Upper, ul. Lower );
}
}
}
2. More flexible object construction methods:
Private class cat
{
// Auto-implemented Properties
Public int age {Get; set ;}
Public string name {Get; set ;}
}
Static void methoda ()
{
// Object initializer
Cat cat = new cat {age = 10, name = "Sylvester "};
}
3. Special Methods for assembly:
Ienumerable <int> highscoresquery =
From score in scores
Where score> 80
Orderby score descending
Select score;
4. Extend methods for existing type definitions:
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 ();
5. implicit set type definition:
VaR v = new {amount = 108, message = "hello "};
VaR productquery =
From prod in products
Select New {prod. Color, prod. Price };

Foreach (var v in productquery)
{
Console. writeline ("color = {0}, price = {1}", V. Color, V. Price );
}

6. Lambda Syntax:
Delegate int del (int I );
Del mydelegate = x => X * X;
Int J = mydelegate (5); // J = 25
7. query syntax:
Class lownums
{
Static void main ()
{
// A simple data source.
Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

// Create the query.
// Lownums is an ienumerable <int>
VaR lownums = from num in numbers
Where num <5
Select num;

// Execute the query.
Foreach (int I in lownums)
{
Console. Write (I + "");
}
}
}
// Output: 4 1 3 2 0
8. Smart and simple accessors:
Class lightweightcustomer
{
Public double totalpurchases {Get; set ;}
Public string name {Get; private set;} // read-only
Public int customerid {Get; private set;} // read-only
}

Lightweightcustomer OBJ = new lightweightcustomer ();
OBJ. totalpurchases = 1;
Console. writeline (obj. totalpurchases. tostring ());

9. Support partial declarations for excuses, classes, structures, methods, and features:
Partial class Earth: Planet, irotate {}
Partial class Earth: irevolve {}
[System. serializableattribute]
Partial Class moon {}
[System. obsoleteattribute]
Partial Class moon {}

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.