--------------------------------------------------------------------------
第一部分 基础知识
第1章 c#开发的进化史
2002 c#1.0 .net1.0
2003 c#1.2 .net1.1
2005 c#2 .net2.0 .net.3.0
2008 c#3 .net3.5
第2章 c#1所搭建的核心基础
第二部分 c#2:解决 c#1的问题
第三部分 c#3--革新写代码的方式
public string Name { get; private set;}
用 var 声明局部变量
Person tom = new Person { Name="tom", Age=4}
var names = new List<string>{"aa","bb","cc","dd"}
Lambda表达式
扩展方法
Linq表达式
----------------------------------------------------------------------------
深入解析c#(2)
第一部分 基础知识
第1章 c#开发的进化史
1.1 从简单的数据类型开始
c#1 :
[Description("Listing 1.01")]
public class Product
{
string name;
public string Name
{
get { return name; }
}
decimal price;
public decimal Price
{
get { return price; }
}
public Product(string name, decimal price)
{
this.name = name;
this.price = price;
}
public static ArrayList GetSampleProducts()
{
ArrayList list = new ArrayList();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
}
public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
c#1 存在的局限:
1.ArrayList没有提供与其内部内容有关的编辑时信息
2.属性的赋值方法也必须是公共的
3.用于创建属性和变量的代码很复杂
c#2.0:
[Description("Listing 1.02")]
public class Product
{
string name;
public string Name
{
get { return name; }
private set { name = value; }
}
decimal price;
public decimal Price
{
get { return price; }
private set { price = value; }
}
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
public static List<Product> GetSampleProducts()
{
List<Product> list = new List<Product>();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
}
public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
解决c#1的2个问题:
1.属性拥有了私有的赋值方法
2.泛型
c#3.0 :
[Description("Listing 1.3")]
class Product
{
public string Name { get; private set; }
public decimal Price { get; private set; }
//public Product(string name, decimal price)
//{
//Name = name;
//Price = price;
//}
Product()
{
}
public static List<Product> GetSampleProducts()
{
return new List<Product>
{
new Product { Name="West Side Story", Price = 9.99m },
new Product { Name="Assassins", Price=14.99m },
new Product { Name="Frogs", Price=13.99m },
new Product { Name="Sweeney Todd", Price=10.99m}
};
}
public override string ToString()
{
return string.Format("{0}: {1}", Name, Price);
}
}
解决c#1中:
自动实现属性
第二部分 c#2:解决c#1的问题
1.泛型
为什么要用泛型?强制转换;参数安全检查
类型约束: 1. 引用类型约束 2.值类型约束 3.构造函数类型约束 4. 转换类
型约束 5.组合约束
泛型的限制: 1.泛型可变性的缺乏 不支持协变性
class ComparisonHelper<TBase, TDerived> : IComparer<TDerived>
where TDerived : TBase
{
private readonly IComparer<TBase> comparer;
public ComparisonHelper(IComparer<TBase> comparer)
{
this.comparer = comparer;
}
public int Compare(TDerived x, TDerived y)
{
return comparer.Compare(x, y);
}
}
2. 缺乏操作符约束或者“数值”约束
3. 缺乏泛型属性,索引器,和其他成员类型
2.可空类型
Nullable<T> = T? (语法糖)
Nullable<T> 是值类型 是结构 , Nullable 静态类
1. 与null值进行比较
DateTime birth;
DateTime? death;
string name;
public TimeSpan Age
{
get
{
if (death == null)
{
return DateTime.Now - birth;
}
else
{
return death.Value - birth;
}
}
}
2.可空转换和操作符
3.可空逻辑
4.对可空类型使用as操作符
5.空合并操作符
6.可空类型的新奇用法[Description("Listing 4.6")]
public static class PartialComparer
{
public static int? Compare<T>(T first, T second)
{
return Compare(Comparer<T>.Default, first, second);
}
public static int? Compare<T>(IComparer<T> comparer,
T first,
T second)
{
int ret = comparer.Compare(first, second);
return ret == 0 ? new int?() : ret;
}
public static int? ReferenceCompare<T>(T first, T second)
where T : class
{
return first == second ? 0
: first == null ? -1
: second == null ? 1
: new int?();
}
public static bool? ReferenceEquals<T>(T first, T second)
where T : class
{
return first == second ? true
: first == null ? false
: second == null ? false
: new bool?();
}
}
3.委托
协变和逆变 匿名方法
4.迭代器
分部类型 静态类 独立取值方法 / 赋值方法属性访问器 命名空间别名 Pragma指令 固定大小的缓冲区 友元程序集
第三部分 c#3:革新写代码的方式
第8章 用智能的编译器来防错
自动实现的属性 用var声明局部变量 简单的初始化
第9章 Lambda表达式和表达式树
对linq数据管线中的操作进行表示的一种符合语言习惯的方式
一般的匿名方法:Func<string, int> dd = delegate(string text) { return text.Length; };
lambda: Func<string, int> dd = (string text) => { return text.Length; };
---------> : Func<string, int> dd = (string text) => text.Length;
---------> : Func<string, int> dd = text => text.Length;
---------> : Func<string, int> dd = p => p.Length;
第10章 扩展方法
public static class RegisteExtensions
{
public static string Css(this HtmlHelper html, params string[] cssfilename)
{
string folderpath = "~/Scripts/ext/resources/css";
string csslink = " <link href=\"{0}\" rel=\"Stylesheet\" type=\"text/css\" />";
StringBuilder sb = new StringBuilder();
foreach (string filename in cssfilename)
{
sb.AppendFormat(csslink, UrlHelper.GenerateContentUrl(folderpath + "/" + filename + ".css", html.ViewContext.HttpContext));
}
return sb.ToString();
}
public static string Js(this HtmlHelper html, params string[] jsKeys)
{
string jslink = "<script src='{0}' type='text/javascript'></script>";
StringBuilder sb = new StringBuilder();
foreach (string key in jsKeys)
{
var jsurl = JsConfig.GetJsUrl(key, html.ViewContext.HttpContext);
if (string.IsNullOrEmpty(jsurl))
continue;
sb.AppendFormat(jslink, UrlHelper.GenerateContentUrl(jsurl, html.ViewContext.HttpContext));
}
return sb.ToString();
}
}
第11章 查询表达式和Linq to objects
var query = from item in dt.AsEnumerable()
where item.Field<DateTime>("f_starthour") == DateTime.Parse(startRealTime)
where item.Field<string>("f_energyitemcode") == code1
select item;
foreach (DataRow dr in query)
{
value = double.Parse(dr["f_hourvalue"].ToString());
}
第12章 超越集合的Linq
第四部分 c#4:良好的交互性