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;
}
}

c#3.0新特性的更多相关文章

  1. 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?

    来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...

  2. Java基础和JDK5.0新特性

    Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...

  3. Visual Studio 2015速递(1)——C#6.0新特性怎么用

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  4. atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性

    atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性   1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...

  5. 背水一战 Windows 10 (1) - C# 6.0 新特性

    [源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...

  6. C# 7.0 新特性2: 本地方法

    本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  7. C# 7.0 新特性1: 基于Tuple的“多”返回值方法

    本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...

  8. C# 7.0 新特性3: 模式匹配

    本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...

  9. C# 7.0 新特性4: 返回引用

    本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  10. C#发展历程以及C#6.0新特性

    一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...

随机推荐

  1. rmarkdown教程

    https://github.com/yihui/r-ninja/blob/master/11-auto-report.md http://rpubs.com/about/getting-starte ...

  2. Web爬虫入门

    1.0示例学习:Web爬虫 public class WebCrawler { // 种子url private static String url = "http://www.cnblog ...

  3. RabbitMQ学习系列(一): 介绍

    1. 介绍 RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue )协议的开源实现.用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面都非 ...

  4. 百度编辑器ueditor插入表格没有边框颜色的解决方法

    附:从word excel 中 复制的表格提交后无边框,参考这个同学的,写的很详细:   http://blog.csdn.net/lovelyelfpop/article/details/51678 ...

  5. URL-Short

    Fortify URL http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html 1.Arstechnica http://a ...

  6. AssetBundle

    AssetBundle是Unity推荐的一种资源打包方式,与不使用AssetBundle相比,它有如下优点: 1.AssetBundle是经过LZMA压缩过的,所以体积更小. 2.可以将AssetBu ...

  7. dos命令行 指令

    盘符: 例如想进入D盘 d: ../ 上一层目录 md 文件夹名 新建文件夹cd 文件夹名 进入到该目录cd.. 返回上一层目录cd\ 返回根目录 cd \windows 进入到当前盘Windows目 ...

  8. c语言经典算法—求0—7 所能组成的奇数个数

    题目:求0—7 所能组成的奇数个数. 算法思想:这个问题其实是一个排列组合的问题,设这个数为sun=a1a2a3a4a5a6a7a8,a1-a8表示这个数的某位的数值,当一个数的最后一位为奇数时,那么 ...

  9. js事件技巧方法整合

    window.resizeTo(800,600); //js设置浏览器窗口尺寸 window.open (function(){ resizeTo(640,480);//设置浏览器窗口尺寸 moveT ...

  10. Linux 双网卡绑定

    Linux 双网卡绑定 Linux 双网卡绑定双网卡绑定的常用模式:mode1:active-backup 模式,即主备模式.mode0:round-broin 模式,即负载均衡模式(需要交换机配置聚 ...