关于'out'参数的C#语言提案 [英] C# Language Proposal for 'out' Parameters

查看:63
本文介绍了关于'out'参数的C#语言提案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意

----

请使用固定宽度的字体来查看,例如Courier New。


问题

-------


当使用修饰符out传递参数时,有必要

写两个语句:


- 一个用于声明因接收值而变量;和

- 一个用于方法调用,其中变量作为参数传递。


示例:


static void Main()

{

int i;

if(TryGetValue(out i)){

Console.WriteLine(" Value is:{0}。,i);

}

}


这与从

方法返回值的传统方式不一致。


示例:


static void Main()

{

int i = GetValue();

Console.WriteLine(" Value is:{0}。" ,i);

}


解决方案

--------


这个问题的解决方案是允许

变量的声明出现在它作为

''传递的语句中out''参数。


示例:


static void Main()

{

if(TryGetValue(out int i)){

Console.WriteLi ne(Value is:{0}。,i);

}

}


福利

--------


(a)每次这个

技术时,方法主体可以从少一个语句中受益使用,提高可读性。


(b)C#变得更加一致,因为使用

传统方式返回值,并使用''out''参数,是统一的。再次,

这促进了可读性。


例如:


//传统方式

static void Main()

{

int i = GetANumber();

DoSomethingWith(i);

}


//使用''out''参数,使用上述语法

static void Main()

{

if(TryGetANumber(out int i)){

DoSomethingWith(i);

}

}


结束

---


评论?


问候,

CS学习者

Note
----

Please use a fixed-width font to view this, such as Courier New.

Problem
-------

When passing a parameter with the modifier ''out'', it is necessary to
write two statements:

- one for the declaration of the variable due to receive the value; and
- one for the method call, where the variable is passed as a parameter.

Example:

static void Main()
{
int i;
if (TryGetValue(out i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

This is inconsistent with the traditional way of returning values from a
method.

Example:

static void Main()
{
int i = GetValue();
Console.WriteLine("Value is: {0}.", i);
}

Solution
--------

The solution to this problem would be to allow the declaration of the
variable to appear within the statment where it''s being passed as an
''out'' parameter.

Example:

static void Main()
{
if (TryGetValue(out int i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

Benefits
--------

(a) Method bodies can benefit from one less statement for each time this
technique is used, promoting readability.

(b) C# becomes more consistent since returning values using both the
traditional way, and using ''out'' parameters, is uniform. Again,
this promotes readability.

Example:

// traditional way
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}

// using an ''out'' parameter, with the aforementioned syntax
static void Main()
{
if (TryGetANumber(out int i)) {
DoSomethingWith(i);
}
}

End
---

Comments?

Regards,
C. S. Learner

推荐答案

我有更好的建议参考和退出:


而不是使用按引用传递语义,为什么不使用

" copyin / copyout" ref的语义和outout的copyout语义。


这样,您不需要每次都声明一个变量。在很多

的情况下,您将能够直接将价值收入对象

属性。例如:


if(TryGetValue(myObj.MyProp))

{

// doSomething

}


这相当于(差不多)以下你今天必须写的




int i;

if(TryGetValue(out i))

{

myObj.MyProp = i;

}


对于参考 (进/出)参数,增益更加明显。它会在调用之前保存从属性到临时变量的分配,

以及调用后的反向分配。


另外,我理解为什么ref和外出在方法声明中需要,

但是在每次调用时都必须指定它们是多余的。


最后一个改进是具有正常参数(没有关键字,在 

语义中)是只读在方法体内部(你可以用Java做这个,用
用final标记它们)。当然这可能太过分了,因为它不符合C / C ++ / Java传统,但IMO,这是更清洁和

会使语言更容易要了解初学者(一次,

模仿Pascal或ADA而不是C ++会有所帮助)。


只是一些想法...
< br $> b $ b布鲁诺。


" C#Learner" < CS **** @ learner.here> aécritdansle message de

news:ew ************** @ TK2MSFTNGP10.phx.gbl ...
I have a better proposal for ref and out:

instead of using "pass by reference" semantics, why not use
"copyin/copyout" semantics for "ref" and "copyout"semantics for "out".

This way, you would not need to declare a variable every time. In lots of
cases, you would be able to receive the value directly into an object
property. For example:

if (TryGetValue(myObj.MyProp))
{
// doSomething
}

This would be equivalent (almost) to the following that you have to write
today:

int i;
if (TryGetValue(out i))
{
myObj.MyProp = i;
}

For a "ref" (in/out) parameter, the gain is even more obvious. It would save
both the assignment from the property to a temp variable before the call,
and the reverse assignment after the call.

Also, I understand why "ref" and "out" are needed in the method declaration,
but it seems superfluous to have to specify them at every call.

The last refinement would be to have normal parameters (no keyword, "in"
semantics) be "readonly" inside the method body (you can do this in Java by
marking them with "final"). Of course this is probably too far fetched as it
goes against the C/C++/Java tradition, but IMO, this is so much cleaner and
would make the language easier to understand to beginners (for once,
mimicking Pascal or ADA rather than C++ would help).

Just some ideas...

Bruno.

"C# Learner" <cs****@learner.here> a écrit dans le message de
news:ew**************@TK2MSFTNGP10.phx.gbl...
Note
----

请使用固定宽度的字体来查看,例如Courier New。


问题
- -----

当使用修饰符out传递参数时,有必要写两个语句:

- 一个用于由于收到价值而声明变量;和
- 一个用于方法调用,其中变量作为参数传递。

示例:

static void Main()
{
int i;
if(TryGetValue(out i)){
Console.WriteLine(" Value is:{0}。",i);
}
方法返回值的传统方法不一致。

示例:

static void Main( )
{i /> int i = GetValue();
Console.WriteLine(" Value is:{0}。",i);
}


解决方案
--------

这个问题的解决方案是允许声明
变量出现在它被作为
''out''参数传递的声明。

示例:

static void Main()
{
if(TryGetValue(out int i)){
Console.WriteLine(Value is:{0}。,i);
}
}


福利
--------

(a)每次使用这种技术时,方法机构都可以从一个较少的陈述中受益,从而提高可读性。

(b)C#变得更加一致,因为使用传统方式和使用out参数返回值是统一的。再次,
这提高了可读性。

示例:

//传统方式
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}
//使用''out''参数,使用上述语法
static void Main()
{
if(TryGetANumber(out int i)){
DoSomethingWith(i);
}
}


结束
---

评论?

问候,
CS学习者
Note
----

Please use a fixed-width font to view this, such as Courier New.

Problem
-------

When passing a parameter with the modifier ''out'', it is necessary to
write two statements:

- one for the declaration of the variable due to receive the value; and
- one for the method call, where the variable is passed as a parameter.

Example:

static void Main()
{
int i;
if (TryGetValue(out i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

This is inconsistent with the traditional way of returning values from a
method.

Example:

static void Main()
{
int i = GetValue();
Console.WriteLine("Value is: {0}.", i);
}

Solution
--------

The solution to this problem would be to allow the declaration of the
variable to appear within the statment where it''s being passed as an
''out'' parameter.

Example:

static void Main()
{
if (TryGetValue(out int i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

Benefits
--------

(a) Method bodies can benefit from one less statement for each time this
technique is used, promoting readability.

(b) C# becomes more consistent since returning values using both the
traditional way, and using ''out'' parameters, is uniform. Again,
this promotes readability.

Example:

// traditional way
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}

// using an ''out'' parameter, with the aforementioned syntax
static void Main()
{
if (TryGetANumber(out int i)) {
DoSomethingWith(i);
}
}

End
---

Comments?

Regards,
C. S. Learner



" C#Learner" < CS **** @ learner.here>写道:
"C# Learner" <cs****@learner.here> wrote:
[建议的新语法]
if(TryGetValue(out int i))
[proposed new syntax]
if (TryGetValue(out int i))




我'我很欣赏。似乎没有比在for语句中声明

a循环计数器变量更有问题了。


P.



I''d appreciate that. It seems no more problematic than being able to declare
a loop counter variable within a for statement.

P.


>另外,我理解为什么ref和外出在方法中需要

声明,
>Also, I understand why "ref" and "out" are needed in the method
declaration,
但是在每次调用时必须指定它们似乎是多余的。
i认为它使代码更容易理解,例如在调试

时你不必转到方法声明来查明

参数是否是ref或out,相反你会知道那里然后从

调用方法所接受的。


" Bruno Jouhier [MVP]" ; < BJ ****** @ club-internet.fr>在消息中写道

新闻:Oc ************** @ TK2MSFTNGP09.phx.gbl ...我有更好的建议参考和退出:

而不是使用通过引用传递语义,为什么不使用
copyin / copyout ref的语义和复制用于输出的语义。

这样,您不需要每次都声明一个变量。在很多
情况下,您将能够直接将值接收到对象
属性中。例如:

if(TryGetValue(myObj.MyProp))
{
// doSomething
}

这相当于(差不多)到今天你要写的


int i;
if(TryGetValue(out i))
{
myObj。 MyProp = i;
}

对于参考 (进/出)参数,增益更加明显。它将
保存在调用之前从属性到临时变量的赋值,
以及调用后的反向赋值。

另外,我理解为什么ref和外出方法
声明中需要它,但是在每次调用时都必须指定它们是多余的。

最后一个改进是具有正常参数(没有关键字,in"
语义)是只读在方法体内部(您可以通过用final标记它们来在Java
中执行此操作)。当然这可能太过分了,因为它违背了C / C ++ / Java的传统,但IMO,这是更加清晰的
并且会让初学者更容易理解语言(一次,
模仿Pascal或ADA而不是C ++会有所帮助。

只是一些想法...

布鲁诺。

&#C#学习者" < CS **** @ learner.here> écritdansle message de
新闻:ew ************** @ TK2MSFTNGP10.phx.gbl ...
but it seems superfluous to have to specify them at every call. i think it makes the code more understandable, for example while debugging
you wont have to goto to see the method declaration to find out if the
parameter was a ref or out, instead you''ll know right there and then from
the call what the methods accepts.

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:Oc**************@TK2MSFTNGP09.phx.gbl... I have a better proposal for ref and out:

instead of using "pass by reference" semantics, why not use
"copyin/copyout" semantics for "ref" and "copyout"semantics for "out".

This way, you would not need to declare a variable every time. In lots of
cases, you would be able to receive the value directly into an object
property. For example:

if (TryGetValue(myObj.MyProp))
{
// doSomething
}

This would be equivalent (almost) to the following that you have to write
today:

int i;
if (TryGetValue(out i))
{
myObj.MyProp = i;
}

For a "ref" (in/out) parameter, the gain is even more obvious. It would save both the assignment from the property to a temp variable before the call,
and the reverse assignment after the call.

Also, I understand why "ref" and "out" are needed in the method declaration, but it seems superfluous to have to specify them at every call.

The last refinement would be to have normal parameters (no keyword, "in"
semantics) be "readonly" inside the method body (you can do this in Java by marking them with "final"). Of course this is probably too far fetched as it goes against the C/C++/Java tradition, but IMO, this is so much cleaner and would make the language easier to understand to beginners (for once,
mimicking Pascal or ADA rather than C++ would help).

Just some ideas...

Bruno.

"C# Learner" <cs****@learner.here> a écrit dans le message de
news:ew**************@TK2MSFTNGP10.phx.gbl...
注意
- ---

请使用固定宽度的字体来查看,例如Courier New。


问题
------ -

当使用修饰符out传递参数时,有必要写两个语句:

- 一个用于声明变量由于收到价值;和
- 一个用于方法调用,其中变量作为参数传递。

示例:

static void Main()
{
int i;
if(TryGetValue(out i)){
Console.WriteLine(" Value is:{0}。",i);
}
方法返回值的传统方法不一致。

示例:

static void Main( )
{i /> int i = GetValue();
Console.WriteLine(" Value is:{0}。",i);
}


解决方案
--------

这个问题的解决方案是允许声明
变量出现在它被作为
''out''参数传递的声明。

示例:

static void Main()
{
if(TryGetValue(out int i)){
Console.WriteLine(Value is:{0}。,i);
}
}


福利
--------

(a)每次使用这种技术时,方法机构都可以从一个较少的陈述中受益,从而提高可读性。

(b)C#变得更加一致,因为使用传统方式和使用out参数返回值是统一的。再次,
这提高了可读性。

示例:

//传统方式
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}
//使用''out''参数,使用上述语法
static void Main()
{
if(TryGetANumber(out int i)){
DoSomethingWith(i);
}
}


结束
---

评论?

问候,
CS学习者
Note
----

Please use a fixed-width font to view this, such as Courier New.

Problem
-------

When passing a parameter with the modifier ''out'', it is necessary to
write two statements:

- one for the declaration of the variable due to receive the value; and
- one for the method call, where the variable is passed as a parameter.

Example:

static void Main()
{
int i;
if (TryGetValue(out i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

This is inconsistent with the traditional way of returning values from a
method.

Example:

static void Main()
{
int i = GetValue();
Console.WriteLine("Value is: {0}.", i);
}

Solution
--------

The solution to this problem would be to allow the declaration of the
variable to appear within the statment where it''s being passed as an
''out'' parameter.

Example:

static void Main()
{
if (TryGetValue(out int i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

Benefits
--------

(a) Method bodies can benefit from one less statement for each time this
technique is used, promoting readability.

(b) C# becomes more consistent since returning values using both the
traditional way, and using ''out'' parameters, is uniform. Again,
this promotes readability.

Example:

// traditional way
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}

// using an ''out'' parameter, with the aforementioned syntax
static void Main()
{
if (TryGetANumber(out int i)) {
DoSomethingWith(i);
}
}

End
---

Comments?

Regards,
C. S. Learner




这篇关于关于'out'参数的C#语言提案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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