- What does the ?: operator do in C#? Answer
The ?: operator is equivalent to an if-else statement except the result is an expression. It
has the syntax:
cond ? a : b
If cond evaluates
to true then a is returned. Otherwise b is returned.
a and b must be of the same type. This operator is useful
in expressions such as assignment expressions.
string newValue = (value != null) ? value.Trim() : "";
- What does the ?? operator do in C#? Answer
This operator was added to C# 2.0. It is similar to the ?: operator in that it evaluates a condition and returns one of two values. The
syntax is:
reference ?? a
If reference is null
then it returns a otherwise it returns reference.
string newValue = value ?? "";
- What does the as operator do in C#? Answer
The as operator is a typecast operator in C#. The syntax is:
value as type
If value is of the given type
or can be converted to the type then value is returned as that type.
Otherwise null is returned. This is the preferred manner
in which to do runtime type checking.
public void Foo ( object value )
{
IMyInterface ifc = value as IMyInterface;
if (ifc != null)
{
};
}
- What does the is operator do in C#? Answer
The is operator is a type checking operator. The syntax is:
value is type
The operator returns true if value is of the given type or can be
converted to the type or false otherwise. This operator is good for conditional
checking but you should use as if you are going to do something
with the value.
if (value is typeof(string))
{
}
- What does CTS stand for in .NET? Answer
CTS stands for Common Type System. The CTS provides a common set of types for use
in all .NET languages. All .NET languages support the given type although
most languages will provide alternative names for the types. The CTS is part
of what makes compatibility across languages possible.
- What does CLS stand for in .NET? Answer
CLS stands for Common Language Specification. The CLS identifies the common features
that all .NET languages need to support. If an assembly, type, type member or other .NET
feature is marked as CLS compliant then any .NET language can use the feature.
It is important to try to make all types and type members CLS compliant whenever
possible.
- For each of the following types, are they CLS compliant? Answer
- Byte
- UInt16
- Int32
- Double
- String
- Decimal
- UInt64
- Boolean
- Yes
- No
- Yes
- Yes
- Yes
- Yes
- No
- Yes
- What is a nullable type (in terms of v2 and not in regard to reference types)? Answer
A nullable type is a value type that can either hold a null value
or a value type value. Nullable types were added in v2 in order to support
better integration with databases where column values can be null. In reality
a nullable type is simply a structure with space for the value type value and a
flag indicating if the value is null.
(Updated 28 Dec 2006 w/ comments received from James Curran)
- What is the difference between a reference type and a value type? Answer
A type that is represented as a pointer (or referenced) to the actual data in .NET.
Two or more objects that are assigned to the same reference type object represent
the same object. Any changes to the first object also change the second object.
In .NET reference types include classes, interfaces, strings and delegates.
A value type is any type that is not a reference type. A value type represents
a block of memory. Each variable of a value type represents a distinct copy
of the type. A change in one copy does not impact the other. Value types
have serious limitations in .NET. In .NET value types include structures,
enumerations and all primitive types
- Is string a reference or value type? Answer
It is a reference type although it exhibits value semantics. Assigning a string
variable to another string variable effectively creates a copy. In reality
whenever a string value is changed a new copy of the string is created in memory.
This is known as being immutable.