C# Flashcards

1
Q

Difference between Array and Array List?

A

Array - similar variables clubbed together
Array List - can have different types of variables and has dynamic memory allocation.

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

Generics?

A

Generics are not linked to a specific type. The type is substituted at runtime.

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

Extension Method?

A

Static methods added by passing (this <class_name> name). Can be used as if method part of the class.</class_name>

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

ref vs out?

A

Both used to pass argument by reference.
ref - requires the variable to be initialized before passing to the method.
out - allows passing an uninitialized variable, and the method must assign a value to it before returning.

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

Abstract class vs interface

A

Abstract - contain both implementation and declarations.
It can have constructor. Useful when you need to share some implementation
Interface - define only the signatures. Useful when unrelated classes need to share functionality

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

Managed vs Unmanaged

A

Managed - Common Language Runtime, features like garbage collection.
Unmanaged - C++, runs directly, more control but also more work.

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

Partial Class

A
  • Allows splitting a class definition across multiple files.
  • All parts are combined into one class during compilation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reflection

A

It allows to read the metadata from datatype during the run time

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

Equals() method vs Equality (==)

A

== compares values for value types and references for reference types by default. Can be overloaded.

Equals - Compares object contents if overridden. For reference types, the default compares references, but it is usually overridden to provide meaningful equality.

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

const in C#

A
  • Compile-time constant, must be assigned at declaration.
  • Implicitly static, cannot change after being set.
  • Only works with value types and immutable types like string.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

readonly in C#

A
  • Runtime constant, can be assigned in the constructor.
  • Can be used with both value types and reference types.
  • Allows different values for different instances (unless static).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Indexer in C#

A

Allow objects to be accessed using array-like syntax ([]).

Syntax: Defined using the this keyword with one or more parameters.
Get/Set: Can define both get and set accessors to retrieve and assign values.

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

Early Binding vs Late Binding

A

When an object is assigned to an object variable in C#, the .NET framework performs the binding.

Early - Binding done during compile time
Late - Binding done during run time

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

Boxing vs Unboxing

A

They help in casting data types

Boxing - Converts value type to reference types
Unboxing - Converts reference types to value types

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

Access Modifiers in C#

A

Private - within class
Protected - within class and who inherit
Internal - current assembly
Public - anywhere in the code

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

Finalize block vs Finalize

A

Finalize block - Always executes after try-catch
Finalize - Always executes before garbage collection, to clean unmanaged code

17
Q

Sealed class

A

wAdds restriction, such that it cannot be inherited.
Works well for singleton pattern

18
Q

Struct vs Class

A

Struct - Value Type, Copied, no parameterless constructor
Class - Reference Type, can be null

19
Q

is vs as

A

is: Used for type checking.
if (obj is string) {}

as: Used for type casting
string str = obj as string;

20
Q

Attribute

A

Allows attaching metadata to class, methods, properties. Uses Reflection during runtime to get the information.

21
Q

namespace

A

Helps to organize code into logical groups.

22
Q

Race Condition

A

When two thread access the same resource and try to change it a the same time.

23
Q

Threadpool

A

Pool of worker threads managed by CLR for

24
Q

Delegate

A

To encapsulate a piece of code so that it can be passed around and executed as necessary
in a type-safe fashion in terms of the return type and parameters.

25
Closure
A closure is able to access all the variables that are in scope at the point of its declaration, even if those variables normally wouldn’t be available anymore when the delegate is executed.
26
Iterator
An iterator is a method or property implemented with an iterator block, which is in turn just a block of code using the yield return or yield break statements.
27
IEnumerable vs IEnumerator
An IEnumerable is a sequence that can be iterated over, whereas an IEnumerator is like a cursor within a sequence. Multiple IEnumerator instances can probably iterate over the same IEnumerable without changing its state at all. If that didn’t make much sense, you might want to think about an IEnumerable as a book and an IEnumerator as a bookmark.
28
What does it mean that Iterators are lazy?
This means that the elements in the sequence are generated only when they are actually needed, rather than all at once. It stops executing when any of the following occurs: - An exception is thrown. - It reaches the end of the method. - It reaches a yield break statement. - It has evaluated the operand to a yield return statement, so it is ready to yield the value.