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