I/O级联c#

Raj*_*tta 5 c# io

在C++中,'>>'和'<<'用于在执行输入/输出操作期间进行级联.
有没有什么方法可以在C#中完成这些事情?直到现在,我知道我可以一次输入一个输入并将其分配给变量,例如,在以下代码片段中:

int a,b;
Console.Write("Enter the value of first number: ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the value of second number: ");
b=Convert.ToInt32(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)

而在C++中,同样的事情可以做到:

int a,b;
cout<<"Enter the values of the two numbers: ";
cin>>a>>b;
Run Code Online (Sandbox Code Playgroud)

taq*_*ion 3

正如 @fredrik 和 @Henk Holterman 所说,该功能并未内置于该语言中。但是...(大但在这里)我们是程序员!几乎没有什么是我们自己无法实现的!

在解释之前,让我们先看一下代码,很多时候代码可以解释自己:

public class Reader
{
    public Reader Read<T>(out T t) where T : struct
    {
        var line = Console.ReadLine();
        t = GetValueFromStringRepresentation<T>(line);
        return this;
    }

    public Reader Read(out string str)
    {
        str = Console.ReadLine();
        return this;
    }

    //GetValueFromStringRepresentation stuff
}
Run Code Online (Sandbox Code Playgroud)

我们在这里实现了一个方法链模式,以根据需要多次读取,并使用out参数来初始化变量。此实现仅适用于结构(但不是全部...)和字符串,这就是采用字符串的重载方法的原因... C# 不允许在类型参数约束中指定 AND... =(

接下来是解析字符串值,这就是我的做法......很好......这只是演示代码:

private static T GetValueFromStringRepresentation<T>(string str)
{
    var type = typeof(T);
    var value = type == typeof(string)
        ? str
        : type == typeof(bool)
            ? bool.Parse(str)
            : type == typeof(sbyte)
                ? sbyte.Parse(str, CultureInfo.InvariantCulture)
                : type == typeof(byte)
                    ? byte.Parse(str, CultureInfo.InvariantCulture)
                    : type == typeof(short)
                        ? short.Parse(str, CultureInfo.InvariantCulture)
                        : type == typeof(ushort)
                            ? ushort.Parse(str, CultureInfo.InvariantCulture)
                            : type == typeof(int)
                                ? int.Parse(str, CultureInfo.InvariantCulture)
                                : type == typeof(uint)
                                    ? uint.Parse(str, CultureInfo.InvariantCulture)
                                    : type == typeof(long)
                                        ? long.Parse(str, CultureInfo.InvariantCulture)
                                        : type == typeof(char)
                                            ? char.Parse(str)
                                            : type == typeof(float)
                                                ? float.Parse(str, CultureInfo.InvariantCulture)
                                                : type == typeof(double)
                                                    ? double.Parse(str, CultureInfo.InvariantCulture)
                                                    : type == typeof(ulong)
                                                        ? ulong.Parse(str, CultureInfo.InvariantCulture)
                                                        : type == typeof(decimal)
                                                            ? decimal
                                                                .Parse(str, CultureInfo.InvariantCulture)
                                                            : type == typeof(Guid)
                                                                ? Guid.Parse(str)
                                                                : (object)null;
    return (T)value;
}
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,这不会对每个可能的结构开箱即用,但您可以轻松添加封装解析的可选参数,例如:Func< string, T > parser

并测试:

    int a, b;
    string c;
    char d;

    var reader = new Reader();
    reader.Read(out a)
        .Read(out b)
        .Read(out c)
        .Read(out d);

    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
    Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

编辑

如果您使用 C# 7+,则可以利用内联变量声明:

    var reader = new Reader();
    reader.Read(out int a)
        .Read(out int b)
        .Read(out string c)
        .Read(out char d);
Run Code Online (Sandbox Code Playgroud)