简体   繁体   English

具有相同名称方法的部分类

[英]Partial class with same name method

I have a partial class like this 我有一个像这样的局部课

public partial class ABC
{
  public string GetName()
  {
     //some code here
  }

  public string GetAge()
  {
     //some code here
  }    
}

public partial class ABC
{
  public string GetSex()
  {
     //some code here
  }

  public string GetAge()
  {
     //some code here
  }    
}

How these 2 class merge at build time? 这两个类在构建时如何合并? Please give me explanation about it. 请给我解释一下。

There will be a compile time error when you try to compile this code! 当您尝试编译此代码时,将出现编译时错误

What happens at build time is the compiler combines all the members defined in all the partial definitions of the class into one . 在构建时发生的事情是compiler将在类的所有部分定义中定义的所有成员合并为一个 It will then try to compile it the usual way. 然后它将尝试以通常的方式编译它。

In your case it will raise an error mentioning you already have defined a method with the same name . 在您的情况下,它会引发错误,提到您已经定义了具有相同名称的方法

它不能编译,因为在一个类中不能有两个具有相同名称的方法。

Even besides the syntactic errors your code won't compile. 即使除了语法错误之外,您的代码也无法编译。 You will get the following error: 您将收到以下错误:

Type 'MyNamespace.ABC' already defines a member called 'GetAge' with the same parameter types 类型'MyNamespace.ABC'已经定义了一个名为'GetAge'的成员,它具有相同的参数类型

This is because the compiler will merge all parts of a partial class into a single class as Section 10.2 of the C# Language Specification explains: 这是因为编译器会将部分类的所有部分合并到一个类中,如C#语言规范的第10.2节所述:

With the exception of partial methods (§10.2.7), the set of members of a type declared in multiple parts is simply the union of the set of members declared in each part. 除了部分方法(第10.2.7节)之外,在多个部分中声明的类型的成员集只是每个部分中声明的成员集的并集。 The bodies of all parts of the type declaration share the same declaration space (§3.3), and the scope of each member (§3.7) extends to the bodies of all the parts. 类型声明的所有部分的主体共享相同的声明空间(第3.3节),每个成员的范围(第3.7节)扩展到所有部分的主体。

C# won't allow to have to methods with the same names and with the same number and types of arguments within one single class. C#不允许在一个单独的类中使用具有相同名称和相同数量和类型的参数的方法。 This is stated in section 1.6.6 of the specification: 这在规范的1.6.6节中说明:

The signature of a method must be unique in the class in which the method is declared. 方法的签名在声明方法的类中必须是唯一的。 The signature of a method consists of the name of the method, the number of type parameters and the number, modifiers, and types of its parameters. 方法的签名包括方法的名称,类型参数的数量以及数量,修饰符和参数类型。 The signature of a method does not include the return type. 方法的签名不包括返回类型。

There is one option though to add the declaration of a method into one part of a partial class and the implementation into another: Partial Methods . 但是有一个选项可以将方法的声明添加到部分类的一部分中,将实现添加到另一部分中部分方法 You can read more about them in Eric Lippert's blog post on that topic: 您可以在Eric Lippert关于该主题的博客文章中阅读有关它们的更多信息:

What's the difference between a partial method and a partial class? 部分方法和部分类之间有什么区别?

Partial classes are merged during compilation. 部分类在编译期间合并。 The compilers look for the partial classes and integrate them while compiling. 编译器在编译时查找部分类并集成它们。 It just combines "two" partial classes into one class. 它只是将“两个”部分类组合成一个类。 There is no modification done in the CLR for the implementation of partial classes. CLR中没有对部分类的实现进行任何修改。 You can consider it just like merging of "two" partial classes. 您可以将其视为合并“两个”部分类。

For example for your code you will have : 例如,对于您的代码,您将拥有:

public partial class ABC
{
  public string GetName()
  {
     //some code here
  }

  public string GetAge()
  {
     //some code here
  }

  public string GetSex()
  {
     //some code here
  }

  public string GetAge()
  {
     //some code here
  }
}

And it will give you an error because you can't have 2 methods with the same name and signature (see GetAge method). 它会给你一个错误,因为你不能有两个具有相同名称和签名的方法(参见GetAge方法)。

它们不合并:您将遇到编译时错误。

They won't merge: compile time error. 它们不会合并:编译时错误。 They might merge in your case if you accidentally put them in different namespaces. 如果您不小心将它们放在不同的命名空间中,它们可能会合并到您的案例中。

The preprocessor (or compiler maybe) scans Your project folder during one of his runs, and checks the classes names that are in a project(or assembly to be precise). 预处理器(或编译器可能)在其中一次运行期间扫描您的项目文件夹,并检查项目中的类名称(或精确的程序集)。 Then it marks partial classes and checks whether there are mutliple definitions of them. 然后它标记了部分类并检查它们是否有多个定义。
Ask Eric Lippert about the details. 向Eric Lippert询问细节。 It then merges methods, comments, attributes, members, interfaces etc. HAve a read at c# lang specification. 然后它合并方法,注释,属性,成员,接口等。阅读c#lang规范。 Your methods do no have partial modfier so as guys before me noticed, it won't compile. 你的方法没有部分修改器,所以在我之前的人注意到,它不会编译。

Try this: 试试这个:

public class ABC
{
  public string GetName()
  {
    //some code here
  }

  public string GetAge()
  {
    //some code here
  }
}

public partial class ABC
{
  public string GetSex()
  {
    //some code here
  }

  public string GetAge()
  {
    //some code here
  }    
}

Leave the partial out of the first class ! 部分留在第一堂课!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM