C# 3.5 语言功能 读书笔记
1.Var :隐含类型化的局部变量
1.1 编译器会根据局部变量初始化表达式来推断变量的类型
1.2 Var声明仅限于局部变量,也可包含在foreach、for、using语句中
例子1:
class Program
{
static void Main(string[] args)
{
var s1 = "My Name is xiaoxiao";//相当于string s1="My Name is xiaoxiao";
var s2 = 20;
var s3 = 1.0;
var s4 = new string[] { "xiaoxiao", "xiaofang", "xiaoming" };
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
foreach (var ss in s4)//Var声明仅限于局部变量,也可包含在foreach、for、using语句中
{
Console.WriteLine(ss);
}
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
var s1 = "My Name is xiaoxiao";//相当于string s1="My Name is xiaoxiao";
var s2 = 20;
var s3 = 1.0;
var s4 = new string[] { "xiaoxiao", "xiaofang", "xiaoming" };
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
foreach (var ss in s4)//Var声明仅限于局部变量,也可包含在foreach、for、using语句中
{
Console.WriteLine(ss);
}
Console.ReadLine();
}
}
结果如下:
例子2:扩展方法:
2.1 是一种特殊的静态方法,定义在静态类中,可在其他类的对象上调用实例方法那样调用
2.2 对一个类型进行功能上的补充
2.3 扩展方法和一般静态方法的定义方法类似唯一区别是在第一个参数前面要加上关键字this作为修饰符
同时第一个参数的类型也绝顶了扩展方法可以扩展的类型
2.4 扩展方法语法
public static 返回类型 扩展方法名(this 要扩展的类型 X){.........}
View Code
class MyExt
{
int Age = 20;
public int myAge
{
set { Age = value; }
get { return Age; }
}
public void Write()
{
Console.WriteLine("大家好! 我是小七 我的年龄是{0}",Age);
}
}
//定义扩展的类
static class myClass
{
public static void ExMyExt(this MyExt n)
{
Console.WriteLine("扩展MyExt类型");
n.myAge = 22;
n.Write();
}
}
class Program
{
static void Main(string[] args)
{
MyExt mx = new MyExt();
Console.WriteLine("这是静态方法!");
mx.Write();
Console.WriteLine();
Console.WriteLine("这是一个扩展方法");
mx.ExMyExt();
Console.ReadKey();
}
}
{
int Age = 20;
public int myAge
{
set { Age = value; }
get { return Age; }
}
public void Write()
{
Console.WriteLine("大家好! 我是小七 我的年龄是{0}",Age);
}
}
//定义扩展的类
static class myClass
{
public static void ExMyExt(this MyExt n)
{
Console.WriteLine("扩展MyExt类型");
n.myAge = 22;
n.Write();
}
}
class Program
{
static void Main(string[] args)
{
MyExt mx = new MyExt();
Console.WriteLine("这是静态方法!");
mx.Write();
Console.WriteLine();
Console.WriteLine("这是一个扩展方法");
mx.ExMyExt();
Console.ReadKey();
}
}
结果如下
例子3 对象初始化器和集合初始化器
3.1 作用简化代码
View Code
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> bk = new List<Book>
{
new Book{BooKName="C#",Price=56,content="好看",Num=50},
new Book{BooKName="ASP.NET",Price=46,content="哈哈",Num=30},
new Book{BooKName="J2EE",Price=88,content="不错",Num=59},
//null,
};
foreach (var str in bk)
{
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> bk = new List<Book>
{
new Book{BooKName="C#",Price=56,content="好看",Num=50},
new Book{BooKName="ASP.NET",Price=46,content="哈哈",Num=30},
new Book{BooKName="J2EE",Price=88,content="不错",Num=59},
//null,
};
foreach (var str in bk)
{
Console.WriteLine(str.BooKName);
Console.WriteLine(str.Price);
Console.WriteLine(str.content);
Console.WriteLine();
}
Console.ReadKey();
}
}
结果:
例子4 匿名类型对象初始化器
View Code
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", Price = 50 };
var s2 = new { Name = "C#", Price = 35 };
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", Price = 50 };
var s2 = new { Name = "C#", Price = 35 };
Console.WriteLine(s1.GetType());
Console.WriteLine(s2.GetType());
Console.ReadKey();
}
}
结果:
例子5 Lambda表达式
参数列表=>语句(语句集)
View Code
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();
}
}
结果:
例子6:
C#3.5中可以不定义私有变量直接使用空的get/set属性,并且会自动自动在类中 生成一个私有成员变量
View Code
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; }
}