Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 505
Pankaj Avatar asked Jul 02 '10 10:07

Pankaj


2 Answers

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.

like image 140
decyclone Avatar answered Oct 06 '22 01:10

decyclone


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

like image 31
Mattias Jakobsson Avatar answered Oct 06 '22 00:10

Mattias Jakobsson