Learn about Enumerations and Structures in C# Programming

0
1148
C# (9) - More Complex Constructs

C# (9) - More Complex Constructs

In addition to all the C# variables we’ve seen so far, the language also offers a set of more complex variable types that are very handy in different situations: Enumerations, Structures, and Arrays. This article is going to address the first two types, while the next article will talk about Arrays.

Enumerations

Suppose your application is dealing with student grades. You have three different kinds of grades: Excellent, good, and fair. You want to store those grades not as separate string variables; but as one variable that can hold either of the grades. This will be handy if you want the user of your code to be confined to selecting only one of the grades you defined. You don’t want the user to assign a custom grade to a student, perhaps like “superb” instead of “excellent”. You can use enumerations for something like this. Enumerations (enums) allow you to define a custom user-defined variable that has a specified range of possible values. Consider the following example:

Notice that we are defining the enum data type before the main function. Then, could use the grades data type as simple as we use any other data type like a string or an integer. The only difference here is that we are not allowed to assign any other value to myGrade except the ones defined in the enumeration.

Internally, the enum data type you’ve just created are stored in integer format by default. That is, the first item (Excellent) is 0, the second one is 1 and so on. You can force the enumeration to store variables in a specific numbering order as follows:

You can also change the underlying type that the enumeration uses to store the items internally. Enums support byte, sbyte, short, ushort, int, uint, long, and ulong. So you can declare a specific type like so:

Sometimes you want to assign multiple items to the same value. Consider the following example:

Here both Excellent and Superb are stored in the same value 1.

Note
Two common uses for enumerations are to represent the days of week, and Months.

Learn Cloud Computing from Scratch for Beginners

Structures

While enumerations are used to store data of the same type, structures (structs) are used to store data of different types again in the one user-defined variable type.

Let’s get back to our students application. You want to store various student data in one entity. For example, a student called Sara Longman will have a first name, last name, age, address and maybe other data. You can store each of this data in a separate value, like this:

Imagine how hard would it be if you want to get her age when she’s among 100 students, each has 3 variables. That’s 300 variables. A better way to do this is to store this information in a structure as follows:

Observe how concise the data became when it’s all related to the same entity: myStudent.

You can access any of myStudent data using the dot format: myStudent.firstname (will output “Sara”). It’s worth noting that the member variables of the struct retain their original data type. That is, myStudent.age is an integer, myStudent.firstname is a string and so on.

Conclusion

In this article, you had a look at some slightly more complex types of variables that C# offers. You examined enumerations, and structures, and saw where and how each of them could be useful.

In the next article, we will start the discussion of arrays.

I hope you enjoyed reading this article. Stay tuned.

LEAVE A REPLY

Please enter your comment!
Please enter your name here