Commonly Asked C# Interview Questions and Answers for Experienced and Freshers
C# is a general-purpose programming language that includes different disciplines like object-oriented programming, static typing, component-oriented programming, strong typing, etc. C# is widely used in the ASP.NET framework for making websites, web applications, and games. There are vast opportunities for C# programming all over the world. If you're considering making a career in C# programming, you have to crack an interview in which you'll be asked several C# basic interview questions and answers as given below.
This is a list of C# interview questions and answers, which are likely to be asked in the interview. Candidates are likely to be asked basic C# interview questions to advance level C#.NET interview questions depends on their experience and various other components. This list covers all the C# questions for freshers, as well as C#, interview questions, and answers for experienced level candidates.
Here are some frequently asked interview questions for experienced as well as freshers C# developers candidates to get the right job.
Q1. What is C#?
A1. C# is an object-oriented, type-safe, and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language.
Q2. Explain types of comment in C# with examples
A2. There are three types of comments in c#
|XML Comments (///)||Multiple line (/* */)||Single line|
/// Set error message for multilingual language.
|/*This is a multiple line comment|
We are in line 2
Last line of comment*/
|//This is a single line comment|
Q3. What is the difference between ref & out parameters?
A3. An argument passed as ref must be initialized before passing to the method whereas out parameter needs not be initialized before passing to a method.
Q4. What is serialization?
A4. When we want to transport an object through a 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.
Q5. What is an interface class? Give one example of it.
A5. An Interface is an abstract class that has only public abstract methods, and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.
Q6. What are value types and reference types?
A6. A value type holds a data value within its own memory space.
Reference type stores the address of the object where the value is being stored. It is a pointer to another memory location.
Q7. What are Custom Control and User Control?
A7. Custom Controls are controls generated as compiled code (Dlls), those are easier to use and can be added to the toolbox. Developers can drag and drop controls to their web forms. Attributes can, at design time. We can easily add custom controls to Multiple Applications (If Shared Dlls). So, If they are private, then we can copy to dll to the bin directory of the web application and then add references and can use them.
User Controls are very much similar to ASP include files, and are easy to create. User controls can't be placed in the toolbox and dragged - dropped from it. They have their design and code-behind. The file extension for user controls is ascx.
Q8. What are sealed classes in C#?
A8. We create sealed classes when we want to restrict the class to be inherited. The sealed modifier used to prevent derivation from a class. If we forcefully specify a sealed class as a base class, then a compile-time error occurs.
Q9. What is the difference between Array and Arraylist?
A9. In an array, we can have items of the same type only. The size of the array is fixed when compared. An ArrayList is similar to an array, but it doesn't have a fixed size.
Q10. Can a private virtual method be overridden?
A10. No, because they are not accessible outside the class.
Q11. Describe the accessibility modifier "protected internal".
A11. Protected Internal variables/methods are accessible within the same assembly and also from the classes that are derived from this parent class.
Q12. What are the differences between System.String and System.Text.StringBuilder classes?
A12. System.String is immutable. When we modify the value of a string variable, then a new memory is allocated to the new value and the previous memory allocation released. System.StringBuilder was designed to have a concept of a mutable string where a variety of operations can be performed without allocation separate memory location for the modified string.
Q13. What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
A13. Using the Clone() method, we create a new array object containing all the elements in the original Array and using CopyTo() method. All the elements of the existing array copies into another existing array. Both methods perform a shallow copy.
Q14. How can we sort the elements of the Array in descending order?
A14. Using Sort() methods followed by Reverse() method.
15. Write down the C# syntax to catch an exception?
A15. To catch an exception, we use try-catch blocks. Catch block can have a parameter of system.Exception type.
In the above example, we can omit the parameter from the catch statement.
16. What is the difference between Finalize() and Dispose() methods?
A16. Dispose() is called when we want for an object to release any unmanaged resources with them. On the other hand, Finalize() is used for the same purpose, but it doesn't assure the garbage collection of an object.
Q17. List down the commonly used types of exceptions in .net?
A17. ArgumentException, ArgumentNullException , ArgumentOutOfRangeException, ArithmeticException, DivideByZeroException ,OverflowException , IndexOutOfRangeException ,InvalidCastException ,InvalidOperationException , IOEndOfStreamException , NullReferenceException , OutOfMemoryException , StackOverflowException etc.
18. What are Custom Exceptions?
A18. Sometimes there are some errors that need to be handled as per user requirements. Custom exceptions are used for them and are used defined exceptions.
Q19. What are delegates?
A19. Delegates are the same are function pointers in C++, but the only difference is that they are type-safe, unlike function pointers. Delegates are required because they can be used to write much more generic type-safe functions.
Q20. How do you inherit a class into another class in C#?
A20. The colon is used as an inheritance operator in C#. Just place a colon and then the class name.
public class DerivedClass : BaseClass
Q21. What is the base class in .net from which all the classes are derived from?
Q22. Why can't you specify the accessibility modifier for methods inside the interface?
A22. In an interface, we have virtual methods that do not have method definitions. All the methods are there to be overridden in the derived class. That's why they all are public.
Q23. How can we set the class to be inherited, but prevent the method from being over-ridden?
A23. Declare the class as public and make the method sealed to prevent it from being overridden.
Q24. What happens if the inherited interfaces have conflicting method names?
A24. The implement is up to you as the method is inside your own class. There might be a problem when the methods from different interfaces expect different data, but as far as the compiler cares you're okay.
Q25. What is the difference between a Struct and a Class?
A25. Structs are value-type variables, and classes are reference types. Structs stored on the Stack causes additional overhead but faster retrieval. Structs cannot be inherited.
26. How to use nullable types in .Net?
A26. Value types can take either their normal values or a null value. Such types are called nullable types.
Q27. How we can create an array with non-default values?
A27. We can create an array with non-default values using Enumerable.Repeat.
Q28. What's a multicast delegate?
A28. A delegate having multiple handlers assigned to it is called a multicast delegate. Each handler is assigned to a method.
Q29. What are indexers in C# .NET?
A29. Indexers are known as smart arrays in C#. It allows the instances of a class to be indexed in the same way as an array.
Q30. What are C# attributes and their significance?
A30. C# provides developers a way to define declarative tags on certain entities, eg. Class, method, etc. are called attributes. The attribute's information can be retrieved at runtime using Reflection.
Q31. How to implement a singleton design pattern in C#?
A31. In a singleton pattern, a class can only have one instance and provides an access point to it globally.
Q32. What is the difference between direct cast and ctype?
A32. DirectCast is used to convert the type of object that requires the run-time type to be the same as the specified type in DirectCast.
Ctype is used for conversion where the conversion is defined between the expression and the type.
Q33. What is a Console application?
A33. A console application is an application that can be run in the command prompt in Windows. For any beginner on .Net, building a console application is ideally the first step, to begin with.
Q34. Give an example of removing an element from the queue?
A34. The dequeue method is used to remove an element from the queue.
Q35. What is the difference between “break” and “continue” statements in C#?
A35. Using a break statement, you can 'jump out of a loop' whereas by using a continue statement, you can 'jump over one iteration' and then resume your loop execution.
Example Break Statement
The number is 0;
The number is 1;
The number is 2;
The number is 3;
Example Continue Statement
The number is 1;
The number is 2;
The number is 3;
The number is 5;
Q36. What is the difference between constant and read-only in C#?
A36. Const is nothing but "constant", a variable of which the value is constant but at compile time. It's mandatory to assign a value to it. By default, a const is static and we cannot change the value of a const variable throughout the entire program.
Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor.
We have a Test Class in which we have two variables, one is read-only and the other is a constant.
Here, I was trying to change the value of both the variables in the constructor, but when I try to change the constant, it gives an error to change their value in the block that I have to call at run time.
Finally, remove that line of code from class and call this Check() function like in the following code snippet:
Q37. What are Properties in C#?
A37. C# properties are members of a C# class that provide a flexible mechanism to read, write or compute the values of private fields, in other words, by using properties, we can access private fields and set their values. Properties in C# are always public data members. C# properties use to get and set methods, also known as accessors, to access and assign values to private fields.
What are accessors?
The get and set portions or blocks of a property are called accessors. These are useful to restrict the accessibility of a property. The set accessor specifies that we can assign a value to a private field in a property. Without the set accessor property, it is like a read-only field. With the 'get' accessor we can access the value of the private field. In other words, it returns a single value. A Get accessor specifies that we can access the value of a field publically.
We have three types of properties: Read/Write, ReadOnly, and write-only. Let's see each one by one.
Q38. What are extension methods in C#?
A38. Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.
How to use extension methods?
An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.