Search code examples
c#partial-classes

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.


Solution

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