In the previous discussion we discussed about the Generic Collections and its types. Also where they are used and what are the different types of Generic collection interfaces.

Moving on to some of the other important concepts, where these concepts are easy and used in our regular programming. But how do we understand them and use them accordingly is of utmost importance.

Reflection:

Now if we try to understand the general meaning of reflection, we can tell that it is a process where something is thrown back to us without absorbing like the mirror. That is when we go in front of the mirror we can see our face as the mirror is not absorbing our face and just reflecting it.

So now what do we mean by reflection in C# .NET?

According to the technical definition Reflection helps us to inspect or look into the contents of an assembly or class. It means that it helps us to look into the Meta data of an assembly or a class, where Meta data means the methods, properties, functions of that class or assembly.

Now this reflection concept is widely used in our visual studio IDE(Integrated Development Environment) or we can say visual studio software.

So for understanding this we take a simple example where we can say that,

class Programming
{
        Static void Main(string[] args)

          {
               string y;
                y.Add
           }
}

Now here we have defined y variable as string. Then in the next line of code we have written y.Add that is we have given the y variable with Add method. Now when we are writing the code in visual studio, it happens that as we write y then put a dot in front of it, it will show us all the methods, properties, functions, etc. or we can say meta data. This thing which shows us all the methods, properties, functions, etc. of the class is known as reflection, because as we have defined the class the reflection will inspect all the meta data related to it and then shows us back. The below picture of the reflection will give us clear idea.

ash

As the definition said that reflection helps us to inspect the meta data of the class or assembly, so here after x variable when we put dot the meta data is shown (which is in red circle). It shows all the methods, functions, properties, public, private methods, etc. Reflection also helps us to invoke the value of the function or method

Thus we can understand the concept of Reflection.

Sothe next topic which is easy, used in regular programming but still difficult to understand conceptually.

Strongly Typed:

Strongly type is the concept in the programming language which tells us that once the data type or the class or any method is defined, we have to strictly follow that data type or method only. Hence they are termed as strongly typed.

Now programming languages can be strongly typed, here C# is a strongly typed language.

So when we write some code where we define some data type or method, we are following that data type or method and if any errors are occurred they are checked during the compilation of code itself. It does not wait till runtime to check errors. We here take a simple example where

int i = 123;

This will not show any errors because we have defined the int data type and we have provided its value as numeric. Now,

int i = “ABC”;

It will show error here during the compilation of code because we have defined int data type and we are providing its value as string.

So the error is shown at the compile time itself so that we can correct it, secondly we can put the values according to the defined data type itself and not any random values.

The advantage of strongly type is that here boxing unboxing does not happen due to which the performance improves.

Dynamic Type or Loosely Type:

Dynamic type or loosely type we can say that it is opposite of strongly type. Technically we can define it as the type where after defining the data type or the method, if we do not follow and put any random value the error will be shown at runtime. This is also a concept in programming language. But unlike strongly type they perfectly run during compile time and show the errors in the run time.

We can say that JavaScript language is a dynamic type.

Now when we write a code defining some data type or method and write its values as random it will check for errors at run time and if no errors are found then it will run properly. But if errors are found it will immediately show the errors at run time.

Here due to the random values of data type boxing unboxing occurs which decreases the performances.

To use dynamic type in the program we use “Dynamic” keyword. A small code sample is shown here:

dynamic string = “ABC”;

string.update();


Here it will show the message as 
 ash1

This means that whatever will be the value it will be resolved at run time.

Now there is an important concept of Object Oriented Programming which is confusing a lot of times and many a time the question arises that “How can we achieve multiple inheritances?”  Therefore we will try to understand what the answer to the question is.

Multiple Inheritances:

We know the concept of inheritance which defines the parent child relationship between the classes and objects. So just think of this happening multiple times in a program code of our project.

Now the multiple inheritances can be achieved through the interfaces.

As we know the concept of interface which states that interface acts like a contract between two or more program codes. So how this interface helps us to achieve multiple inheritances?

Now suppose we create a parent class and its child class, and then we apply interface with the parent class in which some method is defined. Now it happens that in our real projects there are many things which we need to be added in our program code and also secure it with a contract so that no changes can be made.

So now what we can do is that we do not break the original contract or the original interface because once an interface is defined it cannot be changed, if need to add some changes we need to create a new interface. And that is what we do to achieve multiple inheritances. We understand this concept by looking at a simple sample code.

namespace MultipleInherit
{
Interface Method1
  {
        int add(int a, int b);
  }
Interface Method2
   {
        int sub(int a, int b);
    }
class Calculation : Method1, Method2  
{
public int result1;
public int add(int a, int b)
        {
return result1 = a+b;
         }
public int result2;
public int sub(int a, int b)
        {
return result1 = a-b;
         }
}
Class Maths
{
   Static void Main(string[] args)
     {
          Calculation c = new Calculation();
           c.add(1,2);
           c.sub(7,4);
     }
}

Here we see that firstly two interfaces are created with add and subtract methods. Then a Calculation class is created which is parent class and then the two interfaces are inherited to this class. So now what happens is that if we want to use the addition method we can use it, if we want to use the subtraction method we can use it. Both can be used separately because add method or subtract method can be invoked through their respective interfaces.

We can say that Add method can be looked through the binoculars of interface Method1. While looking through the Method1 we only see Add method nothing else. Same can be said for the subtract method. In the above code two individual interfaces were created with their individual methods and then they were inherited to the Calculation parent class.

So in this way we can say that if we want to add a new method to the existing parent child relationship we have to create a new interface and define the new method in it. In this way we can say that multiple inheritances can be achieved through interfaces.

 

 

 

Advertisements