开发者

Partial class with same name method

开发者 https://www.devze.com 2023-01-05 17:38 出处:网络
I have a partial class like this public partial class ABC { public string GetName() { //some code here开发者_如何学编程

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. 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.


It doesn't compile as you can't have two methods with the same name in one class.


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

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:

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. 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.

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. This is stated in section 1.6.6 of the specification:

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:

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. 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).


They don't merge: you'll have a compile-time error.


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. It then merges methods, comments, attributes, members, interfaces etc. HAve a read at c# lang specification. 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 !

0

精彩评论

暂无评论...
验证码 换一张
取 消