C# basics Interview Questions

 List is used to collect items that usually consist of elements of multiple data types. An array is also a vital component that collects several items of the same data type. List cannot manage arithmetic operations. Array can manage arithmetic operations.

https://www.c-sharpcorner.com/article/how-to-insert-an-element-into-an-array-in-c-sharp2/#:~:text=You%20can%20insert%20an%20element,are%20examples%20of%20both%20methods.

https://www.c-sharpcorner.com/UploadFile/mahesh/insert-item-into-a-C-Sharp-list/



Inheritance 

It supports the concept of code reusability and reduces the length of the code in object-oriented programming.

Types of Inheritance are:

  1. Single inheritance
  2. Multi-level inheritance
  3. Multiple inheritance - not supported
  4. Hybrid inheritance - nt supported
  5. Hierarchical inheritance



 In Inheritance, the Child class can consume members of its Parent class as if it is the owner of those members (except private members of the parent).

using System;
namespace InheritanceDemo
{
class A
{
public void Method1()
{
Console.WriteLine("Method 1");
}
public void Method2()
{
Console.WriteLine("Method 2");
}
}
class B : A
{
public void Method3()
{
Console.WriteLine("Method 3");
}
static void Main()
{
B obj = new B();
obj.Method1();
obj.Method2();
obj.Method3();
Console.ReadKey();
}
}
}
Output:

Inheritance in Object-Oriented Programming Language with Examples


 if you are defining an explicit constructor, if you make that constructor private, and if you don’t provide an access specifier, then by default, the class member’s access specifier is private in C#. For example, modify class A as follows. As you can see, we have removed the access specifier from the constructor which makes it private.

class A
{
A()
{
Console.WriteLine("Class A Constructor is Called");
}
public void Method1()
{
Console.WriteLine("Method 1");
}
public void Method2()
{
Console.WriteLine("Method 2");
}
}

As you can see in the code, the Class A constructor is private, so it is not accessible to Class B. Now, if you try to run the code, you will get the following compile-time error as shown in the below image which tells Class A Constructor is inaccessible due to its protection level.

Inheritance in C# Object-Oriented Programming Language with Examples

We are getting the above error because when we create an instance of the child class, the child class constructor will implicitly call its parent class constructors. Right now, the Class B constructor trying to call the Class A constructor, which is not accessible because that constructor is private.

Let us do one more thing. Let us define a constructor in Class B as follows. Let us make the class A constructor public; otherwise, the inheritance would not be possible.

using System;
namespace InheritanceDemo
{
class A
{
public A()
{
Console.WriteLine("Class A Constructor is Called");
}
public void Method1()
{
Console.WriteLine("Method 1");
}
public void Method2()
{
Console.WriteLine("Method 2");
}
}
class B : A
{
B()
{
Console.WriteLine("Class B Constructor is Called");
}
public void Method3()
{
Console.WriteLine("Method 3");
}
static void Main()
{
B obj = new B();
obj.Method1();
obj.Method2();
obj.Method3();
Console.ReadKey();
}
}
}
Output:

Inheritance in Object-Oriented Programming

As you can see in the above output, first Class A constructor is called, and then the Class B constructor is called. So, the point that you need to remember is execution always starts from the Parent class constructor. Why? When we create an instance of a child class, the child class constructor will implicitly call the parent class constructor. If that Parent class has a Parent class, then that Parent class constructor will call its Parent class constructor, and so on. Suppose you have 5 classes in inheritance, and if you are creating an instance of the 5th class, then the 5th class constructor will call the 4th class constructor, and 4th class constructor will call the 3rd class constructor and the 3rd class constructor will call the 2nd class constructor and 2nd class constructor will call the 1st class constructor. So, the execution, in this case, will start from the class 1 constructor, then the class 2 constructor, and the last constructor, in this case, will be the 5th class constructor.

In inheritance, the child class can access the parent class members, but the parent classes can never access any members that are purely defined in the child class.

static void Main()
{
A obj = new A();
obj.Method1();
obj.Method2();
//The following line of code gives you compile time error
obj.Method3();
Console.ReadKey();
}
}
}

When you try to run the above code, you will get the following Compile Time Error.

In C#, child class can access the parent class members but the parent classes can never access any members that are purely defined in the child class


C# Polymorphism

Polymorphism is one of the features provided by Object Oriented Programming. Polymorphism simply means occurring in more than one form.

That is, the same entity (method or operator or object) can perform different operations in different scenarios.

Example of Polymorphism

using System;
class Program
{
// method does not take any parameter public void greet() { Console.WriteLine("Hello"); }
// method takes one string parameter public void greet(string name) { Console.WriteLine("Hello " + name); }
static void Main(string[] args) { Program p1 = new Program(); // calls method without any argument p1.greet(); //calls method with an argument p1.greet("Tim"); } }

Output

Hello
Hello Tim


Types of Polymorphism

  1. Compile Time Polymorphism / Static Polymorphism
           >Method overloading
           >Operator overloading
    The compiler identifies which method is being called at the compile time.
    ************************************
  2. Run-Time Polymorphism / Dynamic Polymorphism
                      >Method Overriding
     The method that is called is determined at the runtime not at compile time,
    We can use virtual and override keywords to achieve method overriding.

    Let's see the example below,

    using System;
    class Polygon
    {
    
    // method to render a shape public virtual void render() { Console.WriteLine("Rendering Polygon..."); }
    } class Square : Polygon {
    // overriding render() method public override void render() { Console.WriteLine("Rendering Square..."); }
    } class myProgram { public static void Main() { // obj1 is the object of Polygon class Polygon obj1 = new Polygon(); // calls render() method of Polygon Superclass obj1.render(); // here, obj1 is the object of derived class Square obj1 = new Square(); // calls render() method of derived class Square obj1.render(); } }

    Output

    Rendering Polygon...
    Rendering Square…

    In the above example, we have created a superclass: Polygon and a subclass: Square.

    Notice, we have used virtual and override with methods of the base class and derived class respectively. Here,

    • virtual - allows the method to be overridden by the derived class
    • override - indicates the method is overriding the method from the base class

    In this way, we achieve method overriding in C#.

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces

Comments

Popular posts from this blog

How are software engineers earning salary packages of 25 LPA in India?

Highest Dividend paying Indian Stocks NSE - 2021 Analysis

Creating Hotspot Using CMD