C
C#15mo ago
احمد

❔ ✅ Return

How do I return a dictionary
56 Replies
احمد
احمدOP15mo 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 🌊💃
Hazel 🌊💃15mo ago
Dictionary requires two type parameters. Dictionary<TKey, TValue>.
احمد
احمدOP15mo ago
public Dictionary<string, int> getNameAndAge() { return nameAndAge; } like that? the key is a string and the value is an int
Hazel 🌊💃
Hazel 🌊💃15mo ago
🙂
احمد
احمدOP15mo 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 🌊💃
Hazel 🌊💃15mo ago
That looks like you tried something like this:
MODiX
MODiX15mo 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 🌊💃
Hazel 🌊💃15mo ago
Console.WriteLine(bob.getNameAndAge)
Console.WriteLine(bob.getNameAndAge)
احمد
احمدOP15mo ago
nope
Hazel 🌊💃
Hazel 🌊💃15mo ago
Console.WriteLine(Human.getNameAndAge);
احمد
احمدOP15mo ago
Human.getNameAndAge yep
Hazel 🌊💃
Hazel 🌊💃15mo ago
That's practically exactly what you did. Human.getNameAndAge is a method, so you need to invoke it like one:
Human.getNameAndAge()
Human.getNameAndAge()
احمد
احمدOP15mo ago
Console.WriteLine(Human.getNameAndAge());
Console.WriteLine(Human.getNameAndAge());
? i tried same thing
MODiX
MODiX15mo 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 🌊💃
Hazel 🌊💃15mo ago
It's not the same thing It's different
احمد
احمدOP15mo ago
oh ye i see
Hazel 🌊💃
Hazel 🌊💃15mo ago
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]
احمد
احمدOP15mo ago
i saw
Hazel 🌊💃
Hazel 🌊💃15mo ago
You're getting closer
احمد
احمدOP15mo ago
yeah
Hazel 🌊💃
Hazel 🌊💃15mo ago
So, I imagine what you want is to print all names and ages
احمد
احمدOP15mo ago
ye in dictionary form {key, value, ... }
Hazel 🌊💃
Hazel 🌊💃15mo ago
You need to iterate over the elements and print each one like:
Console.WriteLine($"{item.Key}: {item.Value}");
Console.WriteLine($"{item.Key}: {item.Value}");
احمد
احمدOP15mo 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 🌊💃
Hazel 🌊💃15mo ago
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 🌊💃
Hazel 🌊💃15mo ago
C# is a zero-based language, yes. Your snippet doesn't make much sense though.
احمد
احمدOP15mo 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
احمد
احمدOP15mo ago
No description
Hazel 🌊💃
Hazel 🌊💃15mo ago
Try using foreach instead. for will work, but foreach is really all that's needed here.
احمد
احمدOP15mo ago
it's working with the for how would you do it?
Hazel 🌊💃
Hazel 🌊💃15mo ago
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
Angius15mo ago
The dictionary has string as the key, you can't use numbers from 0 up to get items from it this way
احمد
احمدOP15mo ago
it's for the index of the pair key = 0, value = 1 and it's iterating over the pairs
Hazel 🌊💃
Hazel 🌊💃15mo ago
It's not though When you access the dictionary like that it expects the key, not an index.
احمد
احمدOP15mo ago
not yet
Hazel 🌊💃
Hazel 🌊💃15mo ago
I highly recommend trying to use foreach instead.
MODiX
MODiX15mo 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
Angius15mo 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 🌊💃
Hazel 🌊💃15mo ago
You can technically iterate over the keys using an index IIRC
احمد
احمدOP15mo 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 🌊💃
Hazel 🌊💃15mo ago
Best way to learn 🙂
احمد
احمدOP15mo ago
how do i use foreach what arguments does it take
MODiX
MODiX15mo 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.
احمد
احمدOP15mo 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
Angius15mo ago
$tias
احمد
احمدOP15mo ago
it's only showing the key
Angius
Angius15mo ago
Trial and error, the basis of all science, including computer science lol
احمد
احمدOP15mo 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
Angius15mo ago
$"{{{pair.Key}, {pair.Value}}}" should do the trick
احمد
احمدOP15mo ago
you gotta do it by hand? 💀
Angius
Angius15mo ago
Well, you could serialize the dictionary to JSON and print it all if you wanted to Otherwise, yes
احمد
احمدOP15mo ago
nvm ye thank you okay i got the hang of it thank you :)))
Hazel 🌊💃
Hazel 🌊💃15mo ago
I supplied a link to documentation that goes over it.
احمد
احمدOP15mo ago
@The Dark Realm's No. 2 - Hazel u 2 thank you 🙂 i understand now thank you again !close
Accord
Accord15mo 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?