Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 40
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

IsNull and IsNullOrEmpty Extension-Methods become Lazy


Autor: Christian Köwing
Sprache: C#
Bewertung: 6,6
(1 Bewertung)
Anzahl der Aufrufe: 3436
  
Kick it on dotnet-kicks.de  

Beschreibung:

Ich finde das macht den Code einfach besser lesbar und das Programmieren komfortabler.

Beispiel:

object abc = null;
string xyz = string.Empty;

if (abc.IsNull())
xyz = "abc is null!";

if (!xyz.IsNullOrEmpty())
abc = new object();


Update 11.01.22: IsNullOrEmpty becomes lazy.

PS: I love extension methods!

Gruß,
Chriss


Abgelegt unter: Extension, IsNull, IsNullOrEmpty, IEnumerable, ICollection, Lazy.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace CKing.Extensions
{
  using System.Collections.Generic;
  using System.Linq;

  /// <summary>
  /// Contains basic extension-methods, like 'IsNull', 'IsNullOrEmpty', aso.
  /// </summary>
  public static class BasicExt
  {
    /// <summary>
    /// Determines whether the object is null.
    /// </summary>
    /// <param name="source">The object, which may be null.</param>
    /// <returns>
    ///     <c>true</c> if the object is null; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNull(this object source)
    {
      return source == null;
    }

    /// <summary>
    /// Determines whether the string is null or contains no chars.
    /// </summary>
    /// <param name="source">The string, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the string is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty(this string source)
    {
      return string.IsNullOrEmpty(source);
    }

    /// <summary>
    /// Determines whether the collection is null or contains no elements.
    /// </summary>
    /// <typeparam name="T">The IEnumerable type.</typeparam>
    /// <param name="source">The enumerable, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
    {
      if (source == null)
        return true;

      var sourceAsCollection = source as ICollection<T>;
      if (sourceAsCollection != null)
        return sourceAsCollection.Count < 1;

      return sourceAsCollection.Any();
    }

    /// <summary>
    /// Determines whether the collection is null or contains no elements.
    /// </summary>
    /// <typeparam name="T">The IEnumerable type.</typeparam>
    /// <param name="source">The collection, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this ICollection<T> source)
    {
      if (source == null)
        return true;

      return source.Count < 1;
    }
  }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Christopher Kk schrieb am:  17.11.2009 10:09:12

Finde ich gut, aber IsNullOrEmpty wird von string doch schon unterstützt? Zwar auf Klassenebene und nicht auf Objektebene, aber da sehe ich jetzt keine Vorteile gegenüber der string-Implementierung.
Christian Köwing schrieb am:  17.11.2009 11:28:36

Oh gut, wusste ich gar nicht. See refresh.


Diese Snippets könnten für Sie interessant sein:
[C#] Datei/Ordner durch Erweiterungsmethoden abfragen
[C#] Programmpfad mittels Erweiterung ermitteln
[C#] Invoke Beispiel mittels Erweiterungs Methoden.
[C#] DateTime Extensions liefern TimeStamp
[C#] Komplette Exception Message (inkl. InnerExceptions) ausgeben
[VB.NET] Programmpfad mittels Erweiterung ermitteln in VB
[C#] LINQ-Erweiterung für eindeutige Liste
[C#] Type Color serialisieren und deserialisieren
[C#] Controls gleichen Typs von einem Container in Winform holen
[C#] Extension in einem Dateinamen/Pfad ändern
[C#] Threadsicherer Codeaufruf im Control-Kontext (Invoking)
[C#] Selektieren eines gekapselten Eintrags aus der ComboBox
[C#] Color Extension-Methods
[C#] Enum-Extension - Flag in einem Bitfeld gesetzt?
[C#] Anzahl der Monate zwischen 2 DateTime-Objekten (Extension)
[C#] "for" mal anders
[C#] Generische Event Args -> EventArgs
[C#] Lazy Serialization-Extensions including Compression
[C#] SystemIconsImageListWrapper
[C#] Wie ToString() welche mit null-Strings umgehen kann
[C#] StringIsNullOrEmpty
[C#] simple generic pipeline
[C#] Duplikate aus einer IEnumerable entfernen
[C#] Enumerator as a parameter of a method
[C#] Stream, der mit foreach() durchlaufen werden kann
[C#] Eine allgemeine Liste (IEnumerable) in ein Array umwandeln
[C#] Zufälliger Eintrag aus einer Liste (Extension Method)
[C#] Implode und Trim für String-Enumerationen (Extension Method)
[C#] generische zip funktion

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.