c#部分类

c#提供了一个部分类,它只显示类的一部分,用关键字partical修饰

using System;

public partial class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Points { get; set; }
}
public partial class Course
{
    public void PrintCoures()
    {
        Console.WriteLine("课程编号:" + Id);
        Console.WriteLine("课程名称:" + Name);
        Console.WriteLine("课程学分:" + Points);
    }
}
public class Start
{
    static void Main(string[] args)
    {
        Course course = new Course();
        course.Id = 1001;
        course.Name = "C#部分类";
        course.Points = 3;
        course.PrintCoures();
    }
}

优点:

1.不同的不分类可以直接互相访问其成员

2.部分类类名必须相同

注意事项:

部分方法必须是私有的,并且不能使用 virtual、abstract、override、new、sealed、extern 等修饰符。

部分方法不能有返回值。

在部分方法中不能使用 out 类型的参数。

猜你喜欢

转载自www.cnblogs.com/jestin/p/12024863.html