Complex Data Structure and Some advance concept in C#
In my first post, I introduce C#, some tools to use for developing C#, and primitive data types. I also mentioned the difference between C# and .NET. If you want to go and see that post, please click on this link: https://medium.com/@ibrahimyengue/introduction-to-c-f57d576803c9#.pnvu513wf
For this post, as I mentioned in the first post, we will talk about complex data structure and some concepts of object oriented programing in C#.
Complex Data Structure in C#
In C#, we do have many types of complex data structure, such as Arrays (same as we have seen in java), Enumeration, and Structure.
What is an Enumeration (enum)?
An enumeration is a user-defined data type that enables a developer to create a variable with a fix set of possible values.
For instance: since there are only seven possible days of the week, we can use an enumeration to define the day of the week.
How to create an enumeration?
The following example shows the syntax of creation an enum called Day, which holds the days of the week.
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
By default enum values start at 0 and each successive member is increased by a value of 1. As a result, the previous enum ‘Day’ would contain the values:
- Sunday = 0
- Monday = 1
- Tuesday = 2
- Wednesday =3
- etc…
How to access the elements contained in an enum?
Let’s see how to access Saturday for instance.
Day favoriteDay = Day.Saturday
Good to know:
Using enumeration, improve code readability and manageability. It is a good practice to define enumerations inside a namespace so each class inside that namespace can access the enum.
Structure (struct)
A struct is another user-defined type in C#, which holds a set of variables. It is a lightweight class and has a limited functionalities than a class. Some of the limitations are:
· A struct cannot declare a default constructor. This is done by the CLR (Common Language Run-Time)
· A struct cannot inherit another struct or class, and it cannot also be a base for another struct or class; however, it can implement an interface.
How to declare a struct?
To declare a struct, use the key word “struct” followed by the name of the struct, and inside the curly braises, declare the variables as you can see below:
In this example, Books is the name of the struct which contains four elements.
How to access elements in a struct?
To access the elements, we have to instantiate the struct by using the key word “new”, as we do with classes in java, or without the key word “new” as shown in the example bellow.
Good to know:
It is recommended to use the key word “new” while instantiating a struct if the struct declares a constructor (which can only be a constructor with parameters). The reason is that the CLR will allocate a block of memory location for the elements while if the new key word is not used, the memory location for the elements might not be in the same location.
Event thought we cannot declare a default construct for a struct, if we don’t declare a constructor with parameters, the CLR creates a default constructor at run-time, which is the case in the example above.
Declaring a constructor in C# is the same as declaring a constructor with parameters in java, so there is no need to go over it.
Now let talk about some concepts of object oriented programing in C#, and some more advanced concept. I want to talk about “Properties”.
What do we call properties in C#?
As we have learned in our security course, while developing a software, we have to make it secure. One way to do that is to define attributes as private. To access those private fields, we need public methods. We can define those method for read only, for write only, or for both.
Properties are what we call access modifiers in java. The difference is that in java the getter and setter are two different methods for the same field while in c#, the get and the set properties are in a same method conventionally named with the capitalize name of the variable to access.
In the example bellow, the class student contains the property “Firstname”. The get returns the firstname while the set assigns the firstname the key word “value”.
The example bellow shows how to use the property define in the Student class.
Now let talk about Inheritance, which is one of the pillar of object oriented programming in general and object oriented programming in C# in particular.
Why do we use inheritance?
Inheritance enables developers to create new classes that reuse, extend, and modify the behavior of existing classes.
In C#, the base class is the class whose members are inherited (super class in java), and the derived class is the class that inherits from the base class (sub class in java).
How to define inheritance in C#?
In C#, we use colon to define an inheritance as shown below where we have two classes (Person and Student). In this example, Student inherit from Person and can access all the attributes and methods inside the Person class by using the key word “base” (super in java).
Person is the base class (super class in java).
Student is the derived class (sub class in java).
How the derived class can override a method from the base class?
To override a method, we have two options. One is to add the new key work in the definition of the override method, or to add the key word override to the override method, and then declare the overridden method as virtual by using the key word “virtual” in the definition of that method. For more information, I suggest you to visit the MSDN on this link: https://msdn.microsoft.com/library/ebca9ah3.aspx
This bring us to the end of this post. Thank you for reading my post, and I hope that you have learned from it.
I could not cover everything I wanted to cover in this post, but since I have two more posts, we will see more interesting concepts in C# in the coming posts, and also in my next presentation. If you have questions, you can ask me any time.
What coming next?
· Event
· Delegation
· Composition
· more