C#, Questions Flashcards
- Explain the concept of polymorphism in C#. Provide an example to illustrate your answer.
Polymorphism in C# allows one interface to be used for different data types or classes, providing flexibility in code. It can be achieved via method overriding or method overloading. Example:
class Animal { public virtual void Speak() { Console.WriteLine(“Animal speaks”); } }
class Dog : Animal { public override void Speak() { Console.WriteLine(“Dog barks”); } }
Animal myDog = new Dog(); myDog.Speak(); // Output: Dog barks
- What is a delegate in C#? Explain with an example of how to declare and use a delegate.
A delegate in C# is a type-safe function pointer that can reference a method. Example:
delegate void Greet(string name);
class Program { static void SayHello(string name) { Console.WriteLine(“Hello, “ + name); }
static void Main() { Greet greet = SayHello; greet(“Alice”); } } // Output: Hello, Alice
- Describe the use of the foreach loop in C#. Provide an example to demonstrate its usage.
The foreach loop in C# is used to iterate through elements in a collection or array. Example:
int[] numbers = { 1, 2, 3 }; foreach (int num in numbers) { Console.WriteLine(num); }
// Output: 1 2 3
- Explain the concept of garbage collection in C#. How does it help manage memory?
Garbage collection in C# is an automatic memory management feature that reclaims memory occupied by objects no longer in use, preventing memory leaks. The .NET runtime automatically identifies and frees unused objects.
- What is an interface in C#? Explain with an example of how to implement and use an interface.
An interface is a class that is complete abstract and contain only abstact methods and properties
- Explain what LINQ is and provide a simple example of a LINQ query.
LINQ or Language Intergrated Query is powefull feature in C# that allows users to perform queries to different data sources using a consitent syntax directily integrated into C#.
- Describe exception handling in C#. Include an example with a try-catch block.
Exception handling in C# uses try-catch blocks to handle runtime errors gracefully. Example:
try { int result = 10 / 0; }
catch (DivideByZeroException ex) { Console.WriteLine(“Cannot divide by zero!”); }
// Output: Cannot divide by zero!
- Describe the difference between public, private, and protected access modifiers in C#. Provide an example for each.
Public: Accessible from anywhere. Private: Accessible only within the class. Protected: Accessible within the class and derived classes.
Example:
class Base { public int PublicValue; private int PrivateValue; protected int ProtectedValue; }
class Derived : Base { void Test() { Console.WriteLine(ProtectedValue); } }
- Explain the concept of encapsulation in C#. Provide an example to illustrate your answer.
Encapsulation is the bundling of data and methods in a class and restricting direct access to fields using access modifiers. Example:
class Person { private string name; public string Name { get { return name; } set { name = value; } } }
Person person = new Person(); person.Name = “Alice”; Console.WriteLine(person.Name);
- Describe how inheritance works in C#. Include an example to demonstrate inheritance between two classes.
Inheritance in C# allows a class to inherit members (fields, methods) from another class. Example:
class Animal { public void Eat() { Console.WriteLine(“Animal eats”); } }
class Dog : Animal { public void Bark() { Console.WriteLine(“Dog barks”); } }
Dog dog = new Dog(); dog.Eat(); dog.Bark(); // Output: Animal eats Dog barks