密封类
sealed,无法被继承
var
可以定义匿名对象
static void test1()
{
var t = 1;
t = "jack";//报错,类型已经确定好了
var s = new
{
id = 1,
name = "tom"
};
Console.WriteLine(s.id + s.name);
}
扩展方法
对现有类型做方法的扩展,密封类也可以实现
不在同一命名空间需要引入,返回类型根据自己需要来决定
系统类型扩展
密封类扩展
Linq
static void Main(string[] args)
{
test1();
}
static void test1()
{
int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
List<int> list = new List<int>(nums);
var res = nums
.Where(x => x % 2 == 1)
.Select(x => x * x)
.OrderByDescending(x => x);
foreach (var item in res)
{
Console.WriteLine(item);
}
}
class Program
{
static void Main(string[] args)
{
test1();
}
static void test1()
{
Student s1 = new Student() { id = 1, name = "tom1" };
Student s2 = new Student() { id = 1, name = "tom2" };
Student s3 = new Student() { id = 2, name = "tom3" };
List<Student> list = new List<Student>() { s1, s2, s3 };
//根据id升序,id相同按照name降序
var res = list
.OrderBy(s => s.id)
.ThenByDescending(s => s.name);
foreach (var item in res)
{
Console.WriteLine(item.ToString());
}
//从1开始,按顺序生成10个数
var num1 = Enumerable.Range(1, 10);
//生成10个abcd
var num2 = Enumerable.Repeat("abcd", 10);
foreach (var item in num1)
{
Console.WriteLine(item);
}
foreach (var item in num2)
{
Console.WriteLine(item);
}
}
}