What do I learn – Week 23

This week I study more deeply how the different variable types exist in C#, how they work, and how to implement them in the code. 

Classes – Interfaces and Inherence 

The pillars of the OOP are polymorphism and inheritance, for achieve this it is important to know how to implement the interfaces and how to inherit the properties and methods from other classes. 

Inherit means to “copy” all the methods and properties from a base class, but the data access types can make some of this information unavailable. 

Remember that when something is marked private, only the class can access the property or method, but if you make it public, you can access that information anywhere in the code. 

This is when the word protected makes a change here, denoting that the property or method can only be accessed by the class and the classes that inherit from that class. 

Another important point to have in account is that C# is a mono-inherit programming language, this means that classes can only be inherited from 1 class. 

In code the inherit looks like this: 

class MyClass : OtherClass {} 

Another story happens with the implementation of interfaces, they are pretty simple to use, they only require a name (commonly they start with a capital I), and inside the methods that are mandatory to implement in the class. 

The syntax for the interfaces is: 

interface IFile() {} 

Var and Dynamic Types 

Another interesting quality of C# is that there exist 2 types of variables that can make things a little easier (or harder) depending on how you use it. 

One of these tools is the type value ‘var’, which automatically detects the correct type of value that your variable needs. 

This is a powerful shortcut if you don’t know the exact return type of a function, but also you have to keep in mind that you need to assign an initial value every time you use ‘val’. 

On the other hand, there is the ‘dynamic’ type, which changes every time that you need it, you can assign first an integer type and then a string. 

This functionality is like the variables in JavaScript. 

Enums 

Sometimes it is easier to assign numbers to words for simplicity. 

But keeping track of what each number means can be tricky. 

For example, the days of the week, if we take that the week starts on Monday, we can assign a value in the code for every day in the week. 

Monday 0, Tuesday 1, Wednesday 2, and so on. 

Well, there is a way to make this in code, and it is called Enums

It is a value that you can name the way that you need, and easily you can transform the strings into numbers and vice versa. 

It is just to make things a little more organized and with a number hierarchy. 


Leave a Reply

Your email address will not be published. Required fields are marked *