c# most important interview questions

1>  What is Interface 

->Interface is similar to class but interface member cannot have a defination,only have declaration
->In Interface we can not define Explicit Access Modifier by default interface method is public
->we can not define fields in interface like int id, string name 
->We can Inherit multiple interface at the time by using , (comma) seprator
->If we are inherting the interface We Must implement the methods of interface in class
->We Can not create instance of Interface , But an interface reference variable can point to a derived class object.
->If Class Or Structure Inherits from an interface  ,It must provide implemenation for all interface member othrwise we get a comile error.

Example 
 public class Class1
    {
        interface I1
        {
            void Method1();
            void Method2();
        }
        interface I2
        {
            void Method3();
        }

        public class Customer : I1, I2
        {
            public void Method1()
            {
                Console.WriteLine("Print Method 1");
            }

            public void Method2()
            {
                Console.WriteLine("Print Method 2");
            }

            public void Method3()
            {
                Console.WriteLine("Print Method 2");
            }
        }

        public static void Main()
        {
            Customer c1 = new Customer();
            c1.Method1();
            c1.Method2();
            c1.Method3();
            Console.ReadLine();
        }
    }

2> What Is Explicit Interface

If you explicitly implement an interface below is how the code looks like. The difference is that the “Add” and “Update” methods are now private.

public class Person : IDal
{
        public string FirstName { get; set; }
        public string LastName { get; set; }


void IDal.Add()
        {
            Console.WriteLine("Add");
        }

        void IDal.Update()
        {
            Console.WriteLine("Update");
        }
}

In simple words the difference between “Implicit” interface and “Explicit” interface is that in implicit the interface methods are publicly implemented while in explicit the methods are privatelyimplemented.

3>What is structure

->Structure is declare same like class for decalring structure we are using struct keywords.
->In C#, a structure is a value type data type. 
  It helps you to make a single variable hold related data of various data types.
->value type hold there value in memory where they are declared,
  but reference type hold a reference to an object in memory.
-> structure can not have destuctor but class can have destructor.
                                    


-> structure can not have explicit parameterless constructor but class can 
->There is no inheritance for structs as there is for classes. 
  A struct cannot inherit from another struct or class, 
  and it cannot be the base of a class. 
  Structs, however, inherit from the base class Object. 
  A struct can implement interfaces, and it does that exactly as classes do.

4>What is enumeration 

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.
C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.

Declaring enum Variable
The general syntax for declaring an enumeration is:

enum <enum_name> 
{
   enumeration list 
};
Where,

The enum_name specifies the enumeration type name.
The enumeration list is a comma-separated list of identifiers.

Each of the symbols in the enumeration list stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0. 

For example:

enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
Example
The following example demonstrates use of enum variable:

using System;
namespace EnumApplication
{
   class EnumProgram
   {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args)
      {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}




Share this

Previous
Next Post »