REF? [英] ref?

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

问题描述

嘿伙计


我需要澄清一下。


ref关键字表示你传入对象或变量的引用,

就像&和*的c ++?


我不明白的是,在c#中,它总是使用我想的参考,

或者它只是参考在课堂上?


如果我这样做


someobj so = new someObj(); //所以分配内存


so.someVar = 10; // somevar设置为10

someobj so2 = so; //他们现在彼此相等,so2正在使用ref我们做了

没有克隆它


so2.someVar = 20; //当我们这样做时,so.someVar也变成了20

Console.Writeline(so.someVar.ToString());


//会打印出20


不是吗?如果是这种情况,为什么ref关键字和什么时候需要它?



Hey guys

I need some clarification.

The ref keyword means that you pass in a reference to an object or variable,
is it like the & and * of c++?

What i don''t understand is, in c#, it always uses reference by i thought,
or is it only refs on classes?

so if i do

someobj so = new someObj(); //so memory allocated

so.someVar = 10; //somevar set to 10

someobj so2 = so; //they now equal each other, so2 is using a ref as we did
not clone it

so2.someVar = 20; //when we do this, so.someVar also becomes 20

Console.Writeline(so.someVar.ToString());

//would print out 20

Isn''t that right? If that is the case, why the ref keyword and when would
you need it?



推荐答案

我不明白的是,在c#中,它总是使用引用我认为,
What i don''t understand is, in c#, it always uses reference by i thought,

或者它只是引用班?
or is it only refs on classes?



内存分配在堆栈和堆上;这转化为

原始的价值类型和引用类型。整数或布尔值

按值存储,而类的实例是使用

new关键字创建的,然后通过引用进行管理,在代码中你有

变量既是对内存中相同对象的引用,又是
当有人做出改变时,另一个会看到它。


但是,直观地说,结构是价值类型。所以,如果你使用x

= y;在您的代码中,您正在克隆值,而不是使用两个指向

的位置。

Memory is allocated on the stack and the heap; this translates to
primitive "value types" and "reference types." An integer or boolean
is stored by value, whereas instances of classes are created with the
new keyword and then managed by reference, where in your code you have
to variables that are both references to the same object in memory, and
when one makes a change, the other will see it.

Counter-intuitively, though, structs are value types. So if you use "x
= y;" in your code you''re cloning the values, not using two pointers to
the same location.


''ref ''和''out''与方法参数一起使用。这里有一些例子

应该可以理解...


void Test1(int val)

{

val ++;

}

void Test2(ref int val)

{

val ++;

}

void Test3(out int val)

{

val ++;

}

int myInt = 5;


Test1(myInt);

// myInt仍然等于5。

//程序修改了自己的数据副本。


Test2(ref myInt); //'ref''在这里是必要的。

// myInt现在等于6.

//程序被赋予对''myInt'的引用直接var。


Test3(out myInt); //''out''在这里也是必须的。

// myInt现在等于1.

//程序给了一个全新的参考,但' 'myInt'用

替换为新的参考。

Daniel写道:
''ref'' and ''out'' are used with method params. Here are some examples
that should get the idea across..

void Test1(int val)
{
val++;
}
void Test2(ref int val)
{
val++;
}
void Test3(out int val)
{
val++;
}
int myInt = 5;

Test1(myInt);
// myInt still equals 5.
// The procedure modified its own copy of the data.

Test2(ref myInt); // The ''ref'' is nessisary here.
// myInt now equals 6.
// The procedure was given a reference to the ''myInt'' var directly.

Test3(out myInt); // The ''out'' is nessisary here as well.
// myInt now equals 1.
// The procedure was given a fresh new reference, but ''myInt'' was
replaced with that new reference.
Daniel wrote:

嘿伙计们/>

我需要澄清一下。


ref关键字表示你传入对象或变量的引用,

就像&和*的c ++?


我不明白的是,在c#中,它总是使用我想的参考,

或者它只是参考在课堂上?


如果我这样做


someobj so = new someObj(); //所以分配内存


so.someVar = 10; // somevar设置为10

someobj so2 = so; //他们现在彼此相等,so2正在使用ref我们做了

没有克隆它


so2.someVar = 20; //当我们这样做时,so.someVar也变成了20

Console.Writeline(so.someVar.ToString());


//会打印出20


不是吗?如果是这种情况,为什么ref关键字以及什么时候需要它?
Hey guys

I need some clarification.

The ref keyword means that you pass in a reference to an object or variable,
is it like the & and * of c++?

What i don''t understand is, in c#, it always uses reference by i thought,
or is it only refs on classes?

so if i do

someobj so = new someObj(); //so memory allocated

so.someVar = 10; //somevar set to 10

someobj so2 = so; //they now equal each other, so2 is using a ref as we did
not clone it

so2.someVar = 20; //when we do this, so.someVar also becomes 20

Console.Writeline(so.someVar.ToString());

//would print out 20

Isn''t that right? If that is the case, why the ref keyword and when would
you need it?





ref关键字用于通过引用传递方法参数并具有

与财产或油田分配无关。这是可观察的行为

对于值类型和引用类型是相同的,但是,它的原因

有点不那么重要。


在下面的代码中,localVar,一个System.Boolean,是一个值类型。通过引用传递

就像在TestRef方法中传入一个指向

变量位置的指针。


void TestRef()

{

bool localStruct = false;


UpdateMyLocalVariable(ref localStruct);


Console.WriteLine(localStruct); // true

}


void UpdateMyLocalVariable(ref bool localStruct)

{

//更新对localStruct的引用

//这样*它是*一个新值

localStruct = true;

}


在以下代码中,更新了对象引用。注意,它是由引用传递的

引用,而不是它通常指向的引用,

这是对象在内存中的实际位置。


void TestRefObject()

{

object localObject = new object();


UpdateMyLocalVariable(ref localObject);


Console.WriteLine(localObject); //"一个字符串对象" ;;

}


void UpdateMyLocalVariable(ref object localObject)

{

//更新对localObject的引用

// so *它指向*一个字符串(这是一个对象)

localObject ="一个字符串对象" ;;

}

当您通过引用传递一个值时,您传入的是

值的引用堆栈或堆,而不是副本

传统上为值类型发送。


当你通过引用传递一个对象时你是传入引用本身,

而不是它指向的对象。传统上,当您传递一个对象时,

您正在传递堆上对象的位置。 By-ref,你是
而不是传递对堆上位置的引用。


-

Dave Sexton
http://davesexton.com/blog


" Daniel" < da ***** @ vestryonline.comwrote in message

news:ui ************** @ TK2MSFTNGP02.phx.gbl ...
Hi,

The ref keyword is used for passing method arguments by reference and has
nothing to do with property or field assignment. It''s observable behavior
is the same for value-types and reference types, however, the reasons for it
are somewhat less trivial.

In the code below, "localVar", a System.Boolean, is a value type. Passing
it in by-reference is like passing in a pointer to the location of the
variable within the TestRef method.

void TestRef()
{
bool localStruct = false;

UpdateMyLocalVariable(ref localStruct);

Console.WriteLine(localStruct); // true
}

void UpdateMyLocalVariable(ref bool localStruct)
{
// update the reference to localStruct
// so that *it is* a new value
localStruct = true;
}

In the following code an object reference is updated. Note, it''s the
reference that is being passed by-reference, not what it normally points to,
which is the actual location of the object in memory.

void TestRefObject()
{
object localObject= new object();

UpdateMyLocalVariable(ref localObject);

Console.WriteLine(localObject); // "A string object";
}

void UpdateMyLocalVariable(ref object localObject)
{
// update the reference to localObject
// so *it points to* a string (which is an object)
localObject = "A string object";
}
When you pass a value by-reference you''re passing in a reference to the
value where it exists on the stack or the heap, not a copy as is
traditionally sent for value-types.

When you pass an object by-reference you''re passing in the reference itself,
not what it points to - the object. Traditionally, when you pass an object,
you''re passing in the location of the object on the heap. By-ref, you''re
passing in the reference to the location on the heap instead.

--
Dave Sexton
http://davesexton.com/blog

"Daniel" <Da*****@vestryonline.comwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...

嘿伙计


我需要澄清一下。


ref关键字意味着你传递一个对象或

变量的引用,它是否像&和*的c ++?


我不明白的是,在c#中,它总是使用我想的参考,

或者它只是参考在课堂上?


如果我这样做


someobj so = new someObj(); //所以分配内存


so.someVar = 10; // somevar设置为10

someobj so2 = so; //他们现在彼此相等,so2正在使用参考,因为我们

没有克隆它


so2.someVar = 20; //当我们这样做时,so.someVar也变成了20

Console.Writeline(so.someVar.ToString());


//会打印出20


不是吗?如果是这样的话,为什么ref关键字和什么时候需要它?


Hey guys

I need some clarification.

The ref keyword means that you pass in a reference to an object or
variable, is it like the & and * of c++?

What i don''t understand is, in c#, it always uses reference by i thought,
or is it only refs on classes?

so if i do

someobj so = new someObj(); //so memory allocated

so.someVar = 10; //somevar set to 10

someobj so2 = so; //they now equal each other, so2 is using a ref as we
did not clone it

so2.someVar = 20; //when we do this, so.someVar also becomes 20

Console.Writeline(so.someVar.ToString());

//would print out 20

Isn''t that right? If that is the case, why the ref keyword and when would
you need it?




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

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