Questions:
- What is the purpose of the "as" keyword in C#?
- Explain the difference between "as" and "is" keywords in C#.
- What is the purpose of the "nameof" operator in C#?
- How do you implement conditional access in C#?
- What is pattern matching in C#?
1. Purpose of the "as" Keyword in C#:
The as
keyword in C# is used for safe type casting. It attempts to cast an object to a specified type, and if the cast fails, it returns null
instead of throwing an exception. This is useful for avoiding exceptions when you are unsure if the conversion will succeed.
object obj = "Hello, world!"; string str = obj as string; if (str != null) { Console.WriteLine(str); // Output: Hello, world! }
In this example, obj
is safely cast to string
using the as
keyword. If obj
were not a string, str
would be null
.
2. Difference Between "as" and "is" Keywords in C#:
is
: Checks if an object is of a specified type and returnstrue
orfalse
.
object obj = "Hello, world!"; if (obj is string) { string str = (string)obj; Console.WriteLine(str); // Output: Hello, world! }
as
: Attempts to cast an object to a specified type and returnsnull
if the cast fails.
object obj = "Hello, world!"; string str = obj as string; if (str != null) { Console.WriteLine(str); // Output: Hello, world! }
In summary, is
is used to check the type, and as
is used to cast the type safely.
3. Purpose of the "nameof" Operator in C#:
The nameof
operator returns the name of a variable, type, or member as a string. It is useful for obtaining the name of a symbol in a type-safe manner, which is particularly helpful in logging, error messages, and code maintenance.
public class Person { public string Name { get; set; } } // Usage Person person = new Person(); Console.WriteLine(nameof(person.Name)); // Output: Name
In this example, nameof(person.Name)
returns the string "Name".
4. Implementing Conditional Access in C#:
Conditional access is implemented using the null-conditional operator ?.
. It allows you to safely access members and elements only when the object is not null
, preventing NullReferenceException
.
Person person = null; string name = person?.Name; // name will be null if person is null Person person2 = new Person(); string name2 = person2?.Name; // name2 will be the value of person2.Name if person2 is not null
In this example, the ?.
operator safely accesses the Name
property only if person
is not null
.
5. Pattern Matching in C#:
Pattern matching in C# enhances type checking and casting capabilities, allowing for more readable and concise code. It can be used with the is
keyword, switch
statements, and more.
Using is
with Pattern Matching:
object obj = "Hello, world!"; if (obj is string str) { Console.WriteLine(str); // Output: Hello, world! }
In this example, the is
keyword checks the type and declares a variable of that type in one step.
Using Pattern Matching in switch
Statements:
public static string GetShapeType(object shape) { switch (shape) { case Circle c: return $"Circle with radius {c.Radius}"; case Rectangle r: return $"Rectangle with width {r.Width} and height {r.Height}"; default: return "Unknown shape"; } } public class Circle { public double Radius { get; set; } } public class Rectangle { public double Width { get; set; } public double Height { get; set; } } // Usage object shape = new Circle { Radius = 5 }; Console.WriteLine(GetShapeType(shape)); // Output: Circle with radius 5
In this example, pattern matching is used in a switch
statement to identify the type of the shape
object and extract its properties.
These explanations and examples should help you understand the purpose and usage of the as
keyword, the difference between as
and is
, the nameof
operator, conditional access, and pattern matching in C#.
Comments
Post a Comment