What is C#?
C# is a modern, object-oriented programming language developed by Microsoft as part of the .NET platform.
What are the different types of variables in C#?
- Value types: int, double, bool, char, enum, struct.
- Reference types: string, object, class, interface.
- Other types: var, dynamic.
Explain the difference between value types and reference types.
- Value types store the actual data, while reference types store references to the data.
- Value types are stored on the stack, while reference types are stored on the heap.
- Value types are passed by value, while reference types are passed by reference.
What is the difference between `int` and `int?` in C#?
- `int` is a non-nullable value type that cannot have a null value.
- `int?` (nullable int) allows the variable to have a null value in addition to integer values.
Explain the `var` keyword in C#.
- `var` is an implicitly typed variable whose type is inferred by the compiler based on the assigned value.
- It allows for cleaner code and reduces verbosity.
What is boxing and unboxing in C#?
- Boxing is the process of converting a value type to a reference type.
- Unboxing is the process of converting a boxed value back to its original value type.
What is the difference between `string` and `StringBuilder` in C#?
- `string` is an immutable type used to represent a sequence of characters.
- `StringBuilder` is a mutable type designed for efficient string manipulation.
What is a constant variable in C#?
- A constant variable is a variable whose value cannot be changed after initialization.
- It is declared using the `const` keyword.
What are implicit and explicit conversions in C#?
- Implicit conversions occur automatically and do not require explicit casting.
- Explicit conversions require explicit casting using the appropriate syntax.
What is the purpose of the `dynamic` keyword in C#?
- The `dynamic` keyword allows for late binding and dynamic typing in C#.
- It defers type checking until runtime, providing more flexibility at the cost of reduced type safety.
What is the difference between `readonly` and `const` in C#?
- `readonly` variables can be assigned a value either at the time of declaration or within a constructor, and their value cannot be changed afterward.
- `const` variables must be assigned a value at the time of declaration, and their value cannot be changed.
What are nullable value types in C#?
- Nullable value types, denoted by `type?`, allow value types to have a null value in addition to their normal range of values.
- They are useful when a value type needs to represent the absence of a value.
What is the difference between `is` and `as` operators in C#?
- The `is` operator checks if an object can be cast to a specified type, returning `true` or `false`.
- The `as` operator attempts to cast an object to a specified type, returning `null` if the cast fails instead of throwing an exception.
Explain the concept of null-coalescing operator (`??`) in C#.
- The null-coalescing operator provides a concise way to assign a default value when a nullable expression evaluates to `null`.
- It returns the left-hand operand if it is not `null`, otherwise it returns the right-hand operand.
What are the released data types in C#?
- `decimal`: Represents decimal numbers with higher precision for financial and monetary calculations.
- `DateTime`: Represents dates and times.
- `TimeSpan`: Represents a duration of time.
- `Guid`: Represents a globally unique identifier.
- `Nullable<T>`: Represents a value type that can also be assigned a null value.
Explain the concept of nullable value types in C#.
- Nullable value types allow value types to have a null value in addition to their normal range of values.
- They are useful when a value type needs to represent the absence of a value.
- The `Nullable<T>` structure is used to create nullable value types.
How do you declare and use an enum in C#?
- An enum is declared using the `enum` keyword, followed by the name and a set of named constants.
- Enums provide a way to define a set of related values, such as days of the week or status codes.
- Enum values can be accessed using dot notation, like `EnumName.EnumValue`.
What is the purpose of the `var` keyword in C#? How is it different from `dynamic`?
- The `var` keyword allows the compiler to infer the type of a variable based on the assigned value.
- It is resolved at compile-time and provides static typing.
- In contrast, `dynamic` allows for late binding and dynamic typing, with type checking deferred until runtime.
How do you convert between data types in C#?
- Implicit conversions occur automatically when the conversion does not involve a data loss.
- Explicit conversions can be done using casting or conversion methods like `Convert.ToInt32()` or `int.Parse()`.
How do you handle null values in C#?
- You can check for null values using the null-coalescing operator (`??`) or the null-conditional operator (`?.`).
- You can also use conditional statements like `if (variable != null)` or null-checking methods like `IsNullOrWhiteSpace()` for strings.
How do you format strings in C#?
- String formatting can be done using composite formatting with `string.Format()`, interpolation with `$"..."`, or using the `String.Format()` method.
- Additionally, you can use formatting placeholders like `{0}`, `{1}`, etc., to specify the position of arguments within the format string.
How do you work with date and time in C#?
- C# provides the `DateTime` struct for working with dates and times.
- You can perform various operations like adding or subtracting time, formatting dates, comparing dates, and extracting components using properties and methods of the `DateTime` struct.
Explain the difference between `StringBuilder` and `String` in C#.
- `StringBuilder` is used for efficient string manipulation when there are frequent modifications to a string.
- `StringBuilder` is mutable and provides methods like `Append()`, `Insert()`, and `Replace()` for modifying strings without creating new instances.
- `String` is immutable, meaning that once created, it cannot be modified.
No comments:
Post a Comment