Interfaces

Hi Friends,

In Today’s section, we’ll talk about Interfaces. Interfaces are basically constraints which Implementing class must follow. In one way, it is similar to abstract class but doesn’t act as base class. C# doesn’t have multiple inheritance. You can inherit from only one class but you can implement as many interfaces as you like. Also, one point to understand that access modifiers like private, public, protected etc are not legal in case of interfaces. Hence, without wasting time, let’s jump at demos.

 

Below is the simple interface in its plain form.

Now, let us go ahead and create one class which implements the same. Below is the default implementation for the same. You may also notice that since I am not having setter in interface, hence in Implementation that came as private setter which means value can be set from constructor.

Below is the modified code for class implementation.

Now, let us go ahead and use the same in main class. While writing the class, you can see available properties and methods to get exposed.

17th Below is the main class in its finished form.

Now, when I run the same, it will produce the following output.

20th

However, It is also perfectly legal to have a variable of type Interface and instantiate a class like shown below. But, while doing so, here I won’t be having method access which is declared and implemented explicitly in class.

18th

With the above change in place, it will print the following output.

19th

Also, we can have main method like shown below.

Here, I have used cast operator as to check whether the object is of type class or interface. And, this will produce the following output.

21th

Download Link:- https://github.com/rahulsahay19/InterfaceDemo

With this, I would like to wrap this session here. Thanks for joining me.

Thanks,
Rahul Sahay
Happy Coding

922 total views, no views today

Abstract Classes

Hi Friends,

Today in this section, we will extend Polymorphism by looking at abstract classes.

Abstract classes are concept wherein you will never make an instance of base class but derived class. Hence, when you make an abstract class, you also create abstract methods which must be overridden in derived class. Therefore, in a nutshell, Abstract class does the following things:-

  • Abstract classes can’t be instantiated
  • Must Derive from Abstract class
  • Abstract methods created must get overridden in derived classes.
  • Abstract class can also have non abstract methods with implementation
  • Purpose of abstract classes is clear. It has to create a contract which all the derived classes will implement.

Let us go ahead and see the demo around. Below, I have created simple Abstract class with abstract and non abstract methods to it. I have also used default and Parameterized constructor to set the values in there.

Below, I have created new class and simply implemented the same using VS. And, as a result, here is the default implementation for the same.

Now, let me go ahead and add few properties to it as well. While doing so, you can observe that base properties coming from abstract class.

13th

Here is the finished class.

Now, let us go ahead and create object for the same in the main class.

With the above change in place, when I go ahead and run the same, it will produce the following output.

14th

Below stuff is also very much legal. Although, we are having variable of type abstract class but we are creating an instance of derived class.

With this, It will produce similar output as shown below.

15th

However, Below shown scenario is not legal obviously.

16th

With this, I would like to wrap up this session. Thanks for Joining Me.

Download Code :- https://github.com/rahulsahay19/AbstractClass

Thanks,
Rahul Sahay
Happy Coding

1,235 total views, no views today

Method Overloading

Hi Friends,

In previous section, we have seen Inheritance and one kind of Polymorphism that is Overriding. In this section, we will delve further and see how Method Overloading works.

Method Overloading follows a set of rules. They are:-

  • Different Number of Parameters
  • Different Types of Parameters

One more important thing to understand here, signature of method means its name and parameter list not the return type.

Also, it’s important to understand difference between Overloading and Overriding.

  • Overloading means same method name with different parameters or types of parameters in the same class.
  • Overriding means virtual methods gets overridden in derived class.

Now, let’s look at the demo for the same. Below, I have added Overloaded method.

Now, let me go ahead and call the same from the main program as shown below.

With the above change in place, When I run the program, then it will produce the following output.

11th

Since, this method is virtual, hence I can override this as well in the derived class as shown below.

Then, in the main class.

12th

Which means, here method got overloaded and also overridden.

I hope you would have enjoyed this session. We’ll delve further in coming section. Till then, stay tuned and Happy coding.

Download Link:- https://github.com/rahulsahay19/csharp-Basics

Thanks,
Rahul Sahay
Happy Coding

2,180 total views, 1 views today

Practical Inheritance

Hi Friends,

In this section, we will talk about practical implementation of Inheritance. Inheritance basically explains IS-A relationship. We will see the same in a moment. Below, I have created simple Doctor class which inherits from DoctorInfo.

Now, the 1st thing which I would like to do with this class is to create a constructor as shown below. 3rd Continue reading “Practical Inheritance” »

1,007 total views, 1 views today

Getting Started with Reactive Extensions

Hi Friends,

In Today’s section, we will talk about Rx library. Basically, Rx is a library for composing asynchronous and event based programs using observable collections. This is very useful in the cases where in you are pulling data asynchronously from different sources and then manipulating the same and finally printing the result. In these kind of scenarios, you have to write lots of glue code and off-course, these codes will be error prone as one of the sources if just throws error, then what will happen?

This way, you really need to do lots of stuff for the things working fine. Hence, Rx is answer to this kind of situation, which keeps the thing simple yet lightweight. Rx also uses LINQ query on the observable collections.

But, it would be nice to talk little about collections before starting Rx.  IEnumerables are one of the most widely used Pull Based collection which is synchronous in nature. Below is the sample snippet for the same.

Continue reading “Getting Started with Reactive Extensions” »

1,826 total views, 1 views today

Getting Started with LINQ – Part 4

Hi Friends,

In today’s section we’ll delve further and see more of LINQ. Let us get started with Take and skip operations.

This will produce me the following output.

13th

Below is the same example while querying db.

14th

Now, let us see select many example.

15th

Cross product example.

16th

Cross product Join example:-

17th

Database cross Join:-

18th

19th

Filtered Association:-

20th

Joining the parent:-

21th

Outer Join – 1st:-

22nnd

Outer Join – 2nd

23rd

Thanks for joining me.

Thanks,
Rahul Sahay
Happy Coding

1,100 total views, no views today

Getting Started with LINQ – Part 3

Hi Friends,

In this section, we are going to delve further. We’ll first start with filtering process. Below is the snippet for the simple filtering process.

This will produce the following result.

8th

Now, let us look at complex style of filtering with multiple where clauses.

9th

Now, let us see the scenario where in we want to skip every 2nd item like shown below.

This will produce the below output.

10th

Let us now query against database. Here, i am reviewing against my movie review database. Here, i am referring Movies table as shown below.

11th

Now, let us consider a scenario where you want to query what IN and NOT-IN scenario as shown below.

12th

Thanks for joining me. We’ll continue same thing in another session. Till then stay tuned and happy coding.

Thanks,
Rahul Sahay
Happy Coding

1,120 total views, 1 views today