参考类型和值类型 [英] Reference Types and Value Types
问题描述
这是一个非常基本的问题,但我真的很想知道,所以
感谢你提供的任何帮助或指示(比如我会给你什么) b $ b google for; o)
假设:
< code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write(myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType传递类型)
{
Console.Write(myType.ToString())
返回true;
}
< / code>
我的问题:由于myType是一个引用类型,因此只在内存中创建了这个
对象的一个实例,对吧?领导软件架构师
这里(C ++,没有C#经验)告诉我关于通过引用传递 -
这是由C#中的引用类型隐式完成的吗?另外,我可以通过
值吗?我为什么要这样?
所有这些问题都是在最少量的b
开销的支持下提出的。我主要关心的是良好的编码实践(和学习)。
谢谢,
-daniel
This is a pretty basic-level question, but I''d really like to know, so
thanks for any help or pointers you can provide (like what I would
google for ;o)
Suppose:
<code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write(myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType passedType)
{
Console.Write(myType.ToString())
return true;
}
</code>
My question: since myType is a reference type, only one instance of this
object is ever created in memory, right? The Lead Software Architect
here (C++, no C# experience) was telling me about passing by reference--
is this implicitly done by reference types in C#? Also, could I pass by
value? Why would I want to?
All these questions are asked under the auspices of least amount of
overhead. My primary concern is good coding practices (and learning).
Thank you,
-daniel
推荐答案
daniel,
据我所知,你不能通过参考。如果你想要
来按值键入。它通过引用自动传递。如果你有一个值类型和
你想要保持它在方法调用中的价值,你需要通过
参考传递它。
示例应打印出12:
private void Test()
{
int index = 0 ;
Test2(参考索引);
Console.Write(index.ToString());
}
>
private void Test2(ref index)
{
index = 12;
}
-
Lateralus [MCAD]
" daniel" <我们** @ example.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
daniel,
To my knowledge you can''t pass a "reference" type by value if you wanted
to. It is automatically passed by reference. If you have a value type and
you want to maintain it''s value across method calls, you need to pass it by
reference.
example should print out 12:
private void Test()
{
int index = 0;
Test2(ref index);
Console.Write(index.ToString());
}
private void Test2(ref index)
{
index = 12;
}
--
Lateralus [MCAD]
"daniel" <us**@example.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
这是一个非常基本的问题,但我真的很想知道,所以
感谢您提供的任何帮助或指示(比如我将谷歌的用途; o)
<假设:
< code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write (myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType传递类型)
{
Console.Write(myType) .ToString())
返回true;
}
< / code>
我的问题:由于myType是一个引用类型,只有一个这个
对象的实例是在内存中创建的,对吗?这里的首席软件架构师
(C ++,没有C#经验)告诉我关于通过引用传递 -
这是由C#中的引用类型隐式完成的吗?另外,我可以通过
值吗?我为什么要这样做?
所有这些问题都是在最少量的开销的支持下提出来的。我主要关心的是良好的编码实践(和学习)。
谢谢你,
-daniel
This is a pretty basic-level question, but I''d really like to know, so
thanks for any help or pointers you can provide (like what I would google
for ;o)
Suppose:
<code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write(myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType passedType)
{
Console.Write(myType.ToString())
return true;
}
</code>
My question: since myType is a reference type, only one instance of this
object is ever created in memory, right? The Lead Software Architect here
(C++, no C# experience) was telling me about passing by reference--
is this implicitly done by reference types in C#? Also, could I pass by
value? Why would I want to?
All these questions are asked under the auspices of least amount of
overhead. My primary concern is good coding practices (and learning).
Thank you,
-daniel
啊,谢谢Lateralus的快速回复。我有一个快速跟进
的问题,但是:
如果引用类型总是通过引用传递,那么(虽然愚蠢
和凌乱):
< code>
//这次有返回类型!
void func1( )
{
SomeType myType = new myType();
func2(myType);
}
void func2(SomeType myType)
{
func3(mytype);
}
void func3(SomeType myType)
{
Console.Write(myType.ToString());
}
< / code>
根本不会导致大量增加的开销(其他
比堆栈中的变量),因为只有一个每个创建的对象。
谢谢,
-daniel
Lateralus [MCAD]写道:
Ahh, thank you Lateralus for the quick reply. I have a quick follow up
question, though:
If a reference type is always passed by reference, then (while stupid
and messy):
<code>
// with a return type this time!
void func1()
{
SomeType myType = new myType();
func2(myType);
}
void func2(SomeType myType)
{
func3(mytype);
}
void func3(SomeType myType)
{
Console.Write(myType.ToString());
}
</code>
will not cause a substantial amount of increased overhead at all (other
than variables on the stack), because only one object is every created.
Thanks,
-daniel
Lateralus [MCAD] wrote:
daniel,
据我所知,你不能通过参考。如果你想要
,请按值键入。它通过引用自动传递。如果你有一个值类型并且
你希望在方法调用中保持它的值,你需要通过
引用传递它。
示例应该打印出12:
private void Test()
{index = 0;
Test2(ref index);
Console.Write(index.ToString() );
}
private void Test2(ref index)
{
index = 12;
}
daniel,
To my knowledge you can''t pass a "reference" type by value if you wanted
to. It is automatically passed by reference. If you have a value type and
you want to maintain it''s value across method calls, you need to pass it by
reference.
example should print out 12:
private void Test()
{
int index = 0;
Test2(ref index);
Console.Write(index.ToString());
}
private void Test2(ref index)
{
index = 12;
}
Daniel,
有关参考类型和信息的信息价值类型以及价值传递
&通过参考传递参见:
http:/ /www.yoda.arachsys.com/csharp/parameters.html
希望这有帮助
Jay
" daniel" <我们** @ example.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
Daniel,
For information on reference types & value types along with passing by value
& passing by reference see:
http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay
"daniel" <us**@example.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
这是一个非常基本的问题,但我真的很想知道,所以
感谢您提供的任何帮助或指示(比如我将谷歌的用途; o)
<假设:
< code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write (myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType传递类型)
{
Console.Write(myType) .ToString())
返回true;
}
< / code>
我的问题:由于myType是一个引用类型,只有一个这个
对象的实例是在内存中创建的,对吗?这里的首席软件架构师
(C ++,没有C#经验)告诉我关于通过引用传递 -
这是由C#中的引用类型隐式完成的吗?另外,我可以通过
值吗?我为什么要这样做?
所有这些问题都是在最少量的开销的支持下提出来的。我主要关心的是良好的编码实践(和学习)。
谢谢你,
-daniel
This is a pretty basic-level question, but I''d really like to know, so
thanks for any help or pointers you can provide (like what I would google
for ;o)
Suppose:
<code>
myFunc()
{
SomeType myType = new SomeType;
Console.Write(myType.MyProperty);
bool whatever = myOtherFunc(myType);
}
myOtherfunc(SomeType passedType)
{
Console.Write(myType.ToString())
return true;
}
</code>
My question: since myType is a reference type, only one instance of this
object is ever created in memory, right? The Lead Software Architect here
(C++, no C# experience) was telling me about passing by reference--
is this implicitly done by reference types in C#? Also, could I pass by
value? Why would I want to?
All these questions are asked under the auspices of least amount of
overhead. My primary concern is good coding practices (and learning).
Thank you,
-daniel
这篇关于参考类型和值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!