在C#中,构造器(Constructor)是一种特殊类型的函数,用于初始化类的新实例。构造器的名字必须与类名完全相同,并且没有返回类型,甚至连void也不行。
当创建类的一个新实例时,构造器会自动被调用。

下面是两种是两种构造器的声明与调用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConstructorExample
{
internal class Program
{
static void Main(string[] args)
{
Student stu= new Student();
Console.WriteLine(stu.Id);
Console.WriteLine(stu.Name);

最低0.47元/天 解锁文章
1012

被折叠的 条评论
为什么被折叠?



