01.自动实现的属性,编译器编译时会创建一个私有的匿名的字段,该字段只能通过属性的get和set访问器进行访问
class Person
{
//不定义私有字段并不是此时没有了私有字段,只是编译器帮我们生成一个匿名的私有字段
public string Name { get; set; }
//相当于
//private string _name;
//public string Name { get => _name; set => _name = value; }
}
02.在结构体中使用自动属性时,则所有构造函数都需要显式地调用无参构造函数this(),否则,就会出现编译时错误,因为只有显式调用无参构造函数this(),编译器才知道所有字段都被赋值了
class TestPerson
{
public struct Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name):this()
{
this.Name = name;
}
}
public Person person=new Person("du7d8u");
}
03.匿名类型
通过使用 new 运算符和对象初始值创建匿名类型
匿名类型是直接从对象派生的类类型,并且其无法强制转换为除对象外的任意类型
匿名类型提供了一种有效的方法,可以将一组只读对象组合到一个对象中,而不需要显式定义这个数据类型。
匿名类型的类型由编译器根据分配给其属性的值自动生成。
C#匿名类型使用关键字var声明,并使用new关键字初始化一个匿名类型。
static void Main(string[] args)
{
var stu1 = new {name = "一休", age = 25};
var stu2 = new {name="法师",age=30};
var stu2 = new {name="对的",Adress=new {ip="192.168.01.01"}};
WriteLine($"{stu1.name}+{stu1.age}");
WriteLine($"{stu2.name}+{stu2.age}");
ReadKey();
}
匿名类型数组
//匿名数组中的每一个匿名对象的属性名以及属性值类型要完全一致
//否则编译器会报错
var customers = new[]
{
new {name = "一休", age = 20},
new {name = "静香", age = 40},
new {name = "一休", age = 20},
};
WriteLine(customers[1].name); //静香
从特定对象中抽取部分属性构造成匿名对象
using System;
using static System.Console;
namespace Other2
{
class Program
{
static void Main(string[] args)
{
//定义了一个Customer类并初始化了一个customer对象,
Customer customer =new Customer()
{
Id = 1,
Name = "一席",
Time = DateTime.Now,
};
//随后使用这个customer对象的部分属性构造了一个匿名的对象anonymousCustomer
var anonymousCustomer = new
{
customer.Id,
customer.Name,
customer.Time,
};
WriteLine($"{anonymousCustomer.Id}{anonymousCustomer.Name}{anonymousCustomer.Time}");
ReadKey();
}
}
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
}
}
04.隐式类型
用关键字var定义的变量则该变量就是为隐式类型,var 关键字告诉编译器根据变量的值类推断变量的类型。
所以对于编译器而言,隐式类型同样也是显式的,同样具有一个显式的类型。
// 编译器推断为int[]类型
var intarray = new[] {1, 2, 3, 4};
05.对象集合初始化
using System;
using static System.Console;
namespace Init
{
class Program
{
static void Main(string[] args)
{
//调用无参构造函数来对类中的字段进行初始化
Person person = new Person() {Age = 20,Name = "一休"};
//调用有参构造函数
Person person1=new Person("大熊",20);
ReadKey();
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// 定义无参的构造函数
/// 如果类中自定义了带参数的构造函数,则编译不会生成默认的构造函数
/// 如果没有默认的构造函数,则使用对象初始化时就会报错说没有实现无参的构造函数
/// </summary>
public Person()
{
}
public Person(string Name, int Age)
{
this.Age = Age;
this.Name = Name;
}
}
}
list初始化
```csharp
var name=new List<String>();
name.Add("1");
name.Add("2");
//等效于
var names = new List<string>
{
"1","2"
};
06对象集合初始化和集合初始化
using System;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
namespace Type1
{
class Program
{
static void Main(string[] args)
{
Book book=new Book(){Price = 100,Title = "5225"};
//book的创建相当于
Book book3 = new Book();
book3.Price = 200f;
book3.Title = "滴滴";
Book book1=new Book(100,"一唏嘘");
List<Book> list=new List<Book>()
{
book,book1,new Book(){Price = 100,Title = "5225"}
};
WriteLine(list.Count);
ReadKey();
}
}
class Book
{
public string Title { get; set; }
public float Price { get; set; }
public Book()
{
}
public Book(float Price, string Title)
{
this.Price = Price;
this.Title = Title;
}
}
}