C
C#3w ago
mich4s

✅ why is the code so short?

so i have a piece of code:
public enum UserStatus
{
Active,
Inactive,
Suspended
};
public enum UserStatus
{
Active,
Inactive,
Suspended
};
thats a whole file and i dont know why it is so short, its in a folder called "Enums" and in file called "UserStatus", im providing a screenshot for better understanding of the whole project, but for example the file "SampleInterests" has all of this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace klasy.Data
{
internal class SampleInterests
{
public static List<string> Interests = new List<string>()
{
"Programowanie",
"Gotowanie",
"Granie",
"Podróże",
"Sport",
"Muzyka"
};
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace klasy.Data
{
internal class SampleInterests
{
public static List<string> Interests = new List<string>()
{
"Programowanie",
"Gotowanie",
"Granie",
"Podróże",
"Sport",
"Muzyka"
};
}
}
so why is the 1st file different from the 2nd one, can someone explain? why is the 2nd one in a class but the 1st one not?
No description
24 Replies
mich4s
mich4sOP3w ago
please mention when reply so i can see
mg
mg3w ago
@mich4s because one is an enum and one is a list of strings
mich4s
mich4sOP3w ago
so enum just doesnt need anything and a list needs a class?
mtreit
mtreit3w ago
I'm not really sure I understand the question.
mg
mg3w ago
there's not really a meaningful comparison you can make between them
mtreit
mtreit3w ago
Do you know what an enum is?
mich4s
mich4sOP3w ago
yeah
mg
mg3w ago
what is it
mich4s
mich4sOP3w ago
idk how to say it in english but its basically a set of values like in my example active is 0 cuz thats default starting value, inactive is 1 and suspended is 2 we usually used it in switch statements
mg
mg3w ago
right it's a set of named constants what's a list?
mtreit
mtreit3w ago
If you're asking why dedicate an entire source code file to a tiny enum, the answer is that you don't have to do that. C# used to have a pretty heavily followed convention of "one file per type" but I think people have since realized that's a bit silly.
mich4s
mich4sOP3w ago
idk how to explain it like a dynamic data storage of the same type in my example string no set length of the list its similar to an array if i remember correctly
mg
mg3w ago
pretty much so what's your question? "why is the enum so short" well, because it can be
mich4s
mich4sOP3w ago
okay so what about the 2nd file why does it need a class
mg
mg3w ago
why does what need a class
mich4s
mich4sOP3w ago
my list
mtreit
mtreit3w ago
C# does not have true global state. Everything is attached to a class.
mg
mg3w ago
^
mtreit
mtreit3w ago
If you want a list of something it has to be part of some class somewhere.
mich4s
mich4sOP3w ago
okay
mg
mg3w ago
"everything" meaning everything that is not itself a type, which an enum is
mtreit
mtreit3w ago
If you are confusd why this is a valid C# program:
Console.WriteLine("Hello, World");
Console.WriteLine("Hello, World");
...when there is no class to be seen, that's because the compiler will generate one for you. It's special and only applies to the Main method entry point of your program.
mich4s
mich4sOP3w ago
okay thanks
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?