ā instanciable enum?
i know C# has all these cool tricks, does it have enum instances? i know it sounds dumb, but i mean like Walking sprinting and sneaking have diff speed values for each creature but each creature only has walk sneaking and sprinting making a single enum perfect
53 Replies
Can you give an example? Enums already have instances, one for each value in the enum.
well yes, but what i mean is more like, you have an enum called movement, which holds Sprint, Walk and Sneak, with values Sprint, 5
Walk 3, Sneak 1
there is also a cheeta which has the same enum but Sprint 30 Walk 15 and Sneak 3
and im wondering if there is a neat way of diong that
im quite new to C# and so far im amazed with all the neat little things it has
Sounds like you want a class more than an enum
i guess a switch would work too but
well i already have a class
XD
the thing is i could make a class but then i would have to create seperate instances for sneaking sprinting and walking
enums are a compiler trick for the underlying type, so no they can't have different values.
You could have a
Stats
class, sort of like this (using a record for brevity):
Then when creating a cheetah you could give it a new Stats(3, 1, 5)
.oh C# has records too
nice
and structs
i mean ye that would work then i guess
Well depends on what you're comparing them to
i knew the structs
but i dont see me using structs soon
i guess they are just an immutable map
records are just sugar for classes and structs
u can assign values of walk, sneak and print in a struct instance
That would be more like an
ImmutableDictionary
sorry java mind, its called a dictionary here
XD
i was thinking of the same thing xD
Also, C#'s enums aren't really as powerful as Java's
In C#, enums are essentially just integers with names.
oh
š¦
so they arent their own type?
well yes they are
However the underlying data of an enum is just a number
So they can't store any other data
WAIT you cant have like a constructor in the enum like in java?
nope
Java patented that so now no other languages can use it
bruh actually?
thats so dumb T-T
However, we are hopefully getting proper discriminated unions in C# soon-ish
idek what that means
applying attribute on an enum value is the closest thing u can get probably
okay but i still need a value thats like 1 value holding multiple things
essentially what Java has but even more powerful
a class is so bulky tho
$unionsdiscussion
GitHub
[LDM] - Union Types Ā· dotnet csharplang Ā· Discussion #7010
The following is a summary of discussions held on discriminated unions in C# by members of the C# language design team. It is not a proposal, and it is not the actual meeting notes. Union Types Uni...
union, like in c++?
enums are static?
or not
union, like in F#
never did F#
look up discriminated unions
yes-ish
can i have an enum that gets its values from a variable? so you can set it in the unity editor
well i guess if the enum shows up that would work too but i dont think it does
oh this is Unity? forget about records then
WHAT
wdym
but records are cool
no, enum values are constants
well ye i know but then what do i use
It's a feature from a language version ahead of what Unity uses
i dont have to change the enum after pressing play, i just want to be able to change it in the unity editor
bruh
well its imposible to do anythign without unity
No idea if you can do that. You'd probably have to ask in $unity.
oh
wdym
well im not gonna build a full on 3d renderer and game enigne
There are other engines which use C#
wait whats the difference between a class and a struct? sorry for these dumb questions but you dont have structs in java
it's not officially supported but you can still use it by adding the namespace manually
System.Runtime.CompilerServices.IsExternalInit
structs are value types, classes are reference types
$refvsvalue
ye i created a new thingy for it
but i understand
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.