C # partial local type

Source: Internet
Author: User

Local type (partial)

I personally understand that is to divide a whole into parts.

We use a piece of code to understand

  /// <summary>
  /// 定义MyClass类的一个部分
  /// </summary>
  public partial class MyClass
  {
     public void RtuStrin()
     {
     }   
  }
  /// <summary>
  /// 定义MyClass类的另一个部分
  /// </summary>
  /// 此地方不用加public,因为在前边一个部分上已加上了public
  partial class MyClass
  {
    public int IPartialValue;
  }
  class Program
  {
    static void Main(string[] args)
    {
      //实例化类
      //此时MyClass类是由两分MyClass的部分组成即 "public partial class MyClass"+" partial class MyClass"
      MyClass myCa = new MyClass();
      //调用类的字段即"partial class MyClass"中的字段
      myCa.IPartialValue = 10;
      //调用类的方法即"public partial class MyClass"中的方法
      myCa.RtuStrin();
    }
  }

Below I look for articles on the web (from)

1. What is a local type?

C # 2.0 introduces the concept of a local type. A local type allows us to divide a class, struct, or interface into several parts and implement it in several different. cs files, respectively.

The local type applies to the following situations:

(1) The type is particularly large and should not be implemented in a single file.

(2) A part of a type of code generated by the Automation tool is not appropriate to mix with the code we write ourselves.

(3) Many people need to cooperate to write a class.

A local type is a compilation of a pure language layer that does not affect any execution mechanism--in fact, the C # compiler still merges local types of parts into a complete class at compile time.

 public partial class Program
 {
  static void Main(string[] args)
  {
  }
 }
 partial class Program
 {
  public void Test()
  {
  }
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.