मैं आपसे सहमत हूं कि ?? ऑपरेटर आमतौर पर सीमित उपयोग का होता है - अगर कुछ शून्य है तो फॉल-बैक वैल्यू प्रदान करना उपयोगी होता है, लेकिन जब आप संपत्तियों में ड्रिलिंग जारी रखना चाहते हैं या कभी-कभी तरीकों से नल संदर्भ अपवाद को रोकने के लिए उपयोगी नहीं है- शून्य संदर्भ।
IMHO, क्या अधिक से अधिक की आवश्यकता है ?? एक "शून्य-dereference" ऑपरेटर है जो आपको लंबी संपत्ति और/या विधि श्रृंखलाओं को एक साथ श्रृंखला की अनुमति देता है, उदा। a.b().c.d().e
नल-नेस के लिए प्रत्येक मध्यवर्ती चरण का परीक्षण किए बिना। Groovy भाषा में Safe Navigation operator है और यह बहुत आसान है।
सौभाग्य से, ऐसा लगता है कि सी # टीम इस फीचर अंतर से अवगत है। C# टीम के लिए यह connect.microsoft.com suggestion देखें, सी # भाषा में एक नल-डीरेंसेंस ऑपरेटर जोड़ने के लिए।
हमें इस तरह के सुविधाओं के लिए बहुत से अनुरोध मिलते हैं। "?" समुदाय में उल्लिखित संस्करण चर्चा हमारे दिल के निकट है - यह आपको पर प्रत्येक "डॉट" पर शून्य के लिए परीक्षण करने की अनुमति देता है, और मौजूदा के साथ अच्छी तरह से लिखता है ?? ऑपरेटर:
एक? .b? .c ?? डी
मतलब अगर कोई, ए.बी. या ए.बी.टी. नल उपयोग डी है।
हम भविष्य में रिलीज के लिए इस पर विचार कर रहे हैं, लेकिन यह सी # 4.0 में नहीं होगा।
एक बार फिर धन्यवाद,
मैड्स Torgersen, सी # भाषा अपराह्न
आप भी सी # में इस सुविधा चाहते हैं, कनेक्ट साइट पर suggestion करने के लिए अपने वोट जोड़ने! :-)
एक वैकल्पिक हल मैं इस सुविधा की कमी से बचने के लिए का उपयोग क्या this blog post में वर्णित है की तरह एक विस्तार विधि, कोड इस तरह की अनुमति देने के लिए है:
string s = h.MetaData
.NullSafe(meta => meta.GetExtendedName())
.NullSafe(s => s.Trim().ToLower())
इस कोड को या तो h.MetaData.GetExtendedName().Trim().ToLower()
देता है या यह रिटर्न शून्य अगर एच, एच। मेटाडेटा, या एच। मेटाडेटा। GetExtendedName() शून्य है। मैंने इसे शून्य या खाली तारों, या शून्य या खाली संग्रहों की जांच करने के लिए भी बढ़ाया है। यहां दिए गए कोड का उपयोग मैं इन एक्सटेंशन विधियों को परिभाषित करने के लिए करता हूं:
public static class NullSafeExtensions
{
/// <summary>
/// Tests for null objects without re-computing values or assigning temporary variables. Similar to
/// Groovy's "safe-dereference" operator .? which returns null if the object is null, and de-references
/// if the object is not null.
/// </summary>
/// <typeparam name="TResult">resulting type of the expression</typeparam>
/// <typeparam name="TCheck">type of object to check for null</typeparam>
/// <param name="check">value to check for null</param>
/// <param name="valueIfNotNull">delegate to compute if check is not null</param>
/// <returns>null if check is null, the delegate's results otherwise</returns>
public static TResult NullSafe<TCheck, TResult>(this TCheck check, Func<TCheck, TResult> valueIfNotNull)
where TResult : class
where TCheck : class
{
return check == null ? null : valueIfNotNull(check);
}
/// <summary>
/// Tests for null/empty strings without re-computing values or assigning temporary variables
/// </summary>
/// <typeparam name="TResult">resulting type of the expression</typeparam>
/// <param name="check">value to check for null</param>
/// <param name="valueIfNotNullOrEmpty">delegate to compute non-null value</param>
/// <returns>null if check is null, the delegate's results otherwise</returns>
public static TResult CheckNullOrEmpty<TResult>(this string check, Func<string, TResult> valueIfNotNullOrEmpty)
where TResult : class
{
return string.IsNullOrEmpty(check) ? null : valueIfNotNullOrEmpty(check);
}
/// <summary>
/// Tests for null/empty collections without re-computing values or assigning temporary variables
/// </summary>
/// <typeparam name="TResult">resulting type of the expression</typeparam>
/// <typeparam name="TCheck">type of collection or array to check</typeparam>
/// <param name="check">value to check for null</param>
/// <param name="valueIfNotNullOrEmpty">delegate to compute non-null value</param>
/// <returns>null if check is null, the delegate's results otherwise</returns>
public static TResult CheckNullOrEmpty<TCheck, TResult>(this TCheck check, Func<TCheck, TResult> valueIfNotNullOrEmpty)
where TCheck : ICollection
where TResult : class
{
return (check == null || check.Count == 0) ? null : valueIfNotNullOrEmpty(check);
}
}
FYI साथ इसे का उपयोग किया गया है: ?? नल कोलेसिंग ऑपरेटर कहा जाता है। –
हां, बिल्कुल मैं क्या सोच रहा हूं। – Ruben
मुझे लगता है कि यह एक "समुदाय विकी" होना चाहिए। –