为什么OUT参数不允许隐式转换? [英] Why isn't implicit casting allowed on OUT parameters?

查看:84
本文介绍了为什么OUT参数不允许隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向C#语言专家提问:为什么这段代码无效?


static void Foo(out string s)

{

s =" test";

}


static void Main()

{

对象s; // ***接受任何输出类型!

Foo(out s);

}


如果s未初始化为Foo的开头,它有什么区别

什么类型的out值最终会分配给,当

函数最终退出?

谢谢!

解决方案

您好Yurik,


这里的重点是的,你不能将对象传递给明显的方法

期望字符串。

-


SevDer
< a rel =nofollowhref =http://www.sevder.comtarget =_ blank> http://www.sevder.com

一个新的.NET .NET开发人员的来源

" Yurik" < ZA ***** @ gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

C#语言专家的一个问题:为什么这段代码没有效?

static void Foo(out string s)
{
s ="测试;
}

static void Main()
{
对象s; // ***接受任何输出类型!
Foo(out s);
}
如果在Foo开头没有初始化s,它有什么区别

函数最终退出时,将最终赋值的类型设为什么类型?

谢谢!



< blockquote>嗨Yurik,


我不是语言专家,但我认为它与编译器需要知道的事实有关。

输出参数是一个值

(基于堆栈)类型,或者一个引用(基于堆)类型。


这是来自C#规范:


< quote>

1.输出参数不会创建新的存储位置。 2

相反,输出参数表示与作为函数成员调用中的参数给出的

变量相同的存储位置。 3

因此,输出参数的值始终与

基础变量相同。

< / quote>


如果将输出参数声明为对象类型,编译器

没有足够的参数存储语义信息。


问候,


Bennie Haelen


Yurik写道:

向C#语言专家提问:为什么这段代码没有效?
static void Foo(out string s)
{
s =" test";
}

static void Main()
{ Foo(out s);
}
如果在Foo开头没有初始化s,它有什么区别

函数最终退出时,将最终赋值的类型设为什么类型?

谢谢!



< blockquote>嗨Yurik,


我不是语言专家,但我认为它与编译器需要知道的事实有关。

输出参数是一个值

(基于堆栈)类型,或者一个引用(基于堆)类型。


这是来自C#规范:


< quote>

1.输出参数不会创建新的存储位置。 2

相反,输出参数表示与作为函数成员调用中的参数给出的

变量相同的存储位置。 3

因此,输出参数的值始终与

基础变量相同。

< / quote>


如果将输出参数声明为对象类型,编译器

没有足够的参数存储语义信息。


问候,


Bennie Haelen


Yurik写道:

向C#语言专家提问:为什么这段代码没有效?
static void Foo(out string s)
{
s =" test";
}

static void Main()
{ Foo(out s);
}
如果在Foo开头没有初始化s,它有什么区别

函数最终退出时,将最终赋值的类型设为什么类型?

谢谢!



A question to the C# language experts: Why isn''t this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!

解决方案

Hi Yurik,

The point here is, you cannot pass object into the method that explicity
expects string.
--

SevDer
http://www.sevder.com
A new .NET Source For .NET Developers
"Yurik" <za*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

A question to the C# language experts: Why isn''t this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!



Hi Yurik,

I am not a language expert, but I think it has to do with the fact that
the compiler needs to know if the output parameter is a value
(stack-based) type, or a reference (heap-based) type.

This is from the C# specification:

<quote>
1. An output parameter does not create a new storage location. 2
Instead, an output parameter represents the same storage location as the
variable given as the argument in the function member invocation. 3
Thus, the value of an output parameter is always the same as the
underlying variable.
</quote>

If you declare your output parameter as an "object" type, the compiler
does not have enough information on the storage semantics of the parameter.

Regards,

Bennie Haelen

Yurik wrote:

A question to the C# language experts: Why isn''t this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!



Hi Yurik,

I am not a language expert, but I think it has to do with the fact that
the compiler needs to know if the output parameter is a value
(stack-based) type, or a reference (heap-based) type.

This is from the C# specification:

<quote>
1. An output parameter does not create a new storage location. 2
Instead, an output parameter represents the same storage location as the
variable given as the argument in the function member invocation. 3
Thus, the value of an output parameter is always the same as the
underlying variable.
</quote>

If you declare your output parameter as an "object" type, the compiler
does not have enough information on the storage semantics of the parameter.

Regards,

Bennie Haelen

Yurik wrote:

A question to the C# language experts: Why isn''t this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!



这篇关于为什么OUT参数不允许隐式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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