Jon*_*eet 11
我有一篇关于此的文章:Bluffer的C#3指南.显然,我的书中有更多细节,但它应该足以让你前进.简而言之:
自动实现的属性:
public int Value { get; set; }
Run Code Online (Sandbox Code Playgroud)对象和集合初始值设定项:
Form form = new Form { Size = new Size(100, 100),
Controls = { new Label { Text = "Hi" } }
};
List<string> strings = new List<string> { "Hi", "There" };
Run Code Online (Sandbox Code Playgroud)隐式输入的局部变量:
var x = new Dictionary<string, int>(); // x is still statically typed
Run Code Online (Sandbox Code Playgroud)隐式类型数组:
DoSomething(new[] { "hi", "there"}); // Creates a string array
Run Code Online (Sandbox Code Playgroud)匿名类型:
var jon = new { Name = "Jon", Age = 33 };
Run Code Online (Sandbox Code Playgroud)Lambda表达式(比如匿名方法但更短):
Func<string, int> lengthFunc = x => x.Length;
Run Code Online (Sandbox Code Playgroud)表达树:
// Representation of logic as data
Expression<Func<string, int>> lengthExpression = x => x.Length;
Run Code Online (Sandbox Code Playgroud)扩展方法:(静态方法,就像第一个参数类型的实例方法一样)
public static string Reverse(this string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
...
string hello = "olleh".Reverse();
Run Code Online (Sandbox Code Playgroud)查询表达式:
var query = from person in people
where person.Age > 18
select person.Name;
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
3906 次 |
| 最近记录: |