खोज…


वाक्य - विन्यास

  • Nullable<int> i = 10;
  • int? j = 11;
  • int? k = नल;
  • दिनांक और समय? DateOfBirth = DateTime.Now;
  • दशमलव? राशि = 1.0 मी;
  • bool? इसवलए = सत्य;
  • चार? पत्र = 'ए';
  • (प्रकार)? चर का नाम

टिप्पणियों

अशक्त प्रकार एक अंतर्निहित प्रकार के सभी मूल्यों के साथ-साथ null प्रतिनिधित्व कर सकते हैं।

सिंटेक्स T? Nullable<T> लिए आशुलिपि है

नल मान हैं System.ValueType वास्तव में वस्तुओं, इसलिए वे बॉक्सिंग और अनबॉक्स्ड जा सकता है। इसके अलावा, एक null वस्तु का null मान किसी संदर्भ वस्तु के null मूल्य के समान नहीं है, यह सिर्फ एक ध्वज है।

जब एक अशक्त वस्तु बॉक्सिंग होती है, तो नल मान null संदर्भ में बदल जाता है, और गैर-शून्य मान गैर-अशक्त अंतर्निहित प्रकार में परिवर्तित हो जाता है।

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

दूसरा नियम सही है, लेकिन विरोधाभास कोड:

DateTime? dt = new DateTime(2015, 12, 11);
var o = (object)dt;
var type = o.GetType(); // is DateTime, not Nullable<DateTime>

संक्षिप्त रूप में:

DateTime? dt = new DateTime(2015, 12, 11);
var type = dt.GetType(); // is DateTime, not Nullable<DateTime>

एक अशक्त शुरू

null मूल्यों के लिए:

Nullable<int> i = null;

या:

int? i = null;

या:

var i = (int?)null;

गैर-शून्य मानों के लिए:

Nullable<int> i = 0;

या:

int? i = 0;

जाँच करें कि क्या किसी Nullable का मान है

int? i = null;

if (i != null)
{
    Console.WriteLine("i is not null");
}
else
{
    Console.WriteLine("i is null");
}

जो निम्नानुसार है:

if (i.HasValue)
{
    Console.WriteLine("i is not null");
}
else
{
    Console.WriteLine("i is null");
}

एक अशक्त प्रकार का मान प्राप्त करें

निम्नलिखित अशक्त int को देखते हुए

int? i = 10;

यदि डिफ़ॉल्ट मान की आवश्यकता है, तो आप अशक्त coalescing ऑपरेटर का उपयोग करके एक असाइन कर सकते हैं, GetValueOrDefault विधि या असाइनमेंट से पहले nullable int HasValue जाँच करें।

int j = i ?? 0;
int j = i.GetValueOrDefault(0);
int j = i.HasValue ? i.Value : 0;

निम्नलिखित उपयोग हमेशा असुरक्षित होता है । अगर i रनटाइम में शून्य i , तो एक System.InvalidOperationException को फेंक दिया जाएगा। डिज़ाइन समय पर, यदि कोई मान सेट नहीं किया गया है, तो आपको Use of unassigned local variable 'i' त्रुटि Use of unassigned local variable 'i' मिलेगा।

int j = i.Value;

एक अशक्त से एक डिफ़ॉल्ट मूल्य प्राप्त करना

.GetValueOrDefault() विधि एक मान लौटाता है, भले ही .HasValue गुण गलत हो (मान गुण के विपरीत, जो अपवाद फेंकता है)।

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
    }
}

आउटपुट:

0
1

जांचें कि क्या सामान्य प्रकार का पैरामीटर एक अशक्त प्रकार है

public bool IsTypeNullable<T>()
{
    return Nullable.GetUnderlyingType( typeof(T) )!=null;
}

अशक्त प्रकारों का डिफ़ॉल्ट मान अशक्त है

public class NullableTypesExample
{
    static int? _testValue;

    public static void Main()
    {
        if(_testValue == null)
            Console.WriteLine("null");
        else
            Console.WriteLine(_testValue.ToString());
    }
}

आउटपुट:

शून्य

अंतर्निहित अशक्त का प्रभावी उपयोग बहस

कोई भी अशक्त प्रकार एक सामान्य प्रकार है। और कोई भी अशक्त प्रकार एक मूल्य प्रकार है।

कुछ ट्रिक्स हैं जो प्रतिबिंब / कोड-जनरेशन उद्देश्यों से संबंधित कोड बनाते समय Nullable.GetUnderlyingType विधि के परिणाम का प्रभावी ढंग से उपयोग करने की अनुमति देती हैं:

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)));
    }
}

उपयोगितायें:

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.");

आउटपुट:

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.

पुनश्च। NullableTypesCache को निम्नानुसार परिभाषित किया गया है:

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)); 
    }
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow