理解字符串连接
Understanding in string concatenation
我编写了以下代码,其中 IsClear
、IsPermanent
和 IsSalaried
为真。 IsSalaried
是一个可为空的布尔值。我期待像 "Clear,Permanent,Salaried"
这样的输出。但它只提供 "Clear"
的输出。谁能帮我理解以下概念:
using System;
public class Program
{
public static void Main()
{
MyClass Employee = new MyClass();
Employee.IsClear = true;
Employee.IsPermanent = true;
Employee.IsSalaried = true;
string Test =
Employee.IsClear ? "Clear" : ""
+ (Employee.IsPermanent ? "Permanent" : "")
+ (Employee.IsSalaried.HasValue ? "Salaried" : "");
Console.WriteLine(Test);
}
}
public class MyClass
{
public bool IsClear { get; set; }
public bool IsPermanent { get; set; }
public bool? IsSalaried { get; set; }
}
提前致谢!!
这应该会产生编译错误,因为右侧的测试未初始化!以下应该有效:
static void Main(string[] args)
{
bool a = true;
bool b = true;
bool c = true;
string x = "";
string Test = x + (a ? "Clear" : "") + (b ? "Permanent" : "") + (c ? "Salaried" : "");
Console.WriteLine(Test);
Console.ReadLine(); //so that my console window doesn't close
}
我编写了以下代码,其中 IsClear
、IsPermanent
和 IsSalaried
为真。 IsSalaried
是一个可为空的布尔值。我期待像 "Clear,Permanent,Salaried"
这样的输出。但它只提供 "Clear"
的输出。谁能帮我理解以下概念:
using System;
public class Program
{
public static void Main()
{
MyClass Employee = new MyClass();
Employee.IsClear = true;
Employee.IsPermanent = true;
Employee.IsSalaried = true;
string Test =
Employee.IsClear ? "Clear" : ""
+ (Employee.IsPermanent ? "Permanent" : "")
+ (Employee.IsSalaried.HasValue ? "Salaried" : "");
Console.WriteLine(Test);
}
}
public class MyClass
{
public bool IsClear { get; set; }
public bool IsPermanent { get; set; }
public bool? IsSalaried { get; set; }
}
提前致谢!!
这应该会产生编译错误,因为右侧的测试未初始化!以下应该有效:
static void Main(string[] args)
{
bool a = true;
bool b = true;
bool c = true;
string x = "";
string Test = x + (a ? "Clear" : "") + (b ? "Permanent" : "") + (c ? "Salaried" : "");
Console.WriteLine(Test);
Console.ReadLine(); //so that my console window doesn't close
}