C#引用另一个方法的方法变量

时间:2016-09-21 14:52:40

标签: c# methods reference

有时我想在方法B中使用很多方法为变量。 通常将所有变量传递给此方法会非常痛苦,特别是如果我必须多次执行此操作(但不能简单地复制粘贴,因为有些事情会发生变化)或者只是为了懒惰。

是否有类似“内部方法”的东西?或者一些概念来轻松处理这个问题?

我想做什么:

    public void A()
    {
        int a = 4;
        string b = "Hello World";

        B(ref vals);

        //Or like so
        C(ref current);
    }

    public void B(ref AllValues)
    {
        a = 3;
        ...
    }

    public void C(ref MethodThatSharesAllValues method)
    {
        method.a = 3;
        ...
    }

4 个答案:

答案 0 :(得分:0)

如果他们都在同一个班级

您可以将它们配置为类变量:

public class MyClass{

    //set this as private/protected/public or nothing and you can also set a default value
    int a;

    public void A()
    {
        a = 4;
        string b = "Hello World";

        B();

        C();
    }

    public void B()
    {
        a = 3;
        ...
    }

    public void C()
    {
        a = 3;
        ...
    }
}

Elseway

public static class MyClassA{
  public static int a = 0;

  public static void MethodA(){
    this.a = 3;
  }
}

现在从方法B可以访问MyClassA

int myExValueA = MyClassA.a;

Elseway你必须把它们作为参数传递

希望这会有所帮助

答案 1 :(得分:0)

您可以创建一个包含参数的类,然后只传递此类的实例

public void metA(Parameters input)
{
    input.a = 5;
    input.c = "hello";
    metB(input);
}

public void metB(Parameters input)
{
    input.b = 10;
}

public class Parameters
{
    public int a;
    public int b;
    public string c;
}

答案 2 :(得分:0)

您可以在类标题中声明变量static并根据需要使用它们,如果属于同一类,则为private,为子类,内部或公共其他保护。或者将变量装入类中的变量:

public class Foo
{
    public int A { get; set; }
    public int B { get; set; }
    public string C { get; set; }
}

如果传递的变量是相同的类型,则可以使用数据结构,如int []或string []或List< int>或列表< string>并且在没有ref的情况下传递它们但是这样做的缺点是,通常你不会使用结构中的所有varibales,因为类拳击变体也是如此。

答案 3 :(得分:0)

如下所示:

public void foo() {
    int a = 10;
    // ...
}

public void foo_bar() {
    // "a" is not in scope for foo_bar, so this won't compile
    a = 20;
    // ...
}

绝对无效。我不认为这是你在问题中所驾驶的内容。

你可以做一些有些类似于你要求使用to this的内容,但它们使用起来有点棘手。基本上,这样的东西是有效的(我不会坐在IDE前面,所以请原谅我,如果语法稍微偏离):

Func<int> GetCounter() {
     int count = 0;

     // This will capture the count variable from its context
     Func<int> method = () => ++count;

     return method;
}

虽然有相当数量的语言(包括我猜的某些版本的C ++)有闭包(或类似的变体),但它们在各种语言中的确切工作方式似乎很少有一致性(例如关于&#34; count&#34;变量一旦被捕获就应该是不可变的)所以检查你使用的语言(在这种情况下,C#)的文档是很重要的,以确切了解它们是如何工作的。

就我提供的第一个代码示例而言,我怀疑这是您所询问的内容,但就像一个简短的题外话一样,您可能并不希望它仍然是允许的(并且我怀疑这不是你要问的语法/语义,因为它会很快导致意外/未定义的行为。例如:

  • 如果您在a中初始化了一个本地变量Foo(),并且在运行Foo_Bar()之前在Foo()中引用它,那么它的价值应该是什么是?
  • 如果你运行Foo()初始化变量,在Foo_Bar()中编辑变量,然后再次运行Foo(),如果你重新初始化变量或允许它保持Foo_Bar()设置的变量?
  • 方法调用完成后垃圾收集局部变量是否安全,或者是否可以再次引用它?

请参阅以下内容:

public class SomeObject
{
   public int SomeProperty { get; set; } = 6;
   // ...
}

public class SomeOtherObject
{
   // ..
}

void foo() {
    // What is the content of "a" before foo() runs?
    object a = new SomeObject();

    // Which "a" should this refer to - the one in foo() or the one in foo_bar()?
    // Also, is this a valid cast given that we haven't specified that SomeOtherObject can be cast to SomeObject?
    var b = (SomeObject)a;

    // If we run foo() again, should "b" retain the value of SetProperty or set it back to the initial value (6)?
    b.SetProperty = 10;

    // ...

    // Is it safe to garbage collect "a" at this point (or will foo_bar refer to it)?
}

void foo_bar() {
    object a = new SomeOtherObject();
    // ...
 }
相关问题