Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring tuple without initializing in C#

Tags:

c#

tuples

I want to use Tuples in my code dynamically and have to assign the values to tuple according to the if statement. I have the following code:

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"]));

In the code, the tuple is declared inside the if and else statements. I want to declare that outside and initialize the tuple accordingly.

like image 521
Haroon nasir Avatar asked May 10 '26 15:05

Haroon nasir


2 Answers

Declare the tuple before the if statement.

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"]));
like image 70
Mei Avatar answered May 13 '26 06:05

Mei


Just specify the type explicity instead of using var:

Tuple<Guid, string, double> scoreTmpTuple;

if (check != null)
    scoreTmpTuple = Tuple.Create<Guid, string, double>(Guid.NewGuid(), "hello", 3.14);
like image 31
Alexander Avatar answered May 13 '26 04:05

Alexander