原文地址:C#语言中类的构造函数详解
作者:摆渡者
默认构造函数
数据类型 | 默认值 |
bool | false |
byte | 0 |
char | ‘’ |
decimal | 0.0M |
double | 0.0M |
enum | 枚举值组合的第一个值 |
float | 0.0F |
int | 0 |
long | 0L |
sbyte | 0 |
short | 0 |
struct | 将所有的值类型字段设置为默认值,将所有的引用类型字段设置为null时产生的值 |
uint | 0 |
ulong | 0 |
ushort | 0 |
1: public class Student
2: {
3: private string name;
4: public string Name
5: {
6: get { return this.name; }
7: set { this.name = value; }
8: }
9:
10: private int age;
11: public int Age
12: {
13: get { return this.age; }
14: set { this.age = value; }
15: }
16:
17: public Student(string name, int age)
18: {
19: this.name = name;
20: this.age = age;
21: }
22: }
1: class Program
2: {
3: static void Main()
4: {
5: Student stu = new Student();
6: }
7: }
1: public Student() {}
构造函数在类内部的调用
1: public class Student
2: {
3: private string name;
4: public string Name
5: {
6: get { return this.name; }
7: set { this.name = value; }
8: }
9:
10: private int age;
11: public int Age
12: {
13: get { return this.age; }
14: set { this.age = value; }
15: }
16:
17: private string grade;
18: public string Grade
19: {
20: get { return this.grade; }
21: set { this.grade = value; }
22: }
23:
24: public Student()
25: {
26: Console.WriteLine("一个学生对象被实例化");
27: }
28:
29: public Student(string name, int age)
30: : this()
31: {
32: this