2.5 C#3.5语言功能 读书笔记
C#3.5支持基于语言的查询框架(LINQ),可以在语言上实现类似SQL的查询。
特性:
隐含本地变量类型
对象初始化器
Lambda表达式,一个匿名方法的进化体
扩展方法,可以为现有类型添加新的方法
匿名类型,可以从对象初始化器自动创建
2.5.1 隐含类型化的局部变量:
一个局部变量被声明为var,就被视为隐含类型局部变量声明。
如:var i=5;
使用范围:var的声明仅限于局部变量,也可以包含在foreach,for,using语句中。
代码2.13 myVar.cs
var s1 = "My Name is xiaoxiao";//相当于string s1="My Name is xiaoxiao";
var s2 = 20;//相当于int s1=20;
var s3 = 1.0;//相当于double c=1.0;
var s4 = new string[] { "xiaoxiao", "xiaofang", "xiaoming" };
//相当于string s4=new string[]{"xiaoxiao","xiaofang","xiaoming"};
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
foreach (var ss in s4)
{
Console.WriteLine(ss);
}
Console.ReadLine();
var s2 = 20;//相当于int s1=20;
var s3 = 1.0;//相当于double c=1.0;
var s4 = new string[] { "xiaoxiao", "xiaofang", "xiaoming" };
//相当于string s4=new string[]{"xiaoxiao","xiaofang","xiaoming"};
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
foreach (var ss in s4)
{
Console.WriteLine(ss);
}
Console.ReadLine();
输出结果如下:

2.5.3 对象与集合初始化器
使用对象初始值设定项可以在创建对象时向对象的任何可访问的字段火属性分配值,而无需显式调用构造函数
代码2.17 myObject.c
public class Book
{
public string BookName { get ; set; }
public int Price { get; set; }
public string content { get; set; }
internal int Num;
}
class Program
{
static void Main(string[] args)
{
Book book = new Book { BookName = "C#", Price = 56, content = "好看", Num = 50 };
Console.WriteLine(book.Num);
Console.WriteLine(book.BookName);
Console.WriteLine();
List<Book> book1 = new List<Book>()
{
new Book { BookName = "C#", Price = 56, content = "好看" },
new Book { BookName = "ASP.NET", Price = 41, content = "呵呵" },
new Book { BookName = "J2EE", Price = 54, content = "不错" },
};
foreach (var str in book1)
{
Console.WriteLine(str.BookName);
Console.WriteLine(str.Price);
Console.WriteLine(str.content);
Console.WriteLine();
}
Console.ReadKey();
}
}
{
public string BookName { get ; set; }
public int Price { get; set; }
public string content { get; set; }
internal int Num;
}
class Program
{
static void Main(string[] args)
{
Book book = new Book { BookName = "C#", Price = 56, content = "好看", Num = 50 };
Console.WriteLine(book.Num);
Console.WriteLine(book.BookName);
Console.WriteLine();
List<Book> book1 = new List<Book>()
{
new Book { BookName = "C#", Price = 56, content = "好看" },
new Book { BookName = "ASP.NET", Price = 41, content = "呵呵" },
new Book { BookName = "J2EE", Price = 54, content = "不错" },
};
foreach (var str in book1)
{
Console.WriteLine(str.BookName);
Console.WriteLine(str.Price);
Console.WriteLine(str.content);
Console.WriteLine();
}
Console.ReadKey();
}
}
输出结果如下:

2.5.4 匿名类型
匿名类型是C#和VB的方便语言特性,在3.5中,通过使用“匿名类型”,只要在需要一个这样的对象时使用没有类型名字的new表达式
使用关键字var制定匿名类型:
public class my
{
public string Name { set; get; }
public int Price { set; get; }
}
class Program
{
static void Main(string[] args)
{
var s1 = new { Name = "ASP.NET 3.0高级编程", Price = 30 };
var s2 = new { Name = "JavaScript特效", Price = 20 };
Console.WriteLine(s1.GetType());
Console.WriteLine(s2.GetType());
Console.ReadKey();
}
}
{
public string Name { set; get; }
public int Price { set; get; }
}
class Program
{
static void Main(string[] args)
{
var s1 = new { Name = "ASP.NET 3.0高级编程", Price = 30 };
var s2 = new { Name = "JavaScript特效", Price = 20 };
Console.WriteLine(s1.GetType());
Console.WriteLine(s2.GetType());
Console.ReadKey();
}
}
输出结果如下:

2.5.5 Lambda表达式
C#3.5允许使用表达式树将Lambda表达式表示为一个内存对象。
Lambda表达式的参数可以显式或隐式定义。
所以Lambda表达式的形式可以使这样的:
class Program
{
static int[] i=new int[]{1,2,3,4};
static void Main(string[] args)
{
Array.FindAll<int>(i, p =>
{
Console.WriteLine(p);
return p % 2 == 0;
});
Console.ReadKey();
}
}
{
static int[] i=new int[]{1,2,3,4};
static void Main(string[] args)
{
Array.FindAll<int>(i, p =>
{
Console.WriteLine(p);
return p % 2 == 0;
});
Console.ReadKey();
}
}
输出结果如下:

2.5.6 自动属性
当属性访问器中不需要其他逻辑时,自动实现的属性可使属性生命变得更加简洁。
如代码2.19
员工类:
public class Student
{
//private string id;
//private string name;
//private string sex;
//private int age;
//public string Id { get { return id; } set { id = value; } }
//public string Name { get { return name; } set { name = value; } }
//public string Sex { get { return sex; } set { sex = value; } }
//public int Age { get { return age; } set { age = value; } }
//简便写法
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
}
{
//private string id;
//private string name;
//private string sex;
//private int age;
//public string Id { get { return id; } set { id = value; } }
//public string Name { get { return name; } set { name = value; } }
//public string Sex { get { return sex; } set { sex = value; } }
//public int Age { get { return age; } set { age = value; } }
//简便写法
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
}
浙公网安备 33010602011771号