# Nullable wird initialisiert
Für null
Werte:
Nullable<int> i = null;
Oder:
int? i = null;
Oder:
var i = (int?)null;
Für Nicht-Null-Werte:
Nullable<int> i = 0;
Oder:
int? i = 0;
# Prüfen, ob ein Nullable einen Wert hat
int? i = null;
if (i != null)
{
Console.WriteLine("i is not null");
}
else
{
Console.WriteLine("i is null");
}
Was dasselbe ist wie:
if (i.HasValue)
{
Console.WriteLine("i is not null");
}
else
{
Console.WriteLine("i is null");
}
# Ruft den Wert eines nullable-Typs ab
Gegeben nach nullable int
int? i = 10;
Falls ein Standardwert benötigt wird, können Sie einen mit dem Null-Coalescing-Operator GetValueOrDefault
zuweisen Methode oder prüfen, ob nullable int HasValue
vor der Zuordnung.
int j = i ?? 0;
int j = i.GetValueOrDefault(0);
int j = i.HasValue ? i.Value : 0;
Die folgende Verwendung ist immer unsicher . Wenn i
ist zur Laufzeit null, ein System.InvalidOperationException
wird geworfen. Wenn zur Entwurfszeit kein Wert festgelegt ist, erhalten Sie einen Use of unassigned local variable 'i'
Fehler.
int j = i.Value;
# Abrufen eines Standardwerts von einer nullable
Die .GetValueOrDefault()
-Methode gibt einen Wert zurück, auch wenn .HasValue
-Eigenschaft ist falsch (im Gegensatz zur Value-Eigenschaft, die eine Ausnahme auslöst).
class Program
{
static void Main()
{
int? nullableExample = null;
int result = nullableExample.GetValueOrDefault();
Console.WriteLine(result); // will output the default value for int - 0
int secondResult = nullableExample.GetValueOrDefault(1);
Console.WriteLine(secondResult) // will output our specified default - 1
int thirdResult = nullableExample ?? 1;
Console.WriteLine(secondResult) // same as the GetValueOrDefault but a bit shorter
}
}
Ausgabe:
0
1
# Prüfen, ob ein generischer Typparameter ein Nullable-Typ ist
public bool IsTypeNullable<T>()
{
return Nullable.GetUnderlyingType( typeof(T) )!=null;
}
# Der Standardwert von nullable-Typen ist null
public class NullableTypesExample
{
static int? _testValue;
public static void Main()
{
if(_testValue == null)
Console.WriteLine("null");
else
Console.WriteLine(_testValue.ToString());
}
}
Ausgabe:
null
# Effektive Nutzung des zugrundeliegenden Nullable-Arguments
Jeder Nullable-Typ ist ein generischer Typ Typ. Und jeder Nullable-Typ ist ein Wert Typ.
Es gibt einige Tricks, die eine effektive Nutzung ermöglichen das Ergebnis der Nullable.GetUnderlyingType-Methode beim Erstellen von Code im Zusammenhang mit Reflektions-/Codegenerierungszwecken:
public static class TypesHelper {
public static bool IsNullable(this Type type) {
Type underlyingType;
return IsNullable(type, out underlyingType);
}
public static bool IsNullable(this Type type, out Type underlyingType) {
underlyingType = Nullable.GetUnderlyingType(type);
return underlyingType != null;
}
public static Type GetNullable(Type type) {
Type underlyingType;
return IsNullable(type, out underlyingType) ? type : NullableTypesCache.Get(type);
}
public static bool IsExactOrNullable(this Type type, Func<Type, bool> predicate) {
Type underlyingType;
if(IsNullable(type, out underlyingType))
return IsExactOrNullable(underlyingType, predicate);
return predicate(type);
}
public static bool IsExactOrNullable<T>(this Type type)
where T : struct {
return IsExactOrNullable(type, t => Equals(t, typeof(T)));
}
}
Die Verwendung:
Type type = typeof(int).GetNullable();
Console.WriteLine(type.ToString());
if(type.IsNullable())
Console.WriteLine("Type is nullable.");
Type underlyingType;
if(type.IsNullable(out underlyingType))
Console.WriteLine("The underlying type is " + underlyingType.Name + ".");
if(type.IsExactOrNullable<int>())
Console.WriteLine("Type is either exact or nullable Int32.");
if(!type.IsExactOrNullable(t => t.IsEnum))
Console.WriteLine("Type is neither exact nor nullable enum.");
Ausgabe:
System.Nullable`1[System.Int32]
Type is nullable.
The underlying type is Int32.
Type is either exact or nullable Int32.
Type is neither exact nor nullable enum.
PS. Die NullableTypesCache
ist wie folgt definiert:
static class NullableTypesCache {
readonly static ConcurrentDictionary<Type, Type> cache = new ConcurrentDictionary<Type, Type>();
static NullableTypesCache() {
cache.TryAdd(typeof(byte), typeof(Nullable<byte>));
cache.TryAdd(typeof(short), typeof(Nullable<short>));
cache.TryAdd(typeof(int), typeof(Nullable<int>));
cache.TryAdd(typeof(long), typeof(Nullable<long>));
cache.TryAdd(typeof(float), typeof(Nullable<float>));
cache.TryAdd(typeof(double), typeof(Nullable<double>));
cache.TryAdd(typeof(decimal), typeof(Nullable<decimal>));
cache.TryAdd(typeof(sbyte), typeof(Nullable<sbyte>));
cache.TryAdd(typeof(ushort), typeof(Nullable<ushort>));
cache.TryAdd(typeof(uint), typeof(Nullable<uint>));
cache.TryAdd(typeof(ulong), typeof(Nullable<ulong>));
//...
}
readonly static Type NullableBase = typeof(Nullable<>);
internal static Type Get(Type type) {
// Try to avoid the expensive MakeGenericType method call
return cache.GetOrAdd(type, t => NullableBase.MakeGenericType(t));
}
}
#-Syntax
Nullable<int> i = 10;
- int? j =11;
- int? k =null;
- DatumUhrzeit? DateOfBirth =DateTime.Now;
- dezimal? Betrag =1,0 Mio.;
- boolesch? IsAvailable =true;
- char? Buchstabe ='a';
- (Typ)? Variablenname
# Bemerkungen
Nullable-Typen können alle Werte eines zugrunde liegenden Typs sowie null
darstellen .
Die Syntax T?
ist eine Abkürzung für Nullable<T>
Nullwerte sind System.ValueType
eigentliche Objekte, sodass sie ein- und ausgepackt werden können. Außerdem null
Der Wert eines Nullable-Objekts ist nicht dasselbe wie null
Wert eines Referenzobjekts, es ist nur ein Flag.
Beim Boxen eines Nullable-Objekts wird der Nullwert in null
konvertiert Verweis, und ein Nicht-Null-Wert wird in einen zugrunde liegenden Typ konvertiert, der keine Nullwerte zulässt.
DateTime? dt = null;
var o = (object)dt;
var result = (o == null); // is true
DateTime? dt = new DateTime(2015, 12, 11);
var o = (object)dt;
var dt2 = (DateTime)dt; // correct cause o contains DateTime value
Die zweite Regel führt zu korrektem, aber paradoxem Code:
DateTime? dt = new DateTime(2015, 12, 11);
var o = (object)dt;
var type = o.GetType(); // is DateTime, not Nullable<DateTime>
In Kurzform:
DateTime? dt = new DateTime(2015, 12, 11);
var type = dt.GetType(); // is DateTime, not Nullable<DateTime>