C#中的I/O级联

5
在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());

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

int a,b;
cout<<"Enter the values of the two numbers: ";
cin>>a>>b;

3
不。据我所知,在C#中没有其他方法可以实现它,尽管您的C#代码可能会更简化(通过只问一个问题并读取两次,就像您的C++代码一样......目前它们甚至不能产生相同的明显输出)。 - fredrik
1
C#的方法是使用ReadLine(),然后解析该行。 - H H
1个回答

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
}

我们在这里实现了方法链模式,以便可以根据需要多次阅读,并使用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;
}

如我之前所说,这种方法不能直接适用于所有可能的结构体,但您可以轻松添加一个可选参数来封装解析,例如: 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();

编辑

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

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

哇,那么长的三元运算符链看起来很棒!+1 - Sweeper
谢谢。非常抱歉回复晚了。这完全解决了我的疑惑。 - Rajdeep Dutta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接