✅ 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...
145 Replies
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>()
buuut
if Enum.GetValues is meant to be used for enums...
it is.
shouldn´t it be enough to just put the name of the Enum in the parameter?
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 informationSo this is just an explicit way of clarifying that my Enum is in fact, an enum?
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
Enums, they are an enumeration type.. just like a string is a string type?
an enum is a type, yes
but your example.. its not fully the same? because what we do, is we create a string like this: string myString = "something"
we create a string instance, yes
and, if I want to print it, I dont have to do Console.WriteLine(typeof(myString)
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 typeah I think Im getting it
typeof(Priority)
gives you a type referenceSo when we create Enums.. its basically like creating your own data type?
thats exactly what it is
but if Priority is the data type... what exactly is retrieved when we do typeof on an Enum? thats what I don´t get
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 codeyes please! 😄
Pobiega
REPL Result: Success
Console Output
Compile: 568.472ms | Execution: 73.029ms | React with ❌ to remove this embed.
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 typeso, t1 is equal to the type of str?
thats an assignment, so yes
and, t1 is also a type object? which is why it is able to save the type information
Im saying
Set the variable t1 to be the type of variable str
yes
t1
is of type Type
the same way str
is string
because we cant to string t1 = str.GetType()
no, that would return a
Type
lemme think for a sec
foreach (Priority p in Enum.GetValues(typeof(Priority)))
so when I do this, typeof(priority) will read as system.Enum?
Angius
REPL Result: Success
Result: <>f__AnonymousType0#1<string, string>
Compile: 565.296ms | Execution: 90.558ms | React with ❌ to remove this embed.
enum
is like a class
or struct
typeof(string)
doesn't return class
( I have a feeling those are not known concepts yet)
So
typeof(MyEnum)
won't return enum
soo what does it return?
Your... enum's type
Angius
REPL Result: Success
Result: <>f__AnonymousType0#1<string, string>
Compile: 271.875ms | Execution: 85.581ms | React with ❌ to remove this embed.
lets say I have two different enums
Ignore that
Submission#0+
, it's the bot that adds itAnd 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?
Angius
REPL Result: Success
Result: <>f__AnonymousType0#1<string, string>
Compile: 276.234ms | Execution: 77.764ms | React with ❌ to remove this embed.
See? Different
The first type is the type of enum
Foo
, the second is the type of enum Bar
ahhh
but where can you see that?
what did you do to see the different types that explicitly?
This contains all of the info about the given type
and FullName?
It's one of the properties of
Type
that returns the type's... full nameok! I think I understand
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 typebasically, 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?
the enums "unique name" is the type
would it then be possible to replace the entire typeof parameter with for example Submission#0+Foo , if that is the type?
is like
not like
No
That is just the type's full name
not with the string "Submission#0+Foo", thats just a string
You need the full type instance
its not the real type
Pobiega
REPL Result: Success
Console Output
Compile: 669.731ms | Execution: 85.775ms | React with ❌ to remove this embed.
this works, but its kinda silly
so that would be like using the string value instead of the actual string in a method? which is why it wouldn´t work
its just saving the type in a variable before using it, instead of using it directly
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
Your name is Emelie, but you are not uniquely identified by "Emelie"
there are many other Emelies as well
aaand lets say there are a bunch of things which make me unique (hopefully lol)
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 etcwe could name all of those things together with a singular FullName, as "THINGS-THAT-MAKE-EMELIE-UNIQE"
like write it all out in a big string?
but its just the name of it... you have to access all of those unique attributes in some way
sure, we could. Thats called "serialization" in rprogramming
which is kind of how typeof works
no?
typeof is magic
its a part of the compiler
i mean, typeof is a way of accessing the thing that actually IS the thing, if that makes sense 😅
yes
thats exactly right
which is why I cant just use the name of the type
yes
well, then Im gonna continue on with my code!
Maaaany thanks, once again Pobiega!!
btw
ye?
Pobiega
REPL Result: Success
Console Output
Compile: 616.703ms | Execution: 82.806ms | React with ❌ to remove this embed.
use this instead 🙂
Enum.GetNames<Priority>();
is a better version of the same thingis the < > a replacement of typeof?
It's a generic
in this particular method, more or less
meaning, it indicates the same thing - we want the type?
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 compilerin this case im not sure it works?
It does
im doing a wpf app by the way, and cbChoosePrio is my combobox
GetValues(typeof(T))
and GetValues<T>()
are identical
they do th eexact same thingWell, what they return is identical
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
if i do this
I get : Cannot convert type 'type' to 'type'
well you changed
Values
to Names
if you want values, keep it values
Names returns the names as strings
but actually, looking at your code...it works now! when I changed to values
the only thing you do wiht the value is calling
.ToString()
on it
do you know what that returns? the name :pyeah the enum looks like this:
and the result I get with the foreach is this
thus, you could just write
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
if I pick "High" there, do you want to be able to get the string "High" out from the box, or
Priority.High
?that makes sense 😅 I actually followed a tutorial online, but something told me I was doing it too complicated with the tostring
note that
Priority.High
would not be a string, but a Priority
valueOh dear god
That's so convoluted 🙂
it is, right?
I had a feeling this was all to complicated
Yeah if you store the enum.value itself instead of the string version
You don't need to parse it back
yes, but this is the thing:
Yup?
In the assignment I have gotten, the Priority and TaskType Enums HAVE TO be a property of the Task class
That's okay
In fact, that makes sense
And I need to parse it back from a string, in order to set it
No you dont
Comboboxes can carry values directly
Not just strings
I dont understand 😅
carry values "directly"?
So look at where you add the priorities to your combobox
Can you paste that code again?
of course!
Right. What do you think
p.ToString()
does?it is the string representation of the p object?
It is indeed
and if you put your mouse to hover over
Add
it says it can add an object?
yep
so we can add anything
because everything is an object
so lets just remove
.ToString()
so it just adds p
hmm it actually looks just the same, you are right
yep, and then in your
btn_add_click
method
we cast to (Priority)
instead of (string)
Priority priority = (Priority)cb...
like this?
tada! we now have the priority, without tostring and parsing
yes
I must say, this looks a billion times smoother
right?
and it works the same way for the user, but its a bit faster for the computer 🙂
Its so easy to get the wrong ideas when looking online
absolutely
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!
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)
yes absolutely
actually, the readability part is way more important
Im actually going to show him this tomorrow 🙂
"Pobiega sends his regards"
* stabs teacher *
because I already told him I was really confused about all this parsing and his way of using enums
As your lawyer, I advice against actually stabbing the teacher.