
- What is method overloading in C#?
- Method overloading in C# is creating multiple methods with the same name but with different parameters.
- Give an example of method overloading in C#.
class Program { void Display(int num) { Console.WriteLine(num); } void Display(string str) { Console.WriteLine(str); } }
- What is method overriding in C#?
- Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its superclass.
- Give an example of method overriding in C#.
class Animal { public virtual void Sound() { Console.WriteLine("The animal makes a sound"); } } class Dog : Animal { public override void Sound() { Console.WriteLine("The dog barks"); } }
- What is the difference between method overloading and method overriding in C#?
- Overloading involves multiple methods with the same name but different parameters or signatures in the same scope. Overriding involves a subclass providing a different implementation for a method that already exists in its superclass.
- What is the base keyword in C#?
- The base keyword is used to access members of the base class from derived class.
- Give an example of the usage of the base keyword in C#.
class Animal { public void Eat() { Console.WriteLine("Eating..."); } } class Dog : Animal { public void DogEat() { base.Eat(); } }
- What is the this keyword in C#?
- The this keyword refers to the current instance of the class. It is used to differentiate between instance members and parameters when their names are the same.
- Give an example of the usage of the this keyword in C#.
class MyClass { int myVariable; public void SetVariable(int myVariable) { this.myVariable = myVariable; } }
- What is an abstract class in C#?
- An abstract class in C# is a class that cannot be instantiated and is usually used as a base class for other classes. Abstract classes can contain both abstract and non-abstract methods.
- Give an example of an abstract class in C#.
abstract class Animal { public abstract void Sound(); public void Eat() { Console.WriteLine("Eating..."); } }
- What are delegates in C#?
- Delegates in C# are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method.
- Give an example of a delegate in C#.
public delegate void DisplayMessage(string message); class Program { public static void ShowMessage(string message) { Console.WriteLine(message); } static void Main(string[] args) { DisplayMessage displayMessage = ShowMessage; displayMessage("Hello, World!"); } }
- What are events in C#?
- An event is a mechanism by which a class or object can notify other classes or objects when something of interest occurs.
- Give an example of an event in C#.
public delegate void MyEventHandler(); public class MyEvent { public event MyEventHandler Trigger; public void OnTrigger() { Trigger?.Invoke(); } } class Program { static void Main(string[] args) { MyEvent evt = new MyEvent(); evt.Trigger += RespondToEvent; evt.OnTrigger(); } static void RespondToEvent() { Console.WriteLine("Event triggered!"); } }
- What is an index in C#?
- An index in C# is a way of sorting and retrieving data from a data structure.
- How do you define an index in C#?
- You define an index in C# by implementing an indexer, which is a special kind of property.
- Give an example of an indexer in C#.
class MyClass { private string[] data = new string[5]; public string this[int index] { get { return data[index]; } set { data[index] = value; } } }
- What is a generic class in C#?
- A generic class in C# is a class that can work with any data type. When creating an instance of the generic class, you specify the specific types that you want the class to work with.
- Give an example of a generic class in C#.
public class MyGenericClass<T> { public T GenericProperty { get; set; } }
- What are the benefits of using generic classes in C#?
- Generic classes increase the reusability, type safety, and performance of your code by allowing you to write a class or method that can work with any data type.
- What is a generic method in C#?
- A generic method in C# is a method that can work with any data type. When calling the method, you specify the specific types that you want the method to work with.
- Give an example of a generic method in C#.
public void MyGenericMethod<T>(T parameter) { Console.WriteLine(parameter); }
- What is the difference between Array and ArrayList in C#?
- Array is a fixed-size data structure while ArrayList can dynamically increase or decrease in size. Array can have multiple dimensions but ArrayList is always one-dimensional.
- What is LINQ in C#?
- LINQ (Language Integrated Query) is a feature in C# that allows you to work with data in a more intuitive and declarative way. It allows you to query and manipulate data from various sources like databases, XML documents, ADO.NET datasets, and any collection of objects supporting IEnumerable or the generic IEnumerable<T> interface.
- Give an example of LINQ query in C#.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var evenNumbers = from number in numbers where number % 2 == 0 select number;
- What is Lambda Expression in C#?
- A lambda expression is an anonymous function that you can use to create delegates or expression tree types.
- Give an example of Lambda Expression in C#.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var evenNumbers = numbers.Where(x => x % 2 == 0);
- What is an interface in C#?
- An interface in C# is a contract that enforces a class to follow a certain blueprint of methods, properties, and events.
- Give an example of an interface in C#.
interface IAnimal { void Eat(); } class Dog : IAnimal { public void Eat() { Console.WriteLine("The dog is eating"); } }
- What is a namespace in C#?
- A namespace in C# is used to organize your code and is a collection of classes, interfaces, structs, enums and delegates.
- Give an example of a namespace in C#.
namespace MyNamespace { class MyClass { public void MyMethod() { Console.WriteLine("Hello, World!"); } } }
- What is an exception in C#?
- An exception in C# is an event that occurs during the execution of a program that disrupts the normal flow of the program’s instructions.
- Give an example of exception handling in C#.
try { int x = 0; int y = 5 / x; } catch(DivideByZeroException ex) { Console.WriteLine("Cannot divide by zero"); } finally { Console.WriteLine("Finally block executed"); }
- What is the difference between List and Dictionary in C#?
- List is an ordered collection of items that can be accessed by index. Dictionary is an unordered collection of key-value pairs, where each unique key is associated with a value.
- What are nullable types in C#?
- Nullable types in C# are value types that can be set to null. This is done using a question mark (?) after the value type. For example,
int?
represents a nullable int.
- Nullable types in C# are value types that can be set to null. This is done using a question mark (?) after the value type. For example,
- What are static classes in C#?
- A static class is a class that cannot be instantiated or extended. It contains only static members.
- Give an example of a static class in C#.
static class MyClass { public static int MyProperty { get; set; } public static void MyMethod() { Console.WriteLine("Hello, World!"); } }
- What is an extension method in C#?
- An extension method in C# is a method that we add to an existing class without modifying the class, creating a new class, or using inheritance.
- Give an example of an extension method in C#.
static class StringExtensions { public static string Reverse(this string str) { char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } }
- What is serialization in C#?
- Serialization in C# is the process of converting an object into a format that can be transported or saved to a file, database, or memory, and then recreating the object from that format.
- What is deserialization in C#?
- Deserialization in C# is the process of converting the serialized format back into a usable object.
- What are asynchronous methods in C#?
- Asynchronous methods in C# are methods that allow the program to perform other tasks while waiting for an operation to complete.
- Give an example of an asynchronous method in C#.
public async Task<string> GetWebContent(string url) { using (HttpClient client = new HttpClient()) { string content = await client.GetStringAsync(url); return content; } }
- What is multi-threading in C#?
- Multi-threading in C# is a feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU.
- What is the difference between Stack and Heap in C#?
- The Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer’s RAM. Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it’s allocation is dealt with when the program is compiled. On the other hand, the heap is used for dynamic memory allocation and it’s management is handled by the programmer.
- What are attributes in C#?
- Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection.
- Give an example of an attribute in C#.
[Obsolete("This method is obsolete, use NewMethod instead")] public void OldMethod() { } public void NewMethod() { }
- What are anonymous types in C#?
- Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level.
- Give an example of an anonymous type in C#.
var anonymousData = new { Name = "John", Age = 30 }; Console.WriteLine(anonymousData.Name); // Outputs "John" Console.WriteLine(anonymousData.Age); // Outputs 30
RELATED POSTS
View all