Home > Mobile >  How can I extract an int, from (int, int) format variable? It says I can't use indexing
How can I extract an int, from (int, int) format variable? It says I can't use indexing

Time:02-19

I'm working on a NeuralNet and I decided to store the edges (connections) of the network in format (int,int) edge. I used it because it's very easy to add it to a list List<(int,int)> listOfConnections

I've already implemented this variable in multiple places in my code and only now I've realized I'm not sure how to access each int separately. When I try edge[0] i get an error that I can't use indexing in this type of variable.

Any ideas how can I pull out the first and second ints separately? Also what's the name of this form of variable? It's not Touple, not a Vector, if I knew the name maybe I could find more information on how to use it.

CodePudding user response:

You've created a ValueTuple.

You can access the first item with edge.Item1 and the second with edge.Item2.

.NET Fiddle Example

CodePudding user response:

Tuples don't support indexing in C#. To access individual members in tuples, use the following syntax:

(int, int) edge = (3, 8)
Console.Writeline(edge.Item1) // 3
Console.Writeline(edge.Item2) // 8

Alternatively, you can name the tuple members for better readability:

(int foo, int bar) edge = (3, 8)
Console.Writeline(edge.foo) // 3
Console.Writeline(edge.bar) // 8

Or use tuple unpacking:

(int foo, int bar) = (3, 8)
Console.Writeline(foo) // 3
Console.Writeline(bar) // 8

CodePudding user response:

So you've a list full of ValueTuple:

var edge = (1, 2);
var var listOfConnections = new List<(int, int)>();

Each item is an (int, int), and like Broots says, has an Item1 and Item2..

listOfConnections[0].Item1

..but if you give your tuple names in the List declaration:

var listOfConnections = new List<(int Prev, int Next)>();

..you can use those names:

listOfConnections[0].Prev

Or if you want to give them different names, regardless of what their current names are, you can define the tuple as having different names in the variable signature:

List<(int Start, int End)> someList = listOfConnections;

someList[0].Start;

Or if you want to dig the ValueTuple out with different names declare the "type" of the variable as a different signature with regard to the names:

(int Go, int Stop) x = listOfConnections[0];
x.Go;

Or if you want to deconstruct the ValueTuple inside the list to a pair of variables, give a type signature for the variable but don't give a name for the variable:

(int node1, int node2) = listOfConnections[0];

Console.Write(a); //node1 is an int variable like any other, e.g. var node1 = 1;

The whole thing with ValueTuples and naming the members is really flexible because it's all just syntactic sugar added by the compiler. They're just (int, int) and the names you use depends on the most-recently-given definition for the context:

public List<(int Foo, int Bar)> GetNodes(){
  return ...
}

var x = GetNodes();                             //x[0].Foo
List<(int Baz, int Bop)> y = GetNodes();        //y[0].Baz
var z = (List<(int Bob, int Ben)>)GetNodes();   //z[0].Bob

Just remember - if you're serializing these things to e.g. JSON, they only have names in the context of your source code. By the time they're compiled everything is just called Item1, Item2 so they'll serialize as that. The knowledge of what your source code called them is gone..

  • Related