C# Fundamentals Flashcards

1
Q

What are Generics in C#?

A

Generics allow you to create classes, methods, and structures that can work with different data types without specifying them in advance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are Generics useful in achieving type safety?

A

Generics ensure type safety by allowing you to specify the type of data a class or method works with at compile-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the difference between a Generic class and a Generic method.

A

A Generic class is a class that can work with multiple data types, while a Generic method is a method within a class that can take different types as arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Generic constraint?

A

A Generic constraint restricts the types that can be used with a Generic class or method, ensuring they meet certain criteria.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you specify a Generic constraint in C#?

A

You can specify a Generic constraint using the where keyword followed by the constraint(s) in angle brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Covariant Generic?

A

Covariant Generics allow you to use a derived class where a base class is expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Contravariant Generic?

A

Contravariant Generics allow you to use a base class where a derived class is expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of using Generics in a collection.

A

List<T> myList = new List<T>();</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you use Generics with value types and reference types?

A

Yes, Generics can be used with both value types and reference types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of the default keyword in Generics?

A

The default keyword returns the default value of a type in Generics, which is null for reference types and a zero-initialized value for value types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Delegate in C#?

A

A Delegate is a type that defines a method signature, allowing you to reference methods as parameters and invoke them indirectly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Multicast Delegate?

A

A Multicast Delegate is a delegate that holds references to multiple methods and can call them all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you declare a Delegate?

A

delegate void MyDelegate(int x);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the concept of Events in C#.

A

Events are a way to provide notifications when a particular action occurs in your program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a Delegate and an Event?

A

A Delegate is a function pointer, while an Event is a higher-level construct that provides a layer of encapsulation and protection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you subscribe to an Event?

A

You use the += operator to add a method to the Event’s invocation list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can you prevent race conditions in Event handling?

A

By using a thread-safe approach like locking or using the Monitor class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the significance of the sender and e parameters in an Event handler?

A

The sender parameter references the object that raised the event, and the e parameter contains additional event-specific information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Can you explain the purpose of the EventHandler and EventHandler<T> delegates?</T>

A

These are predefined delegate types that are commonly used for event handling. The non-generic EventHandler takes sender and e parameters, while EventHandler<T> takes an additional event argument.</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can you unsubscribe from an Event?

A

You use the -= operator to remove a method from the Event’s invocation list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is LINQ?

A

LINQ is a set of language extensions that allow you to perform queries on various data sources directly from C#.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Name the different types of LINQ.

A

LINQ to Objects, LINQ to SQL, LINQ to XML, LINQ to Entities, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the purpose of the IEnumerable interface in LINQ?

A

IEnumerable is the base interface for all LINQ query operations. It provides an iterator for collections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Explain the difference between Deferred Execution and Immediate Execution in LINQ.

A

Deferred Execution means that the query isn’t executed until the data is actually enumerated. Immediate Execution means the query is executed immediately when it’s defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How does LINQ improve code readability and maintainability?

A

LINQ provides a more declarative and intuitive way to write queries, making the code easier to understand and maintain.

26
Q

What is the role of the var keyword in LINQ queries?

A

The var keyword allows the compiler to infer the type of the query result, making the code more concise.

27
Q

How can you filter data using LINQ?

A

By using the where clause to specify a condition that filters the data.

28
Q

What is the purpose of the orderby clause in LINQ?

A

The orderby clause is used to sort the query results based on specified criteria.

29
Q

Explain the difference between FirstOrDefault() and SingleOrDefault() methods in LINQ.

A

FirstOrDefault() returns the first element that matches the condition or a default value if no match is found. SingleOrDefault() does the same but throws an exception if more than one match is found.

30
Q

How can you perform a Join operation in LINQ?

A

By using the join keyword to combine data from two or more collections based on a common key.

31
Q

What is Asynchronous Programming in C#?

A

Asynchronous programming allows you to write code that can execute concurrently without blocking the main thread.

32
Q

What are the benefits of using async and await keywords?

A

They simplify asynchronous code by allowing you to write asynchronous operations in a more sequential manner.

33
Q

How does the async keyword affect a method?

A

The async keyword indicates that a method contains asynchronous operations and can be awaited.

34
Q

What does the await keyword do?

A

The await keyword is used to pause the execution of an async method until the awaited task completes.

35
Q

Can you await multiple tasks simultaneously?

A

Yes, you can use Task.WhenAll() to await multiple tasks concurrently.

36
Q

Explain the concept of the Synchronization Context in asynchronous programming.

A

The Synchronization Context determines which thread is used to execute the continuation after an await. It’s essential for UI applications to ensure updates are done on the UI thread.

37
Q

What is the purpose of the ConfigureAwait() method?

A

ConfigureAwait() allows you to control whether the continuation after an await should marshal back to the original synchronization context.

38
Q

What is the difference between Asynchronous and Parallel programming?

A

Asynchronous programming focuses on managing I/O-bound operations with non-blocking code. Parallel programming focuses on utilizing multiple processors/cores for CPU-bound operations.

39
Q

How can you cancel an asynchronous operation?

A

By using the CancellationToken mechanism and passing it to the asynchronous method.

40
Q

What is the Task-based Asynchronous Pattern (TAP)?

A

TAP is a design pattern in C# for creating asynchronous methods that return a Task or Task<T>.</T>

41
Q

What is an Exception in C#?

A

An Exception is a runtime error that occurs when the normal flow of a program is disrupted.

42
Q

How do you handle exceptions in C#?

A

By using try, catch, finally blocks to catch and handle exceptions.

43
Q

What is the base class for all exceptions in C#?

A

The base class for all exceptions is System.Exception.

44
Q

Explain the purpose of the finally block in exception handling.

A

The finally block is used to contain code that will be executed regardless of whether an exception is thrown or not.

45
Q

Can you have multiple catch blocks for a single try block?

A

Yes, you can have multiple catch blocks to handle different types of exceptions.

46
Q

What is the difference between throw and throw ex?

A

throw preserves the original call stack, while throw ex resets the call stack, potentially leading to loss of debugging information.

47
Q

What is the difference between checked and unchecked exceptions?

A

In C#, all exceptions are unchecked, meaning the compiler does not enforce handling or declaration of exceptions.

48
Q

How can you create a custom exception in C#?

A

By creating a class that derives from System.Exception.

49
Q

Explain the concept of Exception Propagation.

A

Exception Propagation refers to the process of an exception being passed from one method to another if it’s not handled at the current level.

50
Q

What is the purpose of the using statement in relation to exception handling?

A

The using statement ensures that resources like file streams or database connections are properly disposed of, even if an exception is thrown.

51
Q

How does Garbage Collection work in C#?

A

Garbage Collection automatically reclaims memory used by objects that are no longer reachable or referenced.

52
Q

What is the purpose of the Finalizer (destructor) in C#?

A

The Finalizer is used to perform cleanup tasks on an object before it’s reclaimed by the Garbage Collector.

53
Q

Explain the difference between a Reference Type and a Value Type in relation to memory management.

A

Reference Types are allocated on the heap and managed by the Garbage Collector. Value Types are typically allocated on the stack or inline within other objects.

54
Q

What is the IDisposable interface used for?

A

The IDisposable interface is used to release unmanaged resources like file handles or network connections.

55
Q

How can you ensure timely disposal of unmanaged resources?

A

By implementing the IDisposable interface and using the using statement.

56
Q

What is the Large Object Heap (LOH)?

A

The LOH is a special area of memory used to store large objects that are 85,000 bytes or more in size.

57
Q

Explain the concepts of Generational Garbage Collection.

A

Generational Garbage Collection divides objects into three generations (0, 1, 2) based on their age. Younger objects (Gen 0) are collected more frequently, while older objects (Gen 2) are collected less often.

58
Q

How can you force an object to be immediately garbage collected?

A

By calling the GC.Collect() method, although it’s generally not recommended due to its impact on performance.

59
Q

What is the purpose of the GC.SuppressFinalize() method?

A

It prevents the execution of an object’s finalizer during garbage collection.

60
Q

How can you track memory usage and performance bottlenecks in a .NET application?

A

By using tools like Profilers, Memory Profilers, and Performance Profilers.