C# Interview Questions and Answers for 5 years Experienced
by Anuradha, on Mar 17, 2018 1:08:09 PM
Q1. What is C-Sharp (C#)?
Ans: C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).
Q2. Explain the types of comments in C#?
Ans: Below are the types of comments in C#:
Single Line Comment Eg : //
Multiline Comments Eg: /* */
XML Comments Eg : ///
Q3. So what makes your code really object-oriented?
Ans: In order to understand this, we must first analyze what benefits we can derive from OO. In order to do this, we must clearly understand its foundation. So given that C# is at its core object-oriented.
Q4. What is Cohesion?
Ans: In OOPS we develop our code in modules. Each module has certain responsibilities. Cohesion shows how much a module responsibilities are strongly related.
Higher cohesion is always preferred. Higher cohesion benefits are:
- Improves maintenance of modules.
- Increase reusability.
Q5. Why are strings in C# immutable?
Ans: Immutable means string values cannot be changed once they have been created. Any modification to a string value results in a completely new string instance, thus an inefficient use of memory and extraneous garbage collection. The mutable System.Text.StringBuilder class should be used when string values will change.
Q6. List out the differences between Array and Array List in C#?
Ans: Array stores the values or elements of same data type but array list stores values of different data types.
Arrays will use the fixed length but array list does not uses fixed length like array.
Q7. What are the fundamental principles of OO programming?
Ans: As a developer, you might be tempted to answer that it comprises things like Encapsulation, Polymorphism, Abstraction, and Inheritance. Although this is true, it doesn’t really explain the fundamental core of what OO is and what its benefits are.
Principles are crucial but they are not the most important aspect of what OO actually is. What is really important is to understand in what grounds OO is built upon, or in other words, what are the foundations of OO programming.
The two most fundamental core concepts on which OO has been built upon in C# are this pointer and Dynamic Dispatch.
Obviously, there are principles like Encapsulation, Polymorphism, Abstraction, and Inheritance, but these are the consequence and not the generating force behind the OO paradigm in C#.
Q8. What is coupling?
Ans: OOPS Modules are dependent on each other. Coupling refers to level of dependency between two software modules.
Two modules are highly dependent on each other if you have changed in one module and for supporting that change every time you have to change in dependent module.
Loose Coupling is always preferred.
Inversion of Control and dependency injections are some techniques for getting loose coupling in modules.
Q9. What is the execution entry point for a C# console application?
Ans: The Main method.
Q10. Why to use “using” in C#?
Ans: “Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.
Q11. What is the this Pointer?
Ans: The this pointer is silently passed with a call to an instance-level function, which then operates on an object (instance of a class).
Basically, this core mechanism makes it possible to bring operations close to data. It also eliminates the need to have global functions and it gives data structures the intrinsic ability to perform operations on its data.
Q12. What is Abstraction?
Ans: Abstraction is a technique of taking something specific and making it less specific.
In OOPS we achieve the abstraction by separating the implementation from interface. We take a implemented class and took only those method signatures and properties which are required by the class client. We put these method signatures and properties into interface or abstract class.
Q13. How do you initiate a string without escaping each backslash?
Ans: You put an @ sign in front of the double-quoted string.
String ex = @"This has a carriage return\r\n"
Q14. Explain namespaces in C#?
Ans: Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace.
Q15. What is the OO fundamental idea using C# that allows a data structure to perform operations on its own data?
Ans: What would your answer be? Pretty obvious. The humble this pointer.
Notice that despite this being a mind-bending idea, we can already start to appreciate the bigger picture for which C# was designed.
The this pointer is basically a way for a data structure (object) to be able to access methods that allow itself to perform operations on its own data. It is a way to manage state within a data structure.
Now let’s talk a bit about the other core concept that takes this to the next level.
Q16. What is Encapsulation?
Ans: In non object oriented languages, data and behaviors are not tied together. That means any function in the program can modify the data.
In Encapsulation, we bind the data and behaviors in one object. Only defined behaviors in a class can modify the data. We hide the state of an object by using properties and methods. Clients of the object only see these behaviors and by only these behaviors clients can modify the data.
We also protect the data by using access specifiers. We put the private / protected keywords before data to protect it from the outside world.
Q17. What is the difference between a struct and a class?
Ans: Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.
Q18. Explain “static” keyword in C#?
Ans: “Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.
Q19. What is a singleton?
Ans: A singleton is a design pattern used when only one instance of an object is created and shared; that is, it only allows one instance of itself to be created. Any attempt to create another instance simply returns a reference to the first one. Singleton classes are created by defining all class constructors as private. In addition, a private static member is created as the same type of the class, along with a public static member that returns an instance of the class. Here is a basic example:
public class SingletonExample { private static SingletonExample _Instance; private SingletonExample () { } public static SingletonExample Get Instance() { if (_Instance == null) { _Instance = new SingletonExample (); } return _Instance; }}
Q20. Why to use “finally” block in C#?
Ans: “Finally” block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last “finally” block will be executed. So closing connection to database / releasing the file handlers can be kept in “finally” block.
Q21. What is boxing?
Ans: Boxing is the process of explicitly converting a value type into a corresponding reference type. Basically, this involves creating a new object on the heap and placing the value there. Reversing the process is just as easy with unboxing, which converts the value in an object reference on the heap into a corresponding value type on the stack. The unboxing process begins by verifying that the recipient value type is equivalent to the boxed type. If the operation is permitted, the value is copied to the stack
Q22. Can we have only “try” block without “catch” block in C#?
Ans: Yes we can have only try block without catch block.
Q23. How to move to a State-related Codebase?
Ans: So now that we’ve explored these concepts, let’s move to a state-related code base. So you might be asking yourself at this moment.
Q24. What is the difference between “out” and “ref” parameters in C#?
Ans: “out” parameter can be passed to a method and it need not be initialized where as “ref” parameter has to be initialized before it is used.
Q25. How are methods overloaded?
Ans: Methods are overloaded via different signatures (number of parameters and types). Thus, you can overload a method by having different data types, different number of parameters, or a different order of parameters.
Q26. Explain Jagged Arrays in C#?
Ans: If the elements of an array is an array then it’s called as jagged array. The elements can be of different sizes and dimensions.
Q27. How do you prevent a class from being inherited?
Ans: The sealed keyword prohibits a class from being inherited.
Q28. What you mean by inner exception in C#?
Ans: Inner exception is a property of exception class which will give you a brief insight of the exception i.e, parent exception and child exception details.
Q29. What is the GAC, and where is it located?
Ans: The GAC is the Global Assembly Cache. Shared assemblies reside in the GAC; this allows applications to share assemblies instead of having the assembly distributed with each application. Versioning allows multiple assembly versions to exist in the GAC—applications can specify version numbers in the config file. The gacutil command line tool is used to manage the GAC.
Q30. Explain circular reference in C#?
Ans: This is a situation where in, multiple resources are dependent on each other and this causes a lock condition and this makes the resource to be unused