views:

97

answers:

8

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.

+5  A: 

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

Mattias Jakobsson
if 2different user working on these classes they can do this mistake.how can i prevent this.
Pankaj
@Pankaj: Make them talk to each other =)
Jens
@Pakaj - by having a good continuous integration system that doesn't allow them to check in code that won't compile.
ck
@Jens - don't be daft, they are developers. Maybe post it on SO, there's a good chance they might see it...
ck
Or dont use a partial class structure to allow different devs to work on the same class - have it as one class - let your source control handle the problem (thats what they do). The first to check in wins and the second has to merge there changes.
Leom Burke
@Pankaj, I don't see how that would be a problem that you need to prevent in some way? If the code doesn't compile, it doesn't compile. There are a million possible reasons why code doesn't compile, this is just one of them (and a very rare one that I have never encountered). Compile time errors shouldn't be to much of a problem as they are usually easy to fix.
Mattias Jakobsson
@Jens: that's *exactly* what I thought immediately on reading the question, then I scrolled down and saw your comment :) @Pankaj: as others have said already, you shouldn't be checking in code which doesn't compile. Even if they hate each other enough not to communicate, as soon as user 2 pulls user 1's changes and realises the build is broken, he needs to fix it before checking in his own. I know, I know: "if only", eh? :)
shambulator
+2  A: 

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?

0xA3
A: 

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

Anton Gogolev
+4  A: 

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.

decyclone
A: 

You can't declare the same member multiple times in multiple parts of a partial class (in your case it's ABC.GetAge()).

BoltClock
A: 

They won't merge: compile time error. They might merge in your case if you accidentally put them in different namespaces.

Mau
A: 

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.

luckyluke
+1  A: 

Partial classes are merged while 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 method with the same name and signature (See GateAge method).

Incognito