Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting/Combining Partial Methods

Tags:

c#

I understand partial methods can be used to split the definition of a method across multiple files. I'm curious though if it's permissible to have each definition of a method across multiple files contain code?

For example, say I have a method private partial void Foo(). Let's say I've got it defined in file A and file B. Can both instances have code contained in the method, or just one or the other? I guess I'd be surprised if this were permitted.

like image 724
Randy Minder Avatar asked Jan 18 '10 18:01

Randy Minder


People also ask

What are partial methods?

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.

What are all the rules to follow when working with partial class definitions?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

Do partial classes have to be in same namespace?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.

Can partial class be inherited?

Inheritance cannot be applied to partial classes.


1 Answers

No, you can't. If you could, when you call Foo(), which code would execute first? If both versions were dealing with (and modifying) global state, it would be very important to know the order of execution.

Anyway, it makes no sense. So no, you can't.

Nasty example 1

As a simple example of the potential nastiness of the erratic behavior emerging from such a possibility, suppose you could, and suppose you had the following code:

public partial class MyClass {
    private int count = 0;
    public partial void NastyMethod() {
        count++;
    }
}

public partial class MyClass {
    public partial void NastyMethod() {
        Console.WriteLine(count);
    }
}

When you call NastyMethod(), what value would it print? No sense!

Nasty example 2

Now another strange problem. What to do with parameters? And return values?

public partial class MyClass2 {
    public partial bool HasRealSolution(double a, double b, double c) {
        var delta = b*b - 4*a*c;
        return delta >= 0;
    }
}

public partial class MyClass2 {
    public partial void HasRealSolution(double a, double b, double c) {
        return false;
    }
}

And now, how could one possibly give a sense to this code? Which return should we consider after calling HasRealSolution(1, 2, 1)? How is it ever conceivable to have 2 different, simultaneous, return values* for a single method? We are not dealing with nondeterministic finite automata!

To those who would impose that in this hypothetical world my inexistent partial methods should be void, replace the returns with setting a value on some private field to that class. The effect is almost the same.

* Note that what I'm talking here is not a single return value composed of two values, such as a Tuple. I'm talking here about TWO return values. (???)

like image 174
Bruno Reis Avatar answered Oct 06 '22 00:10

Bruno Reis