C# Flashcards
Difference between Array and Array List?
Array - similar variables clubbed together
Array List - can have different types of variables and has dynamic memory allocation.
Generics?
Generics are not linked to a specific type. The type is substituted at runtime.
Extension Method?
Static methods added by passing (this <class_name> name). Can be used as if method part of the class.</class_name>
ref vs out?
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.
Abstract class vs interface
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
Managed vs Unmanaged
Managed - Common Language Runtime, features like garbage collection.
Unmanaged - C++, runs directly, more control but also more work.
Partial Class
- Allows splitting a class definition across multiple files.
- All parts are combined into one class during compilation.
Reflection
It allows to read the metadata from datatype during the run time
Equals() method vs Equality (==)
== 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.
const in C#
- 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.
readonly in C#
- 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).
Indexer in C#
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.
Early Binding vs Late Binding
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
Boxing vs Unboxing
They help in casting data types
Boxing - Converts value type to reference types
Unboxing - Converts reference types to value types
Access Modifiers in C#
Private - within class
Protected - within class and who inherit
Internal - current assembly
Public - anywhere in the code
Finalize block vs Finalize
Finalize block - Always executes after try-catch
Finalize - Always executes before garbage collection, to clean unmanaged code
Sealed class
wAdds restriction, such that it cannot be inherited.
Works well for singleton pattern
Struct vs Class
Struct - Value Type, Copied, no parameterless constructor
Class - Reference Type, can be null
is vs as
is: Used for type checking.
if (obj is string) {}
as: Used for type casting
string str = obj as string;
Attribute
Allows attaching metadata to class, methods, properties. Uses Reflection during runtime to get the information.
namespace
Helps to organize code into logical groups.
Race Condition
When two thread access the same resource and try to change it a the same time.
Threadpool
Pool of worker threads managed by CLR for
Delegate
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.