using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Student student = new Student() {ID = 101, Name = "ll"};
object obj = student;
Student s2 = (Student) obj;
Console.WriteLine($"#{s2.ID}");
Console.WriteLine("===============");
Student stu1 = new Student() {ID = 101, Name = "ll"};
Student stu2 = stu1;
stu2.ID = 1001;
stu2.Name = "zs";
Console.WriteLine($"ID:{stu1.ID},Name:{stu1.Name}");
Console.WriteLine($"ID:{stu2.ID},Name:{stu2.Name}");
}
}
struct Student
{
public int ID { get; set; }
public string Name { get; set; }
}
}
结构体可以实现接口
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student() {ID = 101, Name = "ll"};
Student stu2 = stu1;
stu2.ID = 1001;
stu2.Name = "zs";
stu1.Sepak();
}
}
interface ISpeak
{
void Sepak();
}
struct Student : ISpeak
{
public int ID { get; set; }
public string Name { get; set; }
public void Sepak()
{
Console.WriteLine($"I'm #{this.ID}, Name:{this.Name}");
}
}
}
结构体不能是父类,即不能被继承
结构体不能有显式的无参构造器
结构体可以有显式的有参构造器
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(111, "lll") {ID = 222, Name = "sss"};
Console.WriteLine($"#ID:{stu1.ID}, Name:{stu1.Name}");
}
}
interface ISpeak
{
void Sepak();
}
struct Student : ISpeak
{
public Student(int id, string name)
{
this.ID = id;
this.Name = name;
}
public int ID { get; set; }
public string Name { get; set; }
public void Sepak()
{
Console.WriteLine($"I'm #{this.ID}, Name:{this.Name}");
}
}
}