What is Constructor in Csharp

Constructor is the special type of method of a class which invokes automatically when instance of class is created.  Constructor is used to object initialization and memory allocation of the class. Constructor is used to initialize private field’s value of the class whenever instance or object of class is created. Constructor can be overloaded.

When we don’t create constructor for the class, the compiler automatically create a default constructor for the class. Constructor name is always same of the class name.

What is the difference between a class and a structure?


It is a reference type. When we create the object of a class, CLR allocates memory in heap. It contains constructor or destructor. Classes don’t support inheritance. We can assigned null value to variable of class.    


Structure is a value type.  For structure, memory allocates on stack.  It does not support inheritance.  Variable of structure can not assign with null values.  It does not need constructor or destructor because members of structure can be initialized automatically.

What is a class in C#?

Class is generic definition of what an object is . Class groups together all the attributes of object, as well as variables of other types, events and the methods that implements the behavior of member object. It means that class is a template of an object.  It is like blue print of object. If class is not declared as static class than it can be use it by creating the instance of class. Classes are declared by "class" keywords.

public class Category
     public int CategoryId { get; set; }
     public string CategoryName { get; set; }


What is base keyword in C#?

The base keyword indicates that the base class must be used and it allows to access to its constructors, methods and member variables. When we use base(), we can simply call the constructor of the base class and when we use base.Method(…) then we can call a method of the base class to pass parameter if required. And when we use base.field we can access or get the value of a member variable from the base class or simply assign or set a different one to it.

What is difference between Constant and Read only variables in C#

Constant Variable

Constant variable is a type of variable. It is declared and initialized the value at compile time. As name suggest, it is constant so value of the constant variable can’t be changed.

const keyword is used to declare the constant variable. By default constant variable is static, so you can’t define a constant type as static. Const keyword can apply to value type data type and reference type data type which can be assigned with a value null.

With Value Type 

const int x = 10;
const int y = 20;
const int z = x + y;
Console.WriteLine("The value of x :" + x);
Console.WriteLine("The value of y :" + y);
Console.WriteLine("The value of z :" + z);

//int a = 15;
//const int b = x + a;
//Console.WriteLine("The value of b :" + b); 
///	The variable 'a' is assigned but its value is never used


With Reference Type

const BaseClass objBase = null;
const BaseClass objBase1 = new BaseClass(); //It will throw an exception

'objBase1' is of type 'DemoCsharp.BaseClass'. A const field of a reference type other than string can only be initialized with null.


Read Only

It is declared and initialized the value at run time. The value of read only variable can be set/changed at the time of declaration or in the constructor of the class. readonly keyword is used to declare the read only variable. readonly keyword can apply to value type data type and reference type data type which initialized by using new keyword. You can’t make delegate and event as read only.

You can be declared readonly as static.

class Program
        readonly int x = 10;
        public Program()
            //changed the value in constructor
            x = 20;

Enjoy. Hope it will help you.


How to find second highest number from an array in C#

It is a very comman and tricky question which is generally asked during interview. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDemo
    class Program
        static void Main(string[] args)

        public static void FindSecondHightestNumber()
            int[] myarray = new int[] {2,4,9,13,7,3,5,0,12};

            //First Way
            var result = (from a in myarray
                          orderby a descending
                          select a).Skip(1).First();
            Console.WriteLine("Second Highest value is : " + result);

            //Second Way
            int _largestValue = 0;
            int _secondValue = 0;

            foreach(var val in myarray)
                    _secondValue = _largestValue;
                    _largestValue = val;                  
                    _secondValue = val;
            Console.WriteLine("Second Highest value is : " + _secondValue);



Pages : 1
Category Loading....
Recent Interview Questions
Popular Interviews Loading....
Popular Interview Questions
Popular Interviews Loading....
Follow Us