基本范围问题 [英] fundamental scope question

查看:84
本文介绍了基本范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个白痴问这个但是这里有:我理解''概念''

范围并按价值和/或参考传递数据但是我很困惑

一些细节。


类示例{

int i; //我的全局变量


private void method1(int myInt){

int x; // local var

等...

}


private void method2(int myInt){

int z; // local var

等...

}

}


所以我得到了全局var i可以通过上面的类中的任何方法访问

示例,基于其声明的位置,例如,var x是方法1的本地和/或
只能从method1访问。除此之外,在一个

方法中,您可以在条件IF

块或循环中获得更多本地化变量。我得到传递数据byval传递传递的

变量数据的''副本',并且传递byref传递实际内存位置

传递的变量。因此,例如,如果我的全局变量i被传递给示例中的任何一个方法并被修改,那么实际的全局变量将直接修改。 ..如果它是通过byval传递的,并且

在全局i var保持不变的方法中被修改。


现在这里是我感到困惑的地方:说方法1中的var x是一个参考

类型,如''object''。它在method1中声明,因此scope只是method1。

如果从method1调用method2并传入x,该怎么办。在method2中使用的x

是方法1中的x相同?如果我希望它是x

必须在那些我的示例全局变量
的方法之外声明?


边栏问题:c#是按值传递参数还是按参考传递

默认值? (我认为它的价值除了我记得有什么特别关于

字符串?)

I feel like an idiot asking this but here goes: I understand the ''concept''
of scope and passing data by value and/or by reference but I am confused on
some specifics.

class example{
int i; //my global var

private void method1(int myInt){
int x; //local var
etc...
}

private void method2(int myInt){
int z; //local var
etc...
}
}

so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within a
method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a ''copy'' of the passed
variable''s data and that passing byref passes the actual memory location of
the passed variable. So, for instance, if my global var i was passed byref
to either of those methods in the example and modified, that actual global
var i would be modified directly... whereas if it was passed byval and
modified within the method that the global i var would remain untouched.

now here is where I get confused: say the var x in method1 was a reference
type like ''object''. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var i
is?

side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)

推荐答案

David,


嗯,我不是全球性的,因为它的范围是

类示例的实例。只有班上的其他成员才能看到我。


这里有一个例子可以帮助你理解按值和参考传递引用之间的差异




公共类MyClass

{

public string Name = null;

}


public void DoSomething(MyClass myClass)

{

// myClass按值传递。在这种情况下,它是通过值传递

的引用。

//您可以对实例的成员进行更改,但不能更改

引用本身。所以以下

//将更改名称。

myClass.Name =" Nicholas Paldino";


//但是,这不会改变myClass指向的内容。对于其余的

方法,myClass

//将指向新实例。

myClass = new MyClass();

}


public void DoSomething2(ref MyClass myClass)

{

//这里,myClass可以改变。

myClass = new MyClass();

}


public void Test()

{

//创建一个MyClass实例。

MyClass mc = new MyClass();


//设置名称。

mc.Name =" David";


//存储参考文献以供日后比较。

MyClass oldMc = mc;


//调用第一个DoSomething方法。

DoSomething(mc);


//此时,mc.Name将为Nicholas Paldino。

Console.WriteLine(" mc.Name:{0},mc.Name);


//引用没有改变。

Console.WriteLine(" mc == oldMc:{0}",Object.ReferenceEquals(mc,

oldMc) );


//现在调用DoSomething2。

DoSomething2(ref mc);


//参考文献是不一样的。

Console.WriteLine(调用DoSomething2后的mc == oldMc:{0}",

Object.ReferenceEquals(mc,oldMc) );

}


希望这会有所帮助。

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
David,

Well, i isn''t global, in the sense that it is scoped to instances of the
class example. Only other members of the class can see i.

Here is an example which should help you understand the difference
between passing references by value, and by reference.

public class MyClass
{
public string Name = null;
}

public void DoSomething(MyClass myClass)
{
// myClass is passed by value. In this case, it is the reference that
is passed by value.
// You can make changes to members of the instance, but not the
reference itself. So the following
// will change the name.
myClass.Name = "Nicholas Paldino";

// However, this will not change what myClass points to. For the rest
of the method though, myClass
// will point to the new instance.
myClass = new MyClass();
}

public void DoSomething2(ref MyClass myClass)
{
// Here, myClass can be changed.
myClass = new MyClass();
}

public void Test()
{
// Create an instance of MyClass.
MyClass mc = new MyClass();

// Set the name.
mc.Name = "David";

// Store a reference for comparison later.
MyClass oldMc = mc;

// Call the first DoSomething method.
DoSomething(mc);

// At this point, mc.Name will be "Nicholas Paldino".
Console.WriteLine("mc.Name: {0}, mc.Name);

// The reference has not changed.
Console.WriteLine("mc == oldMc: {0}", Object.ReferenceEquals(mc,
oldMc));

// Now call DoSomething2.
DoSomething2(ref mc);

// The references are not the same.
Console.WriteLine("mc == oldMc after call to DoSomething2: {0}",
Object.ReferenceEquals(mc, oldMc));
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

>我觉得这是一个白痴问这个但是这里有:我理解范围的''概念''
按价值传递数据和/或者通过引用但我对某些细节感到困惑。


类示例{

int i; //我的全局变量


private void method1(int myInt){

int x; // local var

等...

}


private void method2(int myInt){

int z; // local var

等...

}

}


所以我得到了全局var i可以通过上面的类中的任何方法访问

示例,基于其声明的位置,例如,var x是方法1的本地和/或
只能从method1访问。除此之外,在一个方法中,你可以在条件IF

块或循环中获得更多的本地化变量。我得到传递数据byval传递

传递变量'的数据的''副本',并且传递byref传递实际内存

传递变量的位置。因此,举例来说,如果我的全局var i是通过参数传递给示例中的任何一个方法并进行了修改,那么

将直接修改实际的全局变量。 ..如果它被传递了

byval并在全局i var保持的方法中修改了

未触及。


现在这里我感到困惑:说方法1中的var x是一个参考

类似''object''。它在method1中声明,因此scope只是method1。

如果从method1调用method2并传入x,该怎么办。在method2中使用的x

是方法1中的x相同?如果我希望它是x

必须在我的示例全局变量

i的方法之外声明?


侧栏问题:c#是按值传递参数还是按参考传递

默认值? (我认为它的价值除了我记得一些特别的关于

字符串?)

>I feel like an idiot asking this but here goes: I understand the ''concept''
of scope and passing data by value and/or by reference but I am confused on
some specifics.

class example{
int i; //my global var

private void method1(int myInt){
int x; //local var
etc...
}

private void method2(int myInt){
int z; //local var
etc...
}
}

so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within
a method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a ''copy'' of the
passed variable''s data and that passing byref passes the actual memory
location of the passed variable. So, for instance, if my global var i was
passed byref to either of those methods in the example and modified, that
actual global var i would be modified directly... whereas if it was passed
byval and modified within the method that the global i var would remain
untouched.

now here is where I get confused: say the var x in method1 was a reference
type like ''object''. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var
i is?

side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)



Pl看一下C#语言参考中OUT关键词的概念。


不要混淆用C#语言中VB的ByVal和ByRef关键词。


对于VB的ByRef,你所要做的就是使用C#中的OUT关键字,如下所述


int y;

public int DoSome(int x,out int y)

{

y = 10; //将值移出范围

x = 11; //将破坏此方法中的值

}


HTH

-

每个事情是完美的,只要你分享!!!


别忘了评价帖子

" David"写道:
Pl give a look at the concept of OUT Key word in C# Language Ref.

Dont get confuse with the VB''s ByVal and ByRef keywords in C# Language.

For ByRef of VB, all you have do is use OUT key word in C# as mentioned below

int y;
public int DoSome(int x, out int y)
{
y=10; // will send the value out of the scope
x=11; // will distroy the value inside this method
}

HTH
--
Every thing is perfect, as long as you share!!!

Don''''t forget to rate the post
"David" wrote:

我觉得这是一个白痴问这个但是这里有:我理解''概念''

的范围和传递数据通过价值和/或参考,但我很困惑

一些具体细节。


类示例{

int i; //我的全局变量


private void method1(int myInt){

int x; // local var

等...

}


private void method2(int myInt){

int z; // local var

等...

}

}


所以我得到了全局var i可以通过上面的类中的任何方法访问

示例,基于其声明的位置,例如,var x是方法1的本地和/或
只能从method1访问。除此之外,在一个

方法中,您可以在条件IF

块或循环中获得更多本地化变量。我得到传递数据byval传递传递的

变量数据的''副本',并且传递byref传递实际内存位置

传递的变量。因此,例如,如果我的全局变量i被传递给示例中的任何一个方法并被修改,那么实际的全局变量将直接修改。 ..如果它是通过byval传递的,并且

在全局i var保持不变的方法中被修改。


现在这里是我感到困惑的地方:说方法1中的var x是一个参考

类型,如''object''。它在method1中声明,因此scope只是method1。

如果从method1调用method2并传入x,该怎么办。在method2中使用的x

是方法1中的x相同?如果我希望它是x

必须在那些我的示例全局变量
的方法之外声明?


边栏问题:c#是按值传递参数还是按参考传递

默认值? (我认为它的价值除了我记得有什么特别的关于

字符串?)
I feel like an idiot asking this but here goes: I understand the ''concept''
of scope and passing data by value and/or by reference but I am confused on
some specifics.

class example{
int i; //my global var

private void method1(int myInt){
int x; //local var
etc...
}

private void method2(int myInt){
int z; //local var
etc...
}
}

so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within a
method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a ''copy'' of the passed
variable''s data and that passing byref passes the actual memory location of
the passed variable. So, for instance, if my global var i was passed byref
to either of those methods in the example and modified, that actual global
var i would be modified directly... whereas if it was passed byval and
modified within the method that the global i var would remain untouched.

now here is where I get confused: say the var x in method1 was a reference
type like ''object''. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var i
is?

side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)


5月10日晚上7:14 ,大卫 < nos ... @nospam.comwrote:
On May 10, 7:14 pm, "David" <nos...@nospam.comwrote:

我觉得这是一个白痴问这个但是这里有:我理解''概念''

的范围和按价值和/或参考传递数据但是我很困惑

一些细节。


类示例{

int i; //我的全局变量


private void method1(int myInt){

int x; // local var

等...

}


private void method2(int myInt){

int z; // local var

等...

}


}


所以我得到全局var i可以通过上面的类中的任何方法访问

示例,基于它声明的位置,例如,var x是

方法1的本地,只能从method1访问。除此之外,在一个

方法中,您可以在条件IF

块或循环中获得更多本地化变量。我得到传递数据byval传递传递的

变量数据的''副本',并且传递byref传递实际内存位置

传递的变量。因此,例如,如果我的全局变量i被传递给示例中的任何一个方法并被修改,那么实际的全局变量将直接修改。 ..如果它是通过byval传递的,并且

在全局i var保持不变的方法中被修改。


现在这里是我感到困惑的地方:说方法1中的var x是一个参考

类型,如''object''。它在method1中声明,因此scope只是method1。

如果从method1调用method2并传入x,该怎么办。在method2中使用的x

是方法1中的x相同?如果我希望它是x

必须在那些我的示例全局变量
的方法之外声明?


边栏问题:c#是按值传递参数还是按参考传递

默认值? (我认为它的价值除了我记得一些特别的关于

字符串?)
I feel like an idiot asking this but here goes: I understand the ''concept''
of scope and passing data by value and/or by reference but I am confused on
some specifics.

class example{
int i; //my global var

private void method1(int myInt){
int x; //local var
etc...
}

private void method2(int myInt){
int z; //local var
etc...
}

}

so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within a
method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a ''copy'' of the passed
variable''s data and that passing byref passes the actual memory location of
the passed variable. So, for instance, if my global var i was passed byref
to either of those methods in the example and modified, that actual global
var i would be modified directly... whereas if it was passed byval and
modified within the method that the global i var would remain untouched.

now here is where I get confused: say the var x in method1 was a reference
type like ''object''. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var i
is?

side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)



David,


我将从最后开始:所有原始类型都按值传递(如

int,decimal等)。字符串是一个特例,因为它是通过

引用传递但是是不可变的所以你不能改变它的内容,只有它指向的

位置to(你可以做s =" m")。


因为你的示例函数正在获取int参数,所以每个

传递的参数都是通过价值。

您可以使用''ref''关键字通过引用传递基元:

//

void method1(ref int i )

{

++我;

}


希望这有帮助。

Moty

David,

I''ll start from the end: All primitive types are passed by value (like
int, decimal etc). string is a special case since it''s passed by
reference but is immutable so you can''t change it''s content, only the
location it points to (you can do s="m").

Since your example functions are getting int parameters, each of the
passed parameter is passed by value.
You can use the ''ref'' keyword to pass primitives by reference:
//
void method1(ref int i)
{
++i;
}

Hope this helps.
Moty


这篇关于基本范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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