Câu hỏi phỏng vấn C#
Câu hỏi

What is the use of Null Coalescing Operator ( ?? ) in C#?

Câu trả lời

The null coalescing operator (??) in C# is used to provide a default value for a nullable type or reference type when the value is null. It allows you to write more concise and readable code when handling null values.

Here's how it works:

csharp Copy
value ?? defaultValue

If value is not null, the null coalescing operator returns value. Otherwise, it returns defaultValue.

Some common use cases for the null coalescing operator include:

  1. Providing a default value for nullable types:
csharp Copy
int? number = null;
int result = number ?? 0; // result = 0
  1. Providing a default value for reference types:
csharp Copy
string name = null;
string d...
middle

middle

Gợi ý câu hỏi phỏng vấn

entry

What are Property Accessors?

expert

What are Circular References in C#?

junior

What is the difference between string and StringBuilder in C#?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào