.NET

C# Language Essentials

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

15 preguntas de entrevista·
Junior
1

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

Respuesta

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?

Respuesta

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#?

Respuesta

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 preguntas de entrevista

Domina .NET para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis