C# Interview Questions and Answers

Microsoft C# is one of the most popular programming languages and C# is widely used for asp.net application development, thus gaining popularity among web application developer. C# is an object oriented programming language, so rather than just memorising the question, try to understand the concept behind each questions answers.

.Net C# job interview questions and answers for fresher and experienced developers
C# .net Interview Questions Answers
C# is a object-oriented programming language developed by Microsoft. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

Here are some commonly asked .Net C# Interview Questions

C# programming interview questions

  1. Can you define OOP and the 4 principles of OOP
    • Abstraction
    • Encapsulation
    • Inheritance
    • polymorphism

    Learn more about OOP programming in C#

  2. What is Polymorphism, overloading, overriding and virtual?
    Polymorphism is one of the key characteristic in OOPS, the word polymorphism means “having multiple forms”, In object oriented programming defining one function for multiple purpose is considered as Polymorphism,

    Function Overloading is a type Polymorphism, means one function with different parameters, look at example below

    public void HelloWorld()
    {
        Console.writeline("Hello How are you?");
    }
    public void HelloWorld(string name)
    {
        Console.writeline(string.format("Hello {0}, How are you?", name));
    }
    

    Overriding is used when we implement any function that has been declared as either abstract or virtual, then in child class at the time of implementation we need to use override the base.

  3. What is the difference between static polymorphism and dynamic polymorphism?
    Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.
  4. Explain about Generics?

    Generics are the new feature in the c# 2.0. They introduce the concept of Type parameter. This makes it possible to classes and methods that differs the specification of one or more types until the class or method is declared and used by the code, which will be using it.

    We can declare a generic class like this
    public class GenericClass<T>  
    {    
        void Add(T input) { } 
    }
    

    We create an instance of the generic class like this

    GenericClass<string> list2 = new GenericClass<string>();

    Read more about generic

  5. How generic is different from the ArrayList class?

    The System.Collection.ArrayList can be used with any objectn, but no type checking is done when objects are passed to methods. You have to manually cast objects back to our type when retrieving; which makes the code harder.

  6. Define Interface & What is the diff. between abstract & interface?
    • A class can implement any number of interfaces but a subclass can at most use only one abstract class.
    • An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods has to be abstract.
    • An abstract class can’t be used for multiple inheritance while interface can be used as multiple inheritance.
    • An abstract class use constructor while in an interface we don’t have any type of constructor.
    • In an abstract class all data member or functions are private by default while in interface all are public, we can’t change them manually.
    • An abstract class can declare or use any variables while an interface is not allowed to do so.
    • In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don’t need to use that.
  7. What is the difference between a struct and a class in C#?

    Class and struct both are the user defined data type, but they have some difference

    Struct Class
    1. The struct is value type in C# and it inherits from System.Value Type. 1. The class is reference type in C# and it inherits from the System.Object Type
    2. Struct is usually used for smaller amounts of data 2. Classes are usually used for large amounts of data
    3. Structure can't be abstract 3. Class can be abstract type
    4. Struct can't be inherited to other type 4. Classes can be inherited to other class.
    5. Can not create any default constructor. 5. Can create a default constructor
  8. What is a stack, Heap, Value types and Reference types ?
    Stack Heap
    1. Stack is used for static memory allocation 1. Heap for dynamic memory allocation
    2. Stack memory allocated at compiled time, access to this memory is very fast 2. Heap memory are allocated at run time and accessing this memory is a bit slower
    3. Stack is value type 3. Heap reference type
    // Stack example
    int i=10;
    
    // Heap example
    Student stu=new Student();
                            
    4. Struct, Enum, byte, decimal, double, float, long 4. string , class, interface, object

  9. what is boxing and unboxing?

    Boxing is assigning a value type to reference type variable, unboxing is opposite.

    boxing example
    int i=100;
    object o =i;
    unboxing example
    object o=100;
    int i =(int)o; //unboxing
    
  10. What is the difference between ref and out parameters?

    Output parameters are similar to reference parameters, except that they transfer data out of the method rather than within method.

    Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

  11. Is C# code is managed or unmanaged code?

    C# is managed code because Common language runtime can compile C# code to Intermediate language.

  12. How can we set class to be inherited, but prevent the method from being over-ridden?

    Declare the class as public and make the method sealed to prevent it from being overridden.

  13. What is "protected internal" ?

    Protected Internal variables/methods are accessible within the same assembly and also from the classes that are derived from this parent class.

    Learn more about access modifiers and keywords in C#

  14. What would be the output of following code?
    class Program
    {
        static String location;
        static DateTime time;
         
        static void Main(string[] args)
        {
            Console.WriteLine(location == null ? "location is null" : location);
            Console.WriteLine(time == null ? "time is null" : time.ToString());
        }       
    }
    

    The first statement will print, “location is null”.

    And the second statement will print default time, the time variable is not null unless it's specified as DateTime?

  15. What is method overloading?

    Method overloading is creating multiple methods with the same name with unique signatures in the same class. When we compile, the compiler uses overload resolution to determine any specific method to be invoked.

  16. What is serialization?
    When we want to transport an object through network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be serializable, it should implement ISerialize Interface. De-serialization is the reverse process of creating an object from a stream of bytes.
  17. What is encapsulation?

    Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.

  18. What is a enumeration in C#?
    An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.

    enumeration cannot inherit or cannot pass inheritance, in C# enumerations are value data type.

  19. What is difference between constants and read-only?
    Constant variables are declared and initialized at compile time. The value can’t be changed afterwards. Read only is used only when we want to assign the value at run time.
  20. What's the difference between the Copy() and Clone() ?
    In copy Copy() method, we copy the whole object with data, in clone Clone() method, we copy only structure without data.
  21. What is namespace in C#?
    A namespace is boundary, is designed for keeping all class in a organised way, So that a class names declared in one namespace does not conflict with the same class names declared in another.
  22. What is Reflection in .Net ?

    Reflection is functionalities by which we can get metadata information about loaded assemblies.

    All class for using Reflection under System.Reflection

    System.Reflection
  23. What is Threading?
    Threading is collection of process, learn more about Threading
  24. What is thread pool?

    A thread pool is a collection of threads that can be used to perform several tasks in the background.

    Once a thread in the pool completes its task, it is returned to a queue of waiting threads, where it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task.

  25. What is Thread safety?
    Thread safety is a technique which manipulates shared data structure in a manner that guarantees the safe execution of a piece of code by the multiple threads at the same time. A code is called thread safe if it is being called from multiple threads concurrently without the breaking of functionalities

    Learn more about Threading

  26. Write a C# method to total all the values that are even numbers in following array.
    int[] InArray= new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    

    Here are two methods to get the sum of even and odd number of an array.

    public static long SumAllEvenNumbers(int[] intArray)
    {
    return intArray.Where(i => i % 2 == 0)
    .Sum(i => (long)i);
    }
    public static long SumAllOddNumbers(int[] intArray)
    {
    return intArray.Where(i => i % 2 != 0)
    .Sum(i => (long)i);
    }
    
  27. What would be the output of following code? and explain why?
    class Program
        {
        delegate void Printer();
        static void Main(string[] args)
            {
                List<Printer> printers = new List<Printer>();
               
                for (int i = 0; i < 10; i++)
                {
                    printers.Add(delegate { Console.WriteLine(i); });                
                }
                foreach (var printer in printers)
                {
                    printer();
                }
            }
        }
    
    The above code will print 10 ten times, because by the time printer delegates executed the value of i will become 10.
  28. What is the difference between public, static, and void?

    Public is an access modifier, public keyword indicates that the declared variables or methods are accessible anywhere in the application.

    Static indicates, without creating the new instance or object of a class we can access that member directly.

    Void indicates that the type of return is nothing, so that's called void.

  29. What is the difference between Dispose and Finalize methods?
    Dispose method Dispose() is called when we want for an object to release any unmanaged resources with them, we can use IDisposable interface to implement dispose method, On the other hand, Finalize method Finalize() is used for the same purpose, but it doesn't assure the garbage collection of an object, we also can not call Finalize method, it gets executed internally.
  30. How to read configuration details in C# application?
    We can read configuration details with ConfigurationManager class and methods, The following methods will read the section values from app.config file which is kept in root of the system.
    using System;
    using System.Configuration;
    
    public static BusinessSettings GetBusinessSettings(string sectionPath)
    {
        return ConfigurationManager.GetSection(sectionPath) as BusinessSettings;
    }
    

    You may look at Read appSettings json in .Net Core Console Application.

  31. What is Tuple variable in C# ?

    Tuples means more than one data type, Tuple was introduced in C# 7 version.

    We can define Tuples variable like example below, we also can return Tuples data type from any function in c# program.

    (string Alpha, string Beta) namedLetters = ("a", "b");
    Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
    
  32. What are the new features in c# 7?

    There are many new features was introduced in C# 7, like Tuples, out variable, Pattern matching, async Main, default literal expressions etc.

    learn more about c# New Features and latest version.

Check Free C# Tutorial, C# Course Online
SQL Database course online
free C# tutorial online
Interview Question Answers | Fresher Job Interview Tips