C
C#9mo ago
Emelie

✅ Im very(!) confused about the typeof method(not even sure if method is the correct name)

When looping through an enum, we use Enum.GetValues to retrieve an array of each value in the Enum. What I really am confused about is the typeof(Enum) parameter... why doesn´t it work if I remove the "typeof" part? been researching the microsoft API but I still am not getting any wiser...
No description
145 Replies
Pobiega
Pobiega9mo ago
Because Priority is just a type identifier, its not an instance of Type class typeof is a special keyword that AT COMPILE-TIME gets an instance of the correct type and inserts it there
why doesn´t it work if I remove the "typeof"
Because Enum.GetValues expects a Type type If you're using a somewhat modern version of .NET, there are generic versions of Enum.GetValues and Enum.GetNames that you should prefer Enum.GetValues<Priority>()
Emelie
Emelie9mo ago
buuut if Enum.GetValues is meant to be used for enums...
Pobiega
Pobiega9mo ago
it is.
Emelie
Emelie9mo ago
shouldn´t it be enough to just put the name of the Enum in the parameter?
Pobiega
Pobiega9mo ago
Thats not how C# the language works you can't just "pass a name in the parameter" willy nilly we use generics or Type variables to pass type information
Emelie
Emelie9mo ago
So this is just an explicit way of clarifying that my Enum is in fact, an enum?
Pobiega
Pobiega9mo ago
no this is an explicit way of declaring what type you want to iterate this is the same as suggesting Console.WriteLine(string); should work or 1 + int
Emelie
Emelie9mo ago
Enums, they are an enumeration type.. just like a string is a string type?
Pobiega
Pobiega9mo ago
an enum is a type, yes
Emelie
Emelie9mo ago
but your example.. its not fully the same? because what we do, is we create a string like this: string myString = "something"
Pobiega
Pobiega9mo ago
we create a string instance, yes
Emelie
Emelie9mo ago
and, if I want to print it, I dont have to do Console.WriteLine(typeof(myString)
Pobiega
Pobiega9mo ago
but the word string still just means the type thats not the same the equivialent of string myString = "hello"; is Priority p = Priority.High; Priority itself is not a value, its the type
Emelie
Emelie9mo ago
ah I think Im getting it
Pobiega
Pobiega9mo ago
typeof(Priority) gives you a type reference
Emelie
Emelie9mo ago
So when we create Enums.. its basically like creating your own data type?
Pobiega
Pobiega9mo ago
thats exactly what it is
Emelie
Emelie9mo ago
but if Priority is the data type... what exactly is retrieved when we do typeof on an Enum? thats what I don´t get
Pobiega
Pobiega9mo ago
there are many different kind of datatypes, enums is one of them the type reference sometimes, code needs to talk about types at runtime so the C# language has this concept of a Type type one sec, lemme write some demo code
Emelie
Emelie9mo ago
yes please! 😄
MODiX
MODiX9mo ago
Pobiega
REPL Result: Success
string str = "Hello";
Type t1 = str.GetType();
Type t2 = typeof(string);

Console.WriteLine(t1 == t2);
Console.WriteLine(t1.FullName);
string str = "Hello";
Type t1 = str.GetType();
Type t2 = typeof(string);

Console.WriteLine(t1 == t2);
Console.WriteLine(t1.FullName);
Console Output
True
System.String
True
System.String
Compile: 568.472ms | Execution: 73.029ms | React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
so we make a string. we then get the type of that string instance. We then get the type of string itself and compare them I then finally print the "FullName" property of that type
Emelie
Emelie9mo ago
so, t1 is equal to the type of str?
Pobiega
Pobiega9mo ago
thats an assignment, so yes
Emelie
Emelie9mo ago
and, t1 is also a type object? which is why it is able to save the type information
Pobiega
Pobiega9mo ago
Im saying Set the variable t1 to be the type of variable str yes t1is of type Type the same way str is string
Emelie
Emelie9mo ago
because we cant to string t1 = str.GetType()
Pobiega
Pobiega9mo ago
no, that would return a Type
Emelie
Emelie9mo ago
lemme think for a sec
Pobiega
Pobiega9mo ago
No description
Emelie
Emelie9mo ago
foreach (Priority p in Enum.GetValues(typeof(Priority))) so when I do this, typeof(priority) will read as system.Enum?
MODiX
MODiX9mo ago
Angius
REPL Result: Success
string str = "Henlo";
Type type = str.GetType();
Type typeType = type.GetType();

new {
type = type.FullName,
type_type = typeType.FullName
}
string str = "Henlo";
Type type = str.GetType();
Type typeType = type.GetType();

new {
type = type.FullName,
type_type = typeType.FullName
}
Result: <>f__AnonymousType0#1<string, string>
{
"type": "System.String",
"type_type": "System.RuntimeType"
}
{
"type": "System.String",
"type_type": "System.RuntimeType"
}
Compile: 565.296ms | Execution: 90.558ms | React with ❌ to remove this embed.
Angius
Angius9mo ago
enum is like a class or struct typeof(string) doesn't return class
Pobiega
Pobiega9mo ago
( I have a feeling those are not known concepts yet)
Angius
Angius9mo ago
So typeof(MyEnum) won't return enum
Emelie
Emelie9mo ago
soo what does it return?
Angius
Angius9mo ago
Your... enum's type
MODiX
MODiX9mo ago
Angius
REPL Result: Success
class Foo {}
enum Bar {}

new {
Foo = typeof(Foo).FullName,
Bar = typeof(Bar).FullName
}
class Foo {}
enum Bar {}

new {
Foo = typeof(Foo).FullName,
Bar = typeof(Bar).FullName
}
Result: <>f__AnonymousType0#1<string, string>
{
"foo": "Submission#0+Foo",
"bar": "Submission#0+Bar"
}
{
"foo": "Submission#0+Foo",
"bar": "Submission#0+Bar"
}
Compile: 271.875ms | Execution: 85.581ms | React with ❌ to remove this embed.
Emelie
Emelie9mo ago
lets say I have two different enums
Angius
Angius9mo ago
Ignore that Submission#0+, it's the bot that adds it
Emelie
Emelie9mo ago
And I do the foreach for both in what ways will the result of typeof be different? isnt the typeof a enum always an enum type?
MODiX
MODiX9mo ago
Angius
REPL Result: Success
enum Foo {}
enum Bar {}

new {
Foo = typeof(Foo).FullName,
Bar = typeof(Bar).FullName
}
enum Foo {}
enum Bar {}

new {
Foo = typeof(Foo).FullName,
Bar = typeof(Bar).FullName
}
Result: <>f__AnonymousType0#1<string, string>
{
"foo": "Submission#0+Foo",
"bar": "Submission#0+Bar"
}
{
"foo": "Submission#0+Foo",
"bar": "Submission#0+Bar"
}
Compile: 276.234ms | Execution: 77.764ms | React with ❌ to remove this embed.
Angius
Angius9mo ago
See? Different The first type is the type of enum Foo, the second is the type of enum Bar
Emelie
Emelie9mo ago
ahhh but where can you see that? what did you do to see the different types that explicitly?
Angius
Angius9mo ago
No description
Angius
Angius9mo ago
This contains all of the info about the given type
Emelie
Emelie9mo ago
and FullName?
Angius
Angius9mo ago
It's one of the properties of Type that returns the type's... full name
Emelie
Emelie9mo ago
ok! I think I understand
Pobiega
Pobiega9mo ago
when you declare an enum, its type isn't "Enum" the way a string variabes type is String because you didnt make a variable. you made a type
Emelie
Emelie9mo ago
basically, when we create enums, there will automatically be created unique "names" for each enum? and when we throw in an enum to use for example with .GetValues, we must use an "identifier" for the specific Enum?
Pobiega
Pobiega9mo ago
the enums "unique name" is the type
Emelie
Emelie9mo ago
would it then be possible to replace the entire typeof parameter with for example Submission#0+Foo , if that is the type?
Angius
Angius9mo ago
enum Animals
{
Cat,
Dog,
Cockatoo
}
enum Animals
{
Cat,
Dog,
Cockatoo
}
is like
class Person
{
public string Name { get; set; }
public DateTimeOnly Birthday { get; set; }
}
class Person
{
public string Name { get; set; }
public DateTimeOnly Birthday { get; set; }
}
not like
string s = "OwO";
string s = "OwO";
No That is just the type's full name
Pobiega
Pobiega9mo ago
not with the string "Submission#0+Foo", thats just a string
Angius
Angius9mo ago
You need the full type instance
Pobiega
Pobiega9mo ago
its not the real type
MODiX
MODiX9mo ago
Pobiega
REPL Result: Success
enum Priority
{
Unknown,
Low,
Medium,
High
}

Type t = typeof(Priority);
var names = Enum.GetNames(t);
Console.WriteLine(string.Join(", ", names));
enum Priority
{
Unknown,
Low,
Medium,
High
}

Type t = typeof(Priority);
var names = Enum.GetNames(t);
Console.WriteLine(string.Join(", ", names));
Console Output
Unknown, Low, Medium, High
Unknown, Low, Medium, High
Compile: 669.731ms | Execution: 85.775ms | React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
this works, but its kinda silly
Emelie
Emelie9mo ago
so that would be like using the string value instead of the actual string in a method? which is why it wouldn´t work
Pobiega
Pobiega9mo ago
its just saving the type in a variable before using it, instead of using it directly
Angius
Angius9mo ago
Uh, more like using a string length instead of the string Or using a person's first name, instead of the whole person object You can't just use one property of a type as a substitute for the whole type
Pobiega
Pobiega9mo ago
Your name is Emelie, but you are not uniquely identified by "Emelie" there are many other Emelies as well
Emelie
Emelie9mo ago
aaand lets say there are a bunch of things which make me unique (hopefully lol)
Pobiega
Pobiega9mo ago
sure But Emelie is not a type, its an instance of Person most likely 🙂 each person has a name, a date of birth, etc etc etc
Emelie
Emelie9mo ago
we could name all of those things together with a singular FullName, as "THINGS-THAT-MAKE-EMELIE-UNIQE"
Pobiega
Pobiega9mo ago
like write it all out in a big string?
Emelie
Emelie9mo ago
but its just the name of it... you have to access all of those unique attributes in some way
Pobiega
Pobiega9mo ago
sure, we could. Thats called "serialization" in rprogramming
Emelie
Emelie9mo ago
which is kind of how typeof works
Pobiega
Pobiega9mo ago
no? typeof is magic its a part of the compiler
Emelie
Emelie9mo ago
i mean, typeof is a way of accessing the thing that actually IS the thing, if that makes sense 😅
Pobiega
Pobiega9mo ago
yes thats exactly right
Emelie
Emelie9mo ago
which is why I cant just use the name of the type
Pobiega
Pobiega9mo ago
yes
Emelie
Emelie9mo ago
well, then Im gonna continue on with my code! Maaaany thanks, once again Pobiega!!
Pobiega
Pobiega9mo ago
btw
Emelie
Emelie9mo ago
ye?
MODiX
MODiX9mo ago
Pobiega
REPL Result: Success
enum Priority
{
Unknown,
Low,
Medium,
High
}

var names = Enum.GetNames<Priority>();
Console.WriteLine(string.Join(", ", names));
enum Priority
{
Unknown,
Low,
Medium,
High
}

var names = Enum.GetNames<Priority>();
Console.WriteLine(string.Join(", ", names));
Console Output
Unknown, Low, Medium, High
Unknown, Low, Medium, High
Compile: 616.703ms | Execution: 82.806ms | React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
use this instead 🙂 Enum.GetNames<Priority>(); is a better version of the same thing
Emelie
Emelie9mo ago
is the < > a replacement of typeof?
Angius
Angius9mo ago
It's a generic
Pobiega
Pobiega9mo ago
in this particular method, more or less
Emelie
Emelie9mo ago
meaning, it indicates the same thing - we want the type?
Pobiega
Pobiega9mo ago
well, its a way of sending type information to the method just like taking a Type parameter is, but differenr this way is better for the C# runtime or well, the compiler
Emelie
Emelie9mo ago
in this case im not sure it works?
Emelie
Emelie9mo ago
No description
Angius
Angius9mo ago
It does
Emelie
Emelie9mo ago
im doing a wpf app by the way, and cbChoosePrio is my combobox
Pobiega
Pobiega9mo ago
GetValues(typeof(T)) and GetValues<T>() are identical they do th eexact same thing
Angius
Angius9mo ago
Well, what they return is identical
Pobiega
Pobiega9mo ago
yeah true they return the exact same thing, so there should be no difference for you as the user how they WORK behind the scenes is a bit different
Emelie
Emelie9mo ago
if i do this
Emelie
Emelie9mo ago
No description
Emelie
Emelie9mo ago
I get : Cannot convert type 'type' to 'type'
Pobiega
Pobiega9mo ago
well you changed Valuesto Names if you want values, keep it values Names returns the names as strings but actually, looking at your code...
Emelie
Emelie9mo ago
it works now! when I changed to values
Pobiega
Pobiega9mo ago
the only thing you do wiht the value is calling .ToString()on it do you know what that returns? the name :p
Emelie
Emelie9mo ago
yeah the enum looks like this:
Emelie
Emelie9mo ago
No description
Emelie
Emelie9mo ago
and the result I get with the foreach is this
Pobiega
Pobiega9mo ago
thus, you could just write
foreach (string p in Enum.GetNames<Priority>())
{
cbChoosePrio.Items.Add(p);
}
foreach (string p in Enum.GetNames<Priority>())
{
cbChoosePrio.Items.Add(p);
}
this code does the same thing as yours granted, I think you might want to change this code a bit later on if you want the combobox to pick betwen those values, dont use strings in WPF you can set a data source for comboboxes I believe
Emelie
Emelie9mo ago
No description
Pobiega
Pobiega9mo ago
if I pick "High" there, do you want to be able to get the string "High" out from the box, or Priority.High?
Emelie
Emelie9mo ago
that makes sense 😅 I actually followed a tutorial online, but something told me I was doing it too complicated with the tostring
Pobiega
Pobiega9mo ago
note that Priority.High would not be a string, but a Priority value
Emelie
Emelie9mo ago
No description
Pobiega
Pobiega9mo ago
Oh dear god That's so convoluted 🙂
Emelie
Emelie9mo ago
it is, right? I had a feeling this was all to complicated
Pobiega
Pobiega9mo ago
Yeah if you store the enum.value itself instead of the string version You don't need to parse it back
Emelie
Emelie9mo ago
yes, but this is the thing:
Emelie
Emelie9mo ago
No description
Pobiega
Pobiega9mo ago
Yup?
Emelie
Emelie9mo ago
In the assignment I have gotten, the Priority and TaskType Enums HAVE TO be a property of the Task class
Pobiega
Pobiega9mo ago
That's okay In fact, that makes sense
Emelie
Emelie9mo ago
And I need to parse it back from a string, in order to set it
Pobiega
Pobiega9mo ago
No you dont Comboboxes can carry values directly Not just strings
Emelie
Emelie9mo ago
I dont understand 😅 carry values "directly"?
Pobiega
Pobiega9mo ago
So look at where you add the priorities to your combobox Can you paste that code again?
Emelie
Emelie9mo ago
of course!
Emelie
Emelie9mo ago
No description
Pobiega
Pobiega9mo ago
Right. What do you think p.ToString() does?
Emelie
Emelie9mo ago
it is the string representation of the p object?
Pobiega
Pobiega9mo ago
It is indeed and if you put your mouse to hover over Add
Emelie
Emelie9mo ago
it says it can add an object?
Pobiega
Pobiega9mo ago
yep so we can add anything because everything is an object so lets just remove .ToString() so it just adds p
Emelie
Emelie9mo ago
hmm it actually looks just the same, you are right
Pobiega
Pobiega9mo ago
yep, and then in your btn_add_click method we cast to (Priority) instead of (string) Priority priority = (Priority)cb...
Emelie
Emelie9mo ago
No description
Emelie
Emelie9mo ago
like this?
Pobiega
Pobiega9mo ago
tada! we now have the priority, without tostring and parsing yes
Emelie
Emelie9mo ago
I must say, this looks a billion times smoother
Pobiega
Pobiega9mo ago
right? and it works the same way for the user, but its a bit faster for the computer 🙂
Emelie
Emelie9mo ago
Its so easy to get the wrong ideas when looking online
Pobiega
Pobiega9mo ago
absolutely
Emelie
Emelie9mo ago
what also worries me is that my actual teacher published a solution which looks almost identical to the one before with all of the parsing back and forth just so confusing!
Pobiega
Pobiega9mo ago
well, now you can show them this solution and be like "This is better, because converting back and forth is a waste of CPU time" (and readability is important)
Emelie
Emelie9mo ago
yes absolutely
Pobiega
Pobiega9mo ago
actually, the readability part is way more important
Emelie
Emelie9mo ago
Im actually going to show him this tomorrow 🙂
Pobiega
Pobiega9mo ago
"Pobiega sends his regards" * stabs teacher *
Emelie
Emelie9mo ago
because I already told him I was really confused about all this parsing and his way of using enums
Pobiega
Pobiega9mo ago
As your lawyer, I advice against actually stabbing the teacher.
Pobiega
Pobiega9mo ago
catcool
Want results from more Discord servers?
Add your server
More Posts