Get ready for your next interview with our comprehensive question library
Value Types:
int
, bool
, char
, struct
, enum
class
, string
, array
, delegate
null
const:
public class Example
{
const int MAX_SIZE = 100; // Compile-time constant
readonly DateTime createdDate; // Runtime constant
public Example()
{
createdDate = DateTime.Now; // Can set in constructor
}
}
Namespaces organize code into logical groups and prevent naming conflicts. They provide a hierarchical organization system for types.
Benefits:
namespace MyCompany.DataAccess
{
public class DatabaseConnection { }
}
namespace MyCompany.UI
{
public class DatabaseConnection { } // Same name, different namespace
}
var
enables implicit typing where the compiler determines the type from the assigned value. The variable is still strongly typed.
Best Practices:
var number = 42; // int
var text = "Hello"; // string
var list = new List<int>(); // List<int>
// Required for anonymous types
var person = new { Name = "John", Age = 30 };
1. Encapsulation: Bundling data and methods together, hiding internal implementation details.
2. Inheritance: Creating new classes based on existing classes, promoting code reuse.
3. Polymorphism: Objects of different types can be treated as instances of the same type through a common interface.
4. Abstraction: Hiding complex implementation details while showing only essential features.
Method Overloading:
virtual
in base class and override
in derived class// Overloading
public class Calculator
{
public int Add(int a, int b) { return a + b; }
public double Add(double a, double b) { return a + b; }
}
// Overriding
public class Shape
{
public virtual double GetArea() { return 0; }
}
public class Circle : Shape
{
public override double GetArea() { return Math.PI * radius * radius; }
}
public: Accessible from anywhere.
private: Accessible only within the same class.
protected: Accessible within the class and its derived classes.
internal: Accessible within the same assembly.
protected internal: Accessible within the same assembly or derived classes.
private protected: Accessible within the same assembly and only in derived classes.
Constructors are special methods called when an object is created. They initialize the object's state.
Types:
public class Person
{
// Parameterized constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Static constructor
static Person()
{
// Initialize static members
}
}
Nullable types allow value types to have null
values. Useful for database scenarios where values might be undefined.
int? nullableInt = null;
bool? nullableBool = null;
// Checking for null
if (nullableInt.HasValue)
{
int value = nullableInt.Value;
}
// Null coalescing operator
int result = nullableInt ?? 0; // Use 0 if null
string:
// Inefficient - creates multiple string objects
string result = "";
for (int i = 0; i < 1000; i++)
{
result += i.ToString(); // Creates new string each time
}
// Efficient - uses internal buffer
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
sb.Append(i); // Modifies existing buffer
}
string result = sb.ToString();
Implicit Conversion:
// Implicit conversion
int intValue = 123;
double doubleValue = intValue; // Automatic conversion
// Explicit conversion
double largeValue = 123.456;
int smallValue = (int)largeValue; // Manual cast, loses decimal part
// Using Convert class
string numberString = "123";
int convertedValue = Convert.ToInt32(numberString);
Parse():
out
parameter for converted value// Parse - throws exception on failure
try
{
int value1 = int.Parse("123");
int value2 = int.Parse("abc"); // Throws FormatException
}
catch (FormatException ex) { }
// TryParse - safe conversion
if (int.TryParse("123", out int value3))
{
// Conversion successful
}
else
{
// Conversion failed, value3 is 0
}
Array:
// Array
int[] array = new int[5];
// ArrayList (avoid)
ArrayList arrayList = new ArrayList();
arrayList.Add(1); // Boxing occurs
arrayList.Add("text"); // No compile-time type checking
// List<T> (preferred)
List<int> list = new List<int>();
list.Add(1); // No boxing
// list.Add("text"); // Compile-time error
List-based:
List<T>
- Dynamic arrayLinkedList<T>
- Doubly linked listHashSet<T>
- Unique elements, fast lookupSortedSet<T>
- Sorted unique elementsDictionary<K,V>
- Key-value pairs, fast lookupSortedDictionary<K,V>
- Sorted key-value pairsConcurrentDictionary<K,V>
- Thread-safe dictionaryQueue<T>
- FIFO collectionStack<T>
- LIFO collectionUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime