Skip to Content
C# 3.0: kurz & gut
book

C# 3.0: kurz & gut

by Joseph Albahari, Ben Albahari
May 2008
Intermediate to advanced content levelIntermediate to advanced
248 pages
3h 50m
German
O'Reilly Verlag
Content preview from C# 3.0: kurz & gut

Extension-Methoden (C# 3.0)

Extension-Methoden ermöglichen es, bestehende Typen durch neue Methoden zu erweitern, ohne die Definition des Ursprungstyps verändern zu müssen. Eine Extension-Methode ist eine statische Methode einer statischen Klasse, bei der der Modifikator this dem ersten Parameter mitgegeben wird. Der Typ des ersten Parameters ist dann der zu erweiternde Typ:

public static class StringHelper
{
  public static bool IsCapitalized (this string s)
  {
    if (string.IsNullOrEmpty (s)) return false;
    return char.IsUpper (s[0]);
  }
}

Die Extension-Methode IsCapitalized kann so aufgerufen werden, als würde es sich um eine Instanz-Methode eines Strings handeln:

Console.Write ("Perth".IsCapitalized(  ));

Der Aufruf einer Extension-Methode wird beim Kompilieren ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

How I Built a Personal Board of Directors With GenAI

How I Built a Personal Board of Directors With GenAI

Vipin Gupta
How to Become a Game-Changing Leader

How to Become a Game-Changing Leader

Douglas A. Ready, Alan Mulally
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer

Publisher Resources

ISBN: 9783897215443