OOPS In C# – Inheritance Part 6

Scenario:-
Function declared as virtual in base class and overridden in child class. See the sample code below:

  public class MyClass
    {
        public virtual void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public override void foo()
        {
            Console.WriteLine("Child version of foo");
        }

    }



Continue reading

OOPS In C# – Inheritance Part 1

Inheritance:-

Inheritance basically provides two models. Reusability model and extensibility model. Reusability model was there in C also through header file and in VB since Visual Basic is an object based language. An object based language is the one where in we can use the objects or components or modules as is but can’t modify or extend their behavior.
Interview Definition: – Extending or enhancing the functionality of base class.
In inheritance few terms or concepts that needs mentioning are hiding and overriding.
Method or Data Hiding: – Having data member or function in child class having same signature as that in base class. This is not an error but compiler shows you a warning messages when you have same data member in both child and base stating that you are hiding the base class implementation if hiding is intended use new keyword. So by specifying new keyword with data member and functions you can hide base class’s implementation of data or function. This will be clearer with an example later on. Continue reading

OOPS In C# – Encapsulation – Abstraction

Introduction


OOPS or Object Oriented Programming Concepts but some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation
2.) Abstraction
3.) Polymorphism
4.) Inheritance

Encapsulation:-


Interview Definition: – Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g. Classes and Struct
Bu using encapsulation we can create our own custom types by reusing the existing or primitive types.
Continue reading

C# 6.0 New Features: Methods with Expression Body

Since its release, C# has travelled a long journey from being a imperative language to a declarative one. With this journey the way we use to write methods have also changed a lot. Lets try to understand the journey of a method from C# 1.0 to 6.0
This is how we use to write methods in C# 1.0 .These were called named methods.Also you can see how a delegate is using a named method to print a value to console.

Continue reading

C# 6.0 New Features: Async in Catch and Finally Blocks

The most common programming practice is to catch exceptions and log them. Logging involves IO since we usually log into files or database. IO operations are usually expensive in terms of performance which means that in a serviced kind of environment, the end user who is waiting for a response is actually being kept waiting for logging messages as well.

Continue reading

C# 6.0 New Features : Importing Static classes via USING keyword

Static in C#:
Static is a compiler reserved keyword in C#, which can be applied to any member of a class i.e. well Fields, methods, classes, Properties, Events or to a class itself as well.
There will be only one copy of a member variable declared as static. Each object has its own copy of instance variables whereas static variables are shared across objects.

Continue reading