C Sharp Interview Questions

  • 1
    What is C#?

    C# is an object-oriented, type-safe, and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language.

  • 2
    Difference between C++ and C#?

    C#

    •   C# is a higher level programming language built over C++ programming language.
    •  It is similar to Java.
    •   It can be considered as a hybrid programming language consisting of features of C++ and ease of use of VB.Net

    C++

    • C++ is a low level programming language built by adding object oriented concepts to the C programming language.
    •   It is NOT similar to Java.
    • C++ itself is a language and does not inherit any features of other languages.
  • 3
    What is a Datatype?

    Datatype refers to the type of data that can be stored in a variable. It also specifies how memory would be allocated to a variable and the operations that can be performed on that variable.

  • 4
    What are Nullable types?

    Value types that can accept a normal value or a null value are referred to as Nullable types

  • 5
    How to check that a nullable variable is having value?

    The HasValue property is used to check whether a variable having value or not.

  • 6
    What is the ref keyword in C#?

    The ref keyword causes an argument to be passed by reference, not by value.

  • 7
    What is the params keyword in C#?

    The params keyword enables a method parameter to receive n number of arguments. These n number of arguments are changed by the compiler into elements in a temporary array.

  • 8
    What is upcasting and Downcasting?

    Implicit conversion of derived classes to a base class is called Upcasting and explicit conversion of the base class to a derived class is called Downcasting.

  • 9
    What is boxing and unboxing in C#?

    Boxing: Implicit conversion of a value type(int, char) to a reference type is known as boxing. 

    int variable = 15
    object boxingvar = variable
    

    Unboxing: Explicit conversion of the same reference type(which is created back in boxing process) back to value type is known as unboxing.

    int variable = 15
    object boxingvar = variable
    int unboxed = (int)boxingvar
    
  • 10
    What are the different types of decision-making statements in C#?

    Decision-making statements help you to make a decision based on certain conditions. Different types of decision-making statements are: if statements, if-else statements, if-else-if statements, and switch statements.

  • 11
    What are the different ways a method can be overloaded?

    Methods can be overloaded using different data types for a parameter, different order of parameters, and the different number of parameters.

  • 12
    Which one is better/fast, switch or if-else-if statements and why?

    Switch statements are father than if-else-if statements because in if-else if statements each and every condition has to check but in case of the switch statement compiler does not need to check earlier cases.

  • 13
     What do you mean by partial method?

    A partial method is a special method within a partial class or struct. One part of a struct has the only partial method declaration means signature and another part of the same partial class may have an implementation.

  • 14
    What is the goto statement?

    The goto statement transfers program control to a labeled statement. The statement must exist in the scope of the goto statement.

  • 15
    What is an object pool in .NET?

    An object pool is a container having objects ready to be used. It tracks the object that is currently in use, the total number of objects in the pool.

  • 16
     What is the return statement in C#?

    The return statement terminates the execution of the method in which it appears and returns control to the calling method.

  • 17
    Can multiple catch blocks be executed?

    No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the final block, and then the code that follows the final block gets executed.

  • 18
    What is the jump statement in C#?

    The jump statements transfer the program control from one point in the program to another point of the program.

  • 19
    What are the named arguments?

    Any argument can be passed by parameter name instead of the parameter position.

  • 20
    What is the throw statement mean?

    This statement throws an exception which indicates that an error has occurred during the program execution.

  • 21
    What do you mean by an array and what are the different types of an array in C#?

    An array is a collection of the same type of elements that are accessible by a numerical index. Different types of array are- one dimensional, two dimensional, and jagged arrays.

  • 22
    What is a jagged array?

    A jagged array is an array whose elements are array itself of different dimensions and sizes.

  • 23
    What are the differences between events and delegates in C#?

    Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in the subscriber/destination model. 

  • 24
     What is the difference between “out” and “ref” parameters in C#?

    “out” parameter can be passed to a method and it need not be initialized whereas “ref” parameter has to be initialized before it is used.

  • 25
    Do we get an error while executing the “finally” block in C#?

    Yes. We may get an error in finally block.

  • 26
    What are the differences between static, public and void in C#?

    Static classes/methods/variables are accessible throughout the application without creating an instance. The compiler will store the method address as an entry point.

    Public methods or variables are accessible throughout the application.

    The void is used for the methods to indicate it will not return any value.

  • 27
     List out two different types of errors in C#?

    • Below are the types of errors in C# –
    • Compile Time Error
    • Run Time Error
  • 28
    Explain Partial Class in C#?

    Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic into multiple files with the same class name along with “partial” keyword.

  • 29
    What is Thread in C#?

    Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time-consuming processes then it’s better to use Multithreading., which involves multiple threads.

  • 30
    What does it mean?

    Object Pool is nothing but a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.

  • 31
    How does it work?

    We are going to use a Factory pattern for this purpose. We will have a factory method, which will take care of the creation of objects. Whenever there is a request for a new object, the factory method will look into the object pool.If there is any object available within the allowed limit, it will return the object, otherwise, a new object will be created and give you back.

  • 32
    What is Constructor Overloading in C#?

    In Constructor overloading, n number of constructors can be created for the same class. But the signatures of each constructor should always vary.

    Example

    public class Student
    {
     public Student() 
     { }
     public Student(String fullname) 
     { }
    }
    
  • 33
    What are indexers in C#?

    Indexers are known as the smart arrays in C#. It allows the instances of a class to be indexed in the same way as array.

    Example

    public int this[int index]
    
  • 34
    What is Hashtable in C#?

    A Hashtable is a collection of key-value pairs. It contains values based on the key.

  • 35
    What are circular references?

    Circular reference is state in which 2 or more resources are interdependent on each other causes the lock condition and make the resources unusable.

  • 36
    What are the collection types can be used in C#?

    Following are the collection types in C# –

    • ArrayList
    • Stack
    • Queue
    • SortedList
    • HashTable
    • Bit Array
  • 37
    What is the difference between is and as operators in c#?

    “is” operator is used to checking the compatibility of an object with a given type and it returns the result as Boolean.

    “as” operator is used for casting of an object to a type or a class.

  • 38
    How do you inherit a class into other class in C#?

    Colon is used as inheritance operator in C#. Just place a colon and then the class name.

    Example:

    public class DerivedClass : BaseClass
    
  • 39
    What is generics in C#.NET?

    Generics are used to make reusable code classes to decrease the code redundancy, increase type safety and performance. Using generics, we can create a collection of classes. To create a generic collection, System.Collections.Generic namespace should be used.

  • 40
    Explain ‘Virtual’ Keyword?

    The virtual keyword is used while defining a class to specify that the method and properties of that class can be overridden in derived class.

  • 41
    How to sort an array in C#?

    Using Array.sort(array) function.

  • 42
    What is the purpose of using statement in C#?

    using keyword is used to include a namespace in the program. A program generally has multiple using statements.

  • 43
    Why we use keyword “const” in C#.

    “Const” keyword is used for making an entity constant.

    Example:

    const string _name = “techstudy”; Once we define a variable as a const then we can’t reassign the value of that variable(string _name).

  • 44
    What are different types of the parameter in c#?

    Following are different types of parameter in c#

    • Value parameter(in)
    • Reference parameter (ref)
    • Output parameter(out)
    • Parameter Array(param)
  • 45
    How do you get the size of value type?

    using SizeOf operator.

    Example: 

    Console.WriteLine(“The size of long is {0}.”, sizeof(long));
     output: The size of long is 8.
    
  • 46
    What are reference types in C#?

    reference types stored the address of memory where data is stored

    reference types use the heap to store value.

    Example: Class, Interface, String, Object, Delegate, Array

  • 47
    Explain sealed class in C#?

    The sealed class is used to stop the class from being inherited from other classes. If you define a class in C# as “Sealed” & “nonheritable” in VB.NET, then you can not inherit the class further.

  • 48
    What is an object?

    An object is an instance of the class. It is a basic unit of the system. An object is an entity that has attributes, behavior & identity. Attributes & behavior of an object is defined by the class definition.

  • 49
     Name the compiler of C#?

    Name of C# Compiler is – CSC.

  • 50
    Which string method is used for concatenation of two strings in c#?

    “Concat” method of String class is used to concatenate two strings

    Example

    string.Concat(firstnamestr, lastnamestr)