C
C#2y ago
احمد

❔ ✅ Return

How do I return a dictionary
56 Replies
احمد
احمدOP2y ago
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine();
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public Dictionary getNameAndAge() // the error is here, how do i return a dictionary via a function?
{ //
return nameAndAge; //
} //



}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine();
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public Dictionary getNameAndAge() // the error is here, how do i return a dictionary via a function?
{ //
return nameAndAge; //
} //



}
Hazel 🌊💃
Dictionary requires two type parameters. Dictionary<TKey, TValue>.
احمد
احمدOP2y ago
public Dictionary<string, int> getNameAndAge() { return nameAndAge; } like that? the key is a string and the value is an int
Hazel 🌊💃
🙂
احمد
احمدOP2y ago
but how do i get the whole list because i can only call it via the object not the class if i make it static? but i got this
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Hazel 🌊💃
That looks like you tried something like this:
MODiX
MODiX2y ago
Ahmed
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge);
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge);
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Console Output
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Compile: 765.760ms | Execution: 122.947ms | React with ❌ to remove this embed.
Hazel 🌊💃
Console.WriteLine(bob.getNameAndAge)
Console.WriteLine(bob.getNameAndAge)
احمد
احمدOP2y ago
nope
Hazel 🌊💃
Console.WriteLine(Human.getNameAndAge);
احمد
احمدOP2y ago
Human.getNameAndAge yep
Hazel 🌊💃
That's practically exactly what you did. Human.getNameAndAge is a method, so you need to invoke it like one:
Human.getNameAndAge()
Human.getNameAndAge()
احمد
احمدOP2y ago
Console.WriteLine(Human.getNameAndAge());
Console.WriteLine(Human.getNameAndAge());
? i tried same thing
MODiX
MODiX2y ago
Ahmed
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge());
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge());
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Console Output
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Compile: 770.151ms | Execution: 121.414ms | React with ❌ to remove this embed.
Hazel 🌊💃
It's not the same thing It's different
احمد
احمدOP2y ago
oh ye i see
Hazel 🌊💃
Look closely:
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
احمد
احمدOP2y ago
i saw
Hazel 🌊💃
You're getting closer
احمد
احمدOP2y ago
yeah
Hazel 🌊💃
So, I imagine what you want is to print all names and ages
احمد
احمدOP2y ago
ye in dictionary form {key, value, ... }
Hazel 🌊💃
You need to iterate over the elements and print each one like:
Console.WriteLine($"{item.Key}: {item.Value}");
Console.WriteLine($"{item.Key}: {item.Value}");
احمد
احمدOP2y ago
hmm i'd need to iterate a certain number of times too so i got a question dict = {key, value [0] key,value [1] } does the first pair count as 0
Hazel 🌊💃
Iteration statements -for, foreach, do, and while - C#
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
Hazel 🌊💃
C# is a zero-based language, yes. Your snippet doesn't make much sense though.
احمد
احمدOP2y ago
look
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);


int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}


Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);


int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}


Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
this is the snippet im referring to
int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}
int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}
@The Dark Realm's No. 2 - Hazel
احمد
احمدOP2y ago
No description
Hazel 🌊💃
Try using foreach instead. for will work, but foreach is really all that's needed here.
احمد
احمدOP2y ago
it's working with the for how would you do it?
Hazel 🌊💃
I'll tell you once you have a working version in the thread 🙂 Giving you what I would do won't help you learn 😄
Angius
Angius2y ago
The dictionary has string as the key, you can't use numbers from 0 up to get items from it this way
احمد
احمدOP2y ago
it's for the index of the pair key = 0, value = 1 and it's iterating over the pairs
Hazel 🌊💃
It's not though When you access the dictionary like that it expects the key, not an index.
احمد
احمدOP2y ago
not yet
Hazel 🌊💃
I highly recommend trying to use foreach instead.
MODiX
MODiX2y ago
Angius
REPL Result: Success
var dictr = new Dictionary<string, int>(){
["twelve"] = 12,
["eleven"] = 11,
};

dictr["twelve"]
var dictr = new Dictionary<string, int>(){
["twelve"] = 12,
["eleven"] = 11,
};

dictr["twelve"]
Result: int
12
12
Compile: 515.941ms | Execution: 40.232ms | React with ❌ to remove this embed.
Angius
Angius2y ago
This is how you retrieve values from a dictionary But, yes, if you wanted both keys and values, you would use a foreach
Hazel 🌊💃
You can technically iterate over the keys using an index IIRC
احمد
احمدOP2y ago
ima leave this code here n switch to foreach
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
string key = list_for_nameAndAge[KeyValuePair];
int value =
}
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
string key = list_for_nameAndAge[KeyValuePair];
int value =
}
Hazel 🌊💃
Best way to learn 🙂
احمد
احمدOP2y ago
how do i use foreach what arguments does it take
MODiX
MODiX2y ago
Angius
REPL Result: Success
foreach (char letter in "word")
{
Console.WriteLine($"The letter is '{letter}'");
}
foreach (char letter in "word")
{
Console.WriteLine($"The letter is '{letter}'");
}
Console Output
The letter is 'w'
The letter is 'o'
The letter is 'r'
The letter is 'd'
The letter is 'w'
The letter is 'o'
The letter is 'r'
The letter is 'd'
Compile: 612.394ms | Execution: 91.784ms | React with ❌ to remove this embed.
احمد
احمدOP2y ago
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
foreach (var pair in list_for_nameAndAge)
{
Console.WriteLine(pair.Key, pair.Value);
}
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
foreach (var pair in list_for_nameAndAge)
{
Console.WriteLine(pair.Key, pair.Value);
}
?
Angius
Angius2y ago
$tias
احمد
احمدOP2y ago
it's only showing the key
Angius
Angius2y ago
Trial and error, the basis of all science, including computer science lol
احمد
احمدOP2y ago
not the value int length_of_objectCount = Human.objectCount; Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge; foreach (var pair in list_for_nameAndAge) { Console.WriteLine(pair.Key + pair.Value); } i fixed it how do you get it to appear like {key, value ... } in that format
Angius
Angius2y ago
$"{{{pair.Key}, {pair.Value}}}" should do the trick
احمد
احمدOP2y ago
you gotta do it by hand? 💀
Angius
Angius2y ago
Well, you could serialize the dictionary to JSON and print it all if you wanted to Otherwise, yes
احمد
احمدOP2y ago
nvm ye thank you okay i got the hang of it thank you :)))
Hazel 🌊💃
I supplied a link to documentation that goes over it.
احمد
احمدOP2y ago
@The Dark Realm's No. 2 - Hazel u 2 thank you 🙂 i understand now thank you again !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?