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:
- Single inheritance
- Multi-level inheritance
- Multiple inheritance - not supported
- Hybrid inheritance - nt supported
- Hierarchical inheritance
Output:
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.
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.
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.
Output:
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.
When you try to run the above code, you will get the following Compile Time Error.
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
- Compile Time Polymorphism / Static Polymorphism
>Method overloading
>Operator overloading
The compiler identifies which method is being called at the compile time.
************************************ - Run-Time Polymorphism / Dynamic Polymorphism
>Method Overriding
The method that is called is determined at the runtime not at compile time,
We can usevirtual
andoverride
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
andoverride
with methods of the base class and derived class respectively. Here,virtual
- allows the method to be overridden by the derived classoverride
- indicates the method is overriding the method from the base class
In this way, we achieve method overriding in C#.
Abstraction can be achieved with either abstract classes or interfaces
Comments
Post a Comment