C# 在C中声明元组而不进行初始化

C# 在C中声明元组而不进行初始化,c#,tuples,C#,Tuples,我希望在代码中动态使用元组,并且必须根据if语句将值分配给元组。我有以下代码: if(check != null) var scoreTmpTuple = new Tuple<Guid, string, double>( (Guid)row["sampleGuid"], Convert.ToString(row["sampleName"]), Convert.ToDouble(row["sampleScore"])); else var scor

我希望在代码中动态使用元组,并且必须根据if语句将值分配给元组。我有以下代码:

if(check != null)
  var scoreTmpTuple = new Tuple<Guid, string, double>(
    (Guid)row["sampleGuid"],
     Convert.ToString(row["sampleName"]), 
     Convert.ToDouble(row["sampleScore"]));
else
  var scoreTmpTuple = new Tuple<Guid, string, double>(
    (Guid)row["exampleGuid"],
     Convert.ToString(row["exampleName"]), 
     Convert.ToDouble(row["exampleScore"]));

在代码中,元组在if和else语句中声明。我想在外部声明它,并相应地初始化元组

您可以尝试三元运算符并在元组创建中推送分支:

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[check != null ? "sampleGuid" : "exampleGuid"],
     Convert.ToString(row[check != null ? "sampleName" : "exampleName"]),
     Convert.ToDouble(row[check != null ? "sampleScore" : "exampleScore"])
  );
或者,即使我们实际上应该在示例前缀和示例前缀之间切换:

  string prefix = check != null 
    ? "sample"
    : "example";

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[$"{prefix}Guid"],
     Convert.ToString(row[$"{prefix}Name"]),
     Convert.ToDouble(row[$"{prefix}Score"])
  );
您可以尝试在元组创建中使用三元运算符和推送分支:

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[check != null ? "sampleGuid" : "exampleGuid"],
     Convert.ToString(row[check != null ? "sampleName" : "exampleName"]),
     Convert.ToDouble(row[check != null ? "sampleScore" : "exampleScore"])
  );
或者,即使我们实际上应该在示例前缀和示例前缀之间切换:

  string prefix = check != null 
    ? "sample"
    : "example";

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[$"{prefix}Guid"],
     Convert.ToString(row[$"{prefix}Name"]),
     Convert.ToDouble(row[$"{prefix}Score"])
  );

只需指定类型显式,而不使用var:

Tuple<Guid, string, double> scoreTmpTuple;

if (check != null)
    scoreTmpTuple = Tuple.Create<Guid, string, double>(Guid.NewGuid(), "hello", 3.14);

只需指定类型显式,而不使用var:

Tuple<Guid, string, double> scoreTmpTuple;

if (check != null)
    scoreTmpTuple = Tuple.Create<Guid, string, double>(Guid.NewGuid(), "hello", 3.14);
您可以这样做:

bool isChecked = check != null;

var scoreTmpTuple = new Tuple<Guid, string, double>(
                      isChecked ? (Guid)row["sampleGuid"] : (Guid)row["exampleGuid"],
                      isChecked ? Convert.ToString(row["sampleName"]) : Convert.ToString(row["exampleName"]), 
                      isChecked ? Convert.ToDouble(row["sampleScore"]) : Convert.ToDouble(row["exampleScore"]));
您可以这样做:

bool isChecked = check != null;

var scoreTmpTuple = new Tuple<Guid, string, double>(
                      isChecked ? (Guid)row["sampleGuid"] : (Guid)row["exampleGuid"],
                      isChecked ? Convert.ToString(row["sampleName"]) : Convert.ToString(row["exampleName"]), 
                      isChecked ? Convert.ToDouble(row["sampleScore"]) : Convert.ToDouble(row["exampleScore"]));

在if语句之前声明元组

Tuple<Guid, string, double> scoreTmpTuple;

if(check != null)
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["sampleGuid"],Convert.ToString(row["sampleName"]), Convert.ToDouble(row["sampleScore"]));

else
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["exampleGuid"],Convert.ToString(row["exampleName"]), Convert.ToDouble(row["exampleScore"]));

在if语句之前声明元组

Tuple<Guid, string, double> scoreTmpTuple;

if(check != null)
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["sampleGuid"],Convert.ToString(row["sampleName"]), Convert.ToDouble(row["sampleScore"]));

else
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["exampleGuid"],Convert.ToString(row["exampleName"]), Convert.ToDouble(row["exampleScore"]));

这可能行得通,但我有元组的范围问题。。因为很多事情都是在初始化后进行的,我希望使用相同的元组。在c4之上,可以使用动态数据类型,而不是元组。这可能会起作用,但元组的作用域有问题。。因为很多事情都是在初始化之后完成的,我希望使用相同的元组。在c4之上,您可以使用dynamic datatype.bool isChecked=check!=无效的是否对较短的codebool进行了检查=检查!=无效的是一个较短的代码