1.自动属性

   public int ID { get; set; }

    // 上面的ID属性(自动属性)等同于下面的ID属性

    // private int _id;
// public int ID
// {
// get { return _id; }
// set { _id = value; }
// }

2.对象初始化器

/// <summary>
/// ObjectInitializers(对象初始化器)的摘要说明
/// </summary>
public class ObjectInitializers
{
public int ID { get; set; }
public string Name { get; set; } public void ObjectInitializersTest()
{
ObjectInitializers oi = new ObjectInitializers { ID = , Name = "webabcd" }; // 上面的oi对象(对象初始化器)等同于下面的oi对象 // ObjectInitializers oi = new ObjectInitializers();
// oi.ID = 1;
// oi.Name = "webabcd";
}
}

3.集合初始化器

/// <summary>
/// CollectionInitializers(集合初始化器)的摘要说明
/// </summary>
public class CollectionInitializers
{
public int ID { get; set; }
public string Name { get; set; } public void CollectionInitializersTest()
{
List<CollectionInitializers> list = new List<CollectionInitializers>
{
new CollectionInitializers { ID = , Name = "webabcd" },
new CollectionInitializers { ID = , Name = "webabcdefg" },
new CollectionInitializers { ID = , Name = "webabcdefghijklmn" }
}; // 上面的list集合(集合初始化器)等同于下面的list集合 // List<CollectionInitializers> list = new List<CollectionInitializers>();
// list.Add(new CollectionInitializers { ID = 1, Name = "webabcd" });
// list.Add(new CollectionInitializers { ID = 2, Name = "webabcdefg" });
// list.Add(new CollectionInitializers { ID = 3, Name = "webabcdefghijklmn" });
}
}

4.扩展方法

/// <summary>
/// 扩展方法(类和方法均为static)
/// 使用的时候要引用该类的命名空间
/// </summary>
public static class MyExtensionMethods
{
// this代表扩展方法应用于string类型上
// ToInt32()是将string类型转换为int类型的扩展方法
public static int ToInt32(this string s)
{
int i;
Int32.TryParse(s, out i); return i;
} // this代表扩展方法应用于object类型上
// 该扩展方法需要一个类型为System.Collections.IEnumerable的参数
// In()是判断一个object是否存在于一个System.Collections.IEnumerable中的扩展方法
public static bool In(this object o, System.Collections.IEnumerable e)
{
foreach (object i in e)
{
if (i.Equals(o))
{
return true;
}
} return false;
}
}
        string s = "";
// 使用string的ToInt32()扩展方法
int i = s.ToInt32();
// i == 123 string[] ary = new string[] { "a", "b", "c" };
// 使用object的In()扩展方法
bool b = "b".In(ary);
// b == true

5.Lambda表达式

/// <summary>
/// LambdaExpressions(Lambda表达式)的摘要说明
/// </summary>
public class LambdaExpressions
{
public int ID { get; set; }
public string Name { get; set; } public void LambdaExpressionsTest()
{
List<LambdaExpressions> list = new List<LambdaExpressions>
{
new LambdaExpressions { ID = , Name = "webabcd" },
new LambdaExpressions { ID = , Name = "webabcdefg" },
new LambdaExpressions { ID = , Name = "webabcdefghijklmn" }
}; IEnumerable<LambdaExpressions> l = list.Where(le => le.Name == "webabcd"); // 上面的(Lambda表达式)等同于下面的(匿名方法) // IEnumerable<LambdaExpressions> l2 = list.Where(delegate(LambdaExpressions le) { return le.Name == "webabcd"; }); // 相关委托
// public delegate TResult Func<T, TResult>(T arg); // 相关Where扩展方法
// Func<TSource, bool>:接受一个类型为TSource的参数
// Func<TSource, bool>:某个需要满足的条件,返回bool值
// public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
// {
// foreach (TSource item in source)
// {
// if (predicate(item))
// {
// yield return item;
// }
// }
// } }
}

6.查询语法

/// <summary>
/// QuerySyntax(查询语法)的摘要说明
/// </summary>
public class QuerySyntax
{
public int ID { get; set; }
public string Name { get; set; } public void QuerySyntaxTest()
{
List<QuerySyntax> list = new List<QuerySyntax>
{
new QuerySyntax { ID = , Name = "webabcd" },
new QuerySyntax { ID = , Name = "webabcde" },
new QuerySyntax { ID = , Name = "webabcdef" },
new QuerySyntax { ID = , Name = "webabcdefg" },
new QuerySyntax { ID = , Name = "webabcdefgh" },
new QuerySyntax { ID = , Name = "webabcdefghi" },
new QuerySyntax { ID = , Name = "webabcdefghij" },
new QuerySyntax { ID = , Name = "webabcdefghijk" },
new QuerySyntax { ID = , Name = "webabcdefghijkl" },
new QuerySyntax { ID = , Name = "webabcdefghijklm" },
new QuerySyntax { ID = , Name = "webabcdefghijklmn" }
}; IEnumerable<QuerySyntax> l = from o in list
where o.Name.Length >
orderby o.Name.Length descending
select o; // 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式)
// 查询语法相对更容易理解 // IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length); // Projection(映射)
// 可以返回一个新的类型
IEnumerable<Projection> l2 = from o in list
where o.Name.Length >
orderby o.Name.Length descending
select new Projection { Name = o.Name }; var l3 = from o in list
where o.Name.Length >
orderby o.Name.Length descending
select new { Name=o.Name};
}
} /// <summary>
/// 为了演示Projection(映射)而写的实体类
/// </summary>
public class Projection
{
public string Name { get; set; }
}

7.匿名对象

/// <summary>
/// AnonymousTypes(匿名类型)的摘要说明
/// </summary>
public class AnonymousTypes
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; } public void AnonymousTypesTest()
{
List<AnonymousTypes> list = new List<AnonymousTypes>
{
new AnonymousTypes { ID = , Name = "webabcd", Age = },
new AnonymousTypes { ID = , Name = "webabcdefg", Age = },
new AnonymousTypes { ID = , Name = "webabcdefghijklmn", Age = }
}; // listAnonymousTypes - 匿名类型
var listAnonymousTypes = from l in list
where l.Name == "webabcd"
select new { Name = l.Name, Age = l.Age }; foreach (var v in listAnonymousTypes)
{
// v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
string name = v.Name;
int age = v.Age;
} // 声明匿名类型:将new关键词后面的类型名称省略掉
var person = new { Name = "webabcd", Age = };
// person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
string myName = person.Name;
int myAge = person.Age;
}
}

最新文章

  1. MVVM大比拼之avalon.js源码精析
  2. Struts2标签大全
  3. Croppic – 免费开源的 jQuery 图片裁剪插件
  4. python join
  5. Linux 多核下绑定硬件中断到不同 CPU(IRQ Affinity) 转
  6. MySQL的事务
  7. eclipse hibernate 插件测试1
  8. mouseleave 与 mouseout 的不同
  9. ACM1998
  10. 【Hihocoder 1167】 高等理论计算机科学 (树链的交,线段树或树状数组维护区间和)
  11. [总结] Stack: Java V.S. C++
  12. 【POJ】2492 A bug&#39;s life ——种类并查集
  13. PrintWriter返回值乱码问题
  14. POJ 3061 Subsequence 二分查找
  15. 将所需要的图标排成一列组成一张图片,方便管理。li的妙用
  16. 关于rabbitmq的介绍
  17. 关于 someone could be eavesdropping on you right now (man-in-the-middle attack) ssh的解决办法
  18. 洛谷P3183食物链题解
  19. Nagios图像绘制插件PNP4Nagios部署和测试
  20. iptables防火墙企业级模板

热门文章

  1. Net中对Object的定义
  2. [AR+Vuforia]学习笔记
  3. APEX初步
  4. SpringMVC学习记录3
  5. juery学习总结(一)——juery选择器
  6. leetcode--Majority Element
  7. eclipse建立springMVC 简单项目
  8. 第3月第19天 cxx_destruct dispatch_get_main_queue()死锁
  9. Handler sendMessage 与 obtainMessage (sendToTarget)比较
  10. 负载均衡-基础-一致性哈希算法及java实现