拆分/合并局部方法 [英] Splitting/Combining Partial Methods

查看:162
本文介绍了拆分/合并局部方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解部分的方法可以用来分割为多个文件的方法的定义。我很好奇,但如果它是允许有一个方法,每个定义在多个文件中包含的代码?

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?

例如,说我有一个方法私人部分无效美孚()。比方说,我有它在文件中的规定和文件B.既可以实例都包含在方法代码,或只是一个或其他?我想我会感到惊讶,如果这被允许。

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.

推荐答案

没有,你不能。如果你可以,当你调用美孚(),该代码将首先执行?如果这两个版本是与处理(修改)全局状态,这将是非常重要的是知道的执行顺序。

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.

作为飘忽不定的行为与这种可能性出现的潜在的污秽简单的例子,假设你可以,假设你有下面的代码:

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);
    }
}

当你调用 NastyMethod( ),将其打印什么价值?无感!

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

现在另一个奇怪的问题。做什么用的参数?和返回值?

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;
    }
}



而现在,人们怎么可能给一个意义这个代码?我们应该呼吁 HasRealSolution后考虑哪些回报(1,2,1)?它是如何以往任何时候都可以想象到有2个不同的,同时,返回值 * 为一个单一的方法?我们不处理非确定有限状态自动机

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!

要那些谁也强加在这个假设的世界我不存在的部分方法应无效,更换收益 s的设置值对一些私人领域的类。效果几乎是一样的。

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

这篇关于拆分/合并局部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆