在C#中使用out关键字有什么用? [英] what is use of out keyword in C#?

查看:640
本文介绍了在C#中使用out关键字有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,



请告诉我在C#中使用out参数。?

解决方案

< blockquote> out关键字导致参数通过引用传递。这类似于ref关键字,除了ref要求在传递之前初始化变量。要使用out参数,方法定义和调用方法都必须明确使用out关键字。



Out表示引用参数。有时,方法必须返回多个值,而不是存储类状态。满足这些要求。有了它,我们传递参数,其变化在他们的调用方法中实现。

示例: -

 使用系统; 

class 计划
{
静态 void Main()
{
bool 期间; // 用作输出参数
bool 逗号;
bool 分号;
const string value = 有句号,逗号。; // 用作输入字符串

TestString( value out 期间, out 逗号, out 分号);

Console.WriteLine( value ); // 显示值
Console.Write( 句号:); // 显示标签和bools
Console.WriteLine(句号);
Console.Write( 逗号:);
Console.WriteLine(逗号);
Console.Write( 分号:);
Console.WriteLine(分号);
}

静态 void TestString( string value out bool 期间, out bool 逗号, out bool 分号)
{
period = comma = semicolon = false ; // 将所有输出参数指定为false

for int i = 0 ; i < value .Length; i ++)
{
switch value [i])
{
case ' 。'
{
period = ; // 设置参数
break ;
}
case ' , '
{
逗号= true ; // 设置参数
break ;
}
case ' ; '
{
semicolon = true ; // 设置参数
break ;
}
}
}
}
}





输出



有句号,逗号。

句号:真

逗号:真

分号:False


如果您熟悉按值调用调用通过参考,然后很容易理解 out

out 关键字导致参数通过引用传递,更为友好地转到此链接

http://msdn.microsoft.com/en-us/library/t3c3bfhx%28v=vs.80%29.aspx [ ^ ]


hi friends,

please let me know the use of out parameter in C#.?

解决方案

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
or
Out signifies a reference parameter. Sometimes methods must return more than one value and not store class state. Out fills these requirements. With it we pass parameters whose changes are realized in their calling methods.
Example:--

using System;

class Program
{
    static void Main()
    {
    bool period; // Used as out parameter
    bool comma;
    bool semicolon;
    const string value = "has period, comma."; // Used as input string

    TestString(value, out period, out comma, out semicolon);

    Console.WriteLine(value); // Display value
    Console.Write("period: "); // Display labels and bools
    Console.WriteLine(period);
    Console.Write("comma: ");
    Console.WriteLine(comma);
    Console.Write("semicolon: ");
    Console.WriteLine(semicolon);
    }

    static void TestString(string value, out bool period, out bool comma, out bool semicolon)
    {
    period = comma = semicolon = false; // Assign all out parameters to false

    for (int i = 0; i < value.Length; i++)
    {
        switch (value[i])
        {
        case '.':
            {
            period = true; // Set out parameter
            break;
            }
        case ',':
            {
            comma = true; // Set out parameter
            break;
            }
        case ';':
            {
            semicolon = true; // Set out parameter
            break;
            }
        }
    }
    }
}



Output

has period, comma.
period: True
comma: True
semicolon: False


If you are familiar with Call by Value and Call by Reference, then its easy to get idea about out.
out keyword causes arguments to be passed by reference, for more kindly go to this link
http://msdn.microsoft.com/en-us/library/t3c3bfhx%28v=vs.80%29.aspx[^]


这篇关于在C#中使用out关键字有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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