Returning a dictionary value in C# without assigning it to a variable?

Adam

I want to use the value of a dictionary without assigning it to a variable:

Dictionary<int, string> LayoutByID = new Dictionary<int, string>() {
    { 0, "foo"},
    { 1, "bar"}
    // ...
};

I can for e.g. print the values if I create a variable:

string b;
LayoutByID.TryGetValue(1,out b);
print("Trying Dictionary to retrieve value for 1: " + b);

But I was wondering if there is a simpler way, something like:

print("Trying Dictionary to retrieve value for 1: " + LayoutByID.TryGetValue(1 [???]));

I understand I could write a function with a switch in it that would work similarly, but using Dictionaries might be cheaper as I have a longer list. Thanks for advice!

Bastian Thiede

You can access the Dictionary with the key like var x = LayoutByID[0]; But you will get an exception, if the Dictionarydoes not contain an entry with that key.

In order to avoid an exception being throw, you can first check if the key exists using LayoutByID.ContainsKey() - and then write your logic for those cases:

if (LayoutByID.ContainsKey(0)) // Check if the key exists (replace 0 with whatever)
{
    var x = LayoutByID[0]; // Access the value and do whatever with it
    // ...
}
else
{
    // Key doesn't exist:
    // Do something else
}

or with C# 6.0 you could also print like this

var key = -1;
var myString = string.Empty;
LayoutByID.TryGetValue(key, out myString);
Console.WriteLine($"Trying Dictionary to retrieve value for {key}: {myString ?? "Error: Invalid ID"}");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning and printing without assigning to variable?

From Dev

declaring a variable without returning a value .

From Dev

declaring a variable without returning a value .

From Dev

bash - declaring a variable without assigning a value

From Dev

Assigning a dictionary to a variable

From Dev

In C99, can I use a return value without first assigning it to a variable?

From Dev

C# Assigning a new value to a variable

From Dev

Instantiating without assigning to a variable

From Dev

Assigning an input variable to python dictionary

From Dev

Assigning a DataTable value to a Variable

From Dev

Assigning value for a class variable

From Dev

Assigning a Variable and a Column to a Value

From Dev

condition is not assigning value to the variable

From Dev

Assigning value to global variable

From Dev

Assigning ArrayList value into a Variable

From Dev

Not returning the right value of the dictionary

From Dev

C++ no equal sign but compiles without assigning value

From Dev

Assigning command to variable without alias

From Dev

conditional operator without assigning to variable

From Dev

C: Iterate through SQLite records and assigning each value to a variable

From Dev

Custom method for console write line and assigning value to variable - C#

From Dev

AngularJS: returning data from a function and assigning it to a variable

From Dev

returning array from function and assigning it to variable

From Dev

In Python, what is a minimal way of assigning a value to a variable depending on whether a dictionary contains a key?

From Dev

Assigning value to pointer without new

From Dev

FORTRAN - Assigning an array value to variable

From Dev

Awk not assigning value of field to variable

From Dev

Assigning a value in SQLite cursor to a variable

From Dev

Assigning a value to a variable randomly, but not working

Related Related

  1. 1

    Returning and printing without assigning to variable?

  2. 2

    declaring a variable without returning a value .

  3. 3

    declaring a variable without returning a value .

  4. 4

    bash - declaring a variable without assigning a value

  5. 5

    Assigning a dictionary to a variable

  6. 6

    In C99, can I use a return value without first assigning it to a variable?

  7. 7

    C# Assigning a new value to a variable

  8. 8

    Instantiating without assigning to a variable

  9. 9

    Assigning an input variable to python dictionary

  10. 10

    Assigning a DataTable value to a Variable

  11. 11

    Assigning value for a class variable

  12. 12

    Assigning a Variable and a Column to a Value

  13. 13

    condition is not assigning value to the variable

  14. 14

    Assigning value to global variable

  15. 15

    Assigning ArrayList value into a Variable

  16. 16

    Not returning the right value of the dictionary

  17. 17

    C++ no equal sign but compiles without assigning value

  18. 18

    Assigning command to variable without alias

  19. 19

    conditional operator without assigning to variable

  20. 20

    C: Iterate through SQLite records and assigning each value to a variable

  21. 21

    Custom method for console write line and assigning value to variable - C#

  22. 22

    AngularJS: returning data from a function and assigning it to a variable

  23. 23

    returning array from function and assigning it to variable

  24. 24

    In Python, what is a minimal way of assigning a value to a variable depending on whether a dictionary contains a key?

  25. 25

    Assigning value to pointer without new

  26. 26

    FORTRAN - Assigning an array value to variable

  27. 27

    Awk not assigning value of field to variable

  28. 28

    Assigning a value in SQLite cursor to a variable

  29. 29

    Assigning a value to a variable randomly, but not working

HotTag

Archive