Introduction to Enums in C#

Hello friends, I hope you all are having fun. In today's tutorial, we will have a look at detailed Introduction to Enums in C#. It's our 23rd tutorial in C# series and this C# concept is quite easy one. It's normally used to give better meanings/understanding to your project. If you haven't studied lectures on C# Classes & C# Methods, then do read them first, as we are going to use them today. So, let's get started with Introduction to Enums in C#:

Introduction to Enums in C#

  • Enums in C# ( short for Enumerations ) is a value type datatype for constants, created by using keyword enum and can be controlled by the class Enum. ( keyword with small e, while class with capital E )
  • We can declare & implement Enums directly in C# Namespace, class or struct and the underlying type of an enum is integer.
  • Enums are strongly typed so we can't implicitly convert them into any other Data Type. Although we can use explicit conversion to convert enums into integers etc. ( We studied it in Data Types Conversions and we will also look at it shortly )
  • Let's understand this enum concept with an example, suppose we are working on some scheduling project and we need to print names of all days.
  • As we know these names are constants, and are also occurring in a sequence, so we can create enums for them, as shown in figure on right side.
  • There are 3 ways to create Enums as you can see in the figure.
  • In the first enum declaration, I have simply added the data in the enum WeekDays. Although, I haven't added any index but still it got the index starting from 0 and then incremented automatically by 1, thorough out the enum, as its underlying type is integer.
  • In the second enum declaration, I have added the index in front of the data, so we can do that as well. Although, there's no difference between first & second implementation, its just second one displays the hidden values.
  • In the third enum implementation, I have assigned random values to constants, so we can do that as well. Instead of values in a sequence, we can assign random values to these Constants.

Why use Enums in C# ?

  • You must be thinking, why we need to use Enums, we can remember such things and can easily replace them with integer checks.
  • Yeah we can do that in simple project but in complex projects and especially in those projects where you don't have a sequence, enums play an important role.
  • Let's say you need to use Elements of Periodic Table in your project, there are too many elements, so in such cases, enum comes quite handy, you can place these elements in right order in your enum and then can use them easily though out your project.

How to use Enums in C# ???

  • Now let's have a look at How to use Enums in C#, so I am creating a simple code where we will check whether it's holiday or working day using enums.
  • As shown in below figure, I have created an Enum named WeekDays and this enum is public so we can use it in other classes.
  • Moreover, enums are value type so, we can't instantiate them in other classes.
  • You can see in above figure that in Main Function, I have created a new variable CurrentDay of Data Type WeekDays, as enum is a Data Type.
  • We can't assign any other value to this variable, we can only assign members of its enum to this variable.
  • In the IF Loop, I have used the members of enum in my conditions to verify.
  • So, now you can see the code has become more Readable & we can easily maintain it without remembering anything.
  • We have studied enum keyword so far, now let's have a look at class Enum:

How to use class Enum in C# ??

  • In Visual studio, you will also find Enum class along with enum keyword. (enum keyword starts with small e while class Enum starts with capital E )
  • We can use this Enum class to get different properties of our enums i.e. we can get their Constants or can also get their underlying integer values.
  • We can also search for any keyword in the Enum, whether it exists or not.
  • I have performed all these three operations in the code, shown in below figure:
  • So, you can see in above figure that we can get Names & Indexes of our enums using this Enum class.
  • I have first used the class name Enum, then dot operator and finally methods from Enum Class.
  • We can also change the underlying type of our enum using ( : ) colon operator.
  • In the right figure,I have changed the underlying type of my enum from integer to short.
  So, that was all about Enums in C#. I hope you understood enums & their importance. In next lecture, we will haveĀ  look at Attributes in C#. Till then take care & have fun !!! :)

Introduction to Access Modifiers in C#

Hello friends, I hope you all are having fun. In today's tutorial, we will have a look at Introduction to Access Modifiers in C#. It's our 22nd tutorial in C# series and now it's time to have a look at access modifiers, which we have to use a lot in C#. I hope that you have already studied previous lectures i.e. C# Methods, C# Structs, C# Classes etc. as these access modifiers are used with them. So, let's get started with Introduction to Access Modifiers in C#:

Introduction to Access Modifiers in C#

  • Access Modifiers in C# are used to apply restrictions on the accessibility of C# Objects within or outside the project, and are defined in the declaration.
  • There are 5 Access Modifiers available in C#, whcih are:
    • Private
    • Public
    • Protected
    • Internal
    • Protected Internal
  • We will have a look at all of them, in detail but before going any further, we need to understand the difference between Types & Type Members.
  • All those C# objects, which can contain members in them as called Types i.e. C# Classes, Structs, Interface, Enums etc.
  • While the members of these types are called Type Members i.e. C# Fields, C# Properties and C# Methods.
  • All the C# Types can only be either Public or Internal i.e. we can't make a class private. ( We will discuss it in detail shortly )

1. Private - Access Modifier in C#

  • Private Access Modifier restricts the C# Type Members to be used in the containing class only. ( Containing Class means, the class in which they are created )
  • We can't use Private Members in external classes, as shown in the figure on right side.
  • I have created a Private variable in a new class and now when I am trying to access this variable in Main function, I am unable to do that.
  • In the IntelliSense, there's no property/option to use this private variable.
  • Although, we can use this variable Number1 in HelloWorld class quite easily.

2. Public - Access Modifier in C#

  • Public Access Modifier removes all restrictions from C# Objects and these objects become available in any assembly or project.
  • In order to use Public Objects, we need to follow the proper way i.e. we need to instantiate in order to get the Public Property.
  • In the figure, you can see I have created a public variable Number1 in a new class and then I have assigned a value to it in my Main Method.

3. Protected - Access Modifier in C#

  • Protected Access Modifier restricts the C# Type Members to use only in containing classes or inherited classes.
  • We can't use Protected Members in any independent Class or Struct, it has to be inherited from the Containing Class.
  • Here's an example of Protected Access Modifier in action, and you can see that I can assign value to Protected variable Number1 in the inherited class HelloWorld2.
  • But the independent class Program can't access this Number1 variable.

4. Internal - Access Modifier in C#

  • Internal Access Modifier restricts the C# Objects to be used in the current assembly only.
  • In complex projects, we need to add multiple assemblies i.e. different add-ons and plugins etc.
  • So, Internal variables can be used in the current assembly / project only, we can't access it in external assemblies.
  • We will look at them in detail, in our coming lectures.

5. Protected Internal - Access Modifier in C#

  • Protected Internal Modifier allows the C# Type Members to be accessed in the containing assembly as well as in those classes ( of external assemblies ), which are inherited from the containing class.
  • When we will be done with these basic concepts in C#, then we will design few complex projects and then you will get better understanding of these access Modifiers.
So, that was all about Access Modifiers. In the coming lecture, we will have a look at Enumerations in C#. Till then take care & have fun !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir