C#从另一个方法引用方法变量
有时我想在方法B中使用很多方法作为变量。 通常,将所有变量传递给这个方法是一件非常痛苦的事情,特别是如果我不得不多次这样做(但不能简单地复制粘贴,因为有些事情会改变)或者我只是太懒了 有没有像“内在方法”这样的东西?或者一些简单的概念来处理这个问题 我想做的是:C#从另一个方法引用方法变量,c#,methods,reference,C#,Methods,Reference,有时我想在方法B中使用很多方法作为变量。 通常,将所有变量传递给这个方法是一件非常痛苦的事情,特别是如果我不得不多次这样做(但不能简单地复制粘贴,因为有些事情会改变)或者我只是太懒了 有没有像“内在方法”这样的东西?或者一些简单的概念来处理这个问题 我想做的是: public void A() { int a = 4; string b = "Hello World"; B(ref vals); //Or lik
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;
...
}
如果他们都在同一个班
您可以将它们配置为类变量:
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;
...
}
}
埃尔斯韦 现在,您可以从方法B访问MyClassA
int myExValueA = MyClassA.a;
否则你必须把它们作为参数传递
希望这有帮助您可以创建一个保存参数的类,然后只传递该类的一个实例
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;
}
您可以在类头中声明静态变量,并根据自己的喜好使用它们,如果它们在同一个类中,则使用private,对于子类,则使用internal或public-else进行保护。或者在类中框选变量,如下所示:
public class Foo
{
public int A { get; set; }
public int B { get; set; }
public string C { get; set; }
}
如果传递的变量类型相同,则可以使用数据结构,如int[]或string[]或List或List,并在不使用ref的情况下传递它们,但这有一个缺点,即通常不会使用结构中的所有变量,因为类装箱变量也是如此。类似于以下内容:
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;
// ...
}
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();
// ...
}
肯定是无效的。我不认为这就是你在问题中的意图
你可以做一些类似于你要求使用的东西,但它们有点棘手。基本上,这样的东西是有效的(我不是坐在IDE前面,所以如果语法有点不正确,请原谅我):
当前不可能,您需要传递值。C#7将具有本地函数。创建一个对象,封装需要传递的所有字段。把那个东西传过来,谢谢。当然,这是最好的方法。根本没想过,不知道为什么。。。
Func<int> GetCounter() {
int count = 0;
// This will capture the count variable from its context
Func<int> method = () => ++count;
return method;
}
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();
// ...
}