.NET

C# Language Essentials

Nullable types, generics, attributes, extension methods, type casting, advanced enums

15 면접 질문·
Junior
1

What is the correct syntax to declare a nullable int variable in C#?

답변

In C#, a nullable type for value types is declared with the '?' suffix after the type (int?, bool?, DateTime?). This allows value types to have a null value, which is not possible by default. Nullable types are useful to represent the absence of value in database or API scenarios.

2

Which operator allows providing a default value if a nullable variable is null?

답변

The null-coalescing operator '??' returns the left-hand value if it is not null, otherwise the right-hand value. This operator is very useful for handling nullable types and avoiding NullReferenceException. There is also the '??=' operator for conditional assignment introduced in C# 8.

3

What is the difference between casting with '(Type)' and 'as' in C#?

답변

The cast '(Type)object' throws an InvalidCastException if the conversion fails, while the 'as' operator returns null on failure. The 'as' operator only works with reference types and nullable value types. Use 'as' to avoid exceptions in cases where null is an acceptable value.

4

How to check if a nullable variable has a value before using it?

5

What is the syntax to declare a generic class with a type constraint?

+12 면접 질문

다음 면접을 위해 .NET을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기