Here are some common C# interview questions along with their answers and examples
1. What is C#?
Answer: C# is an object-oriented programming language developed by Microsoft as part of the .NET framework. It is used to create Windows applications, web applications, and games, among other things.
2. What are the features of C#?
Answer: The features of C# include garbage collection, type safety, object-oriented programming, automatic memory management, and support for generics and lambda expressions.
3. What are the differences between C# and Java?
Answer: The main differences between C# and Java are that C# is a Microsoft language and Java is a Sun Microsystems language. C# supports delegates, whereas Java uses interfaces. C# also has support for properties, events, and operator overloading, which Java does not have.
4. What is the difference between string and StringBuilder in C#?
Answer: The main difference between string and StringBuilder is that strings are immutable, meaning that once they are created, they cannot be changed. StringBuilder, on the other hand, is mutable, meaning that you can modify its contents. Here is an example:
string str = "hello"; str += " world"; // this creates a new string object, it does not modify the original string StringBuilder sb = new StringBuilder("hello"); sb.Append(" world"); // this modifies the original StringBuilder object
5. What is an interface in C#?
Answer: An interface is a contract between a class and the outside world. It defines a set of methods that a class must implement, but does not provide an implementation for those methods. Here is an example:
6. What is an abstract class in C#?interface IShape { void Draw(); } class Circle : IShape { public void Draw() { // draw a circle } } class Rectangle : IShape { public void Draw() { // draw a rectangle } }
Answer: An abstract class is a class that cannot be instantiated. It is used as a base class for other classes to derive from, and it may contain abstract methods that must be implemented by its derived classes. Here is an example:
abstract class Animal { public abstract void MakeSound(); } class Dog : Animal { public override void MakeSound() { Console.WriteLine("Woof!"); } } class Cat : Animal { public override void MakeSound() { Console.WriteLine("Meow!"); } }
0 Comments