Extension methods are a compiler trick which allows static methods to be called using instance methods syntax. They are so convenient that LINQ is essentially a collection of extension methods. Extension methods are a good way to shrink your code and follow the DRY principle. For example, instead of the following long code
bool
IsValidOs(
string
os)
{
return
os.Equals(
"Windows"
, StringComparison.OrdinalIgnoreCase) ||
os.Equals(
"Linux"
, StringComparison.OrdinalIgnoreCase) ||
os.Equals(
"Mac OS"
, StringComparison.OrdinalIgnoreCase);
}
The following version is much more readable:
bool
IsValidOs(
string
os)
{
return
os.IsLikeAny(
"Windows"
,
"Linux"
,
"Mac OS"
);
}
To add the library to your project, open Package Manager Console, select your project and type
Install-Package ExtensionMethods
Import the namespace
using ExtensionMethods;
ToFormat()
Similar to string.Format
"My name is {0} {1}".ToFormat("Nadeem", "Afana"); // My name is Nadeem Afana
You can even use it on nullable types:
int? value = 12700;
value.ToFormat("#,#"); // 12,700
value = null;
value.ToFormat("#,#"); // null
ToFormatNamed()
Similar to string.Format but uses named formats for better readability.
string firstName = "Nadeem";
string lastName = "Afana";
string language = "C#";
"My name is {first} {last}, and I like {lang}.".ToFormatNamed(firstName, lastName, language);
// My name is Nadeem Afana, and I like C#.
It also supports composite formatting:
int kbps = 30000;
@"The day is {dayName:dddd} and the time is {hour:hh:mm}.
The Internet speed is {speed:#.0} mbps.
".ToFormatNamed(DateTime.Now, DateTime.Now, kbps/1000.0);
// The day is Sunday and the time is 05:31. The Internet speed is 30.0 mbps.
The variable names do not have to match the arguments,but the order of arguments is important.
Reverse()
Reverses a string.
"The quick brown fox jumps over the lazy dog".Reverse();
// god yzal eht revo spmuj xof nworb kciuq ehT
Left()
Reads the first n characters from the left of a string.
"The quick brown fox jumps over the lazy dog".Left(9);
// The quick
Right()
Reads the first n characters from the right of a string.
"The quick brown fox jumps over the lazy dog".Right(3);
// dog
IsLike()
Determines whether two strings have the same value ignoring Case.
"STRING".IsLike("String"); // True
IsLikeAny()
Determines whether a string matches any value in a collection of strings ignoring case.
"c#".IsLikeAny("C++", "C#", "VB"); // True
IsLikeAll()
Determines whether a string matches all the values in a collection of strings ignoring case.
"internet".IsLikeAll("Internet", "INTERNET", "iNTerNet"); // True
StartsLike()
Determines whether the beginning of a string matches the specified string ignoring case.
string s = "The quick brown fox jumps over the lazy dog";
s.StartsLike("the"); // True
EndsLike()
Determines whether the end of a string matches the specified string ignoring case.
string s = "The quick brown fox jumps over the lazy dog";
s.EndsLike("DOG"); // True
TrimStart()
Removes a string from the beginning of another string.
string s = "The quick brown fox jumps over the lazy dog";
s.TrimStart("The"); // quick brown fox jumps over the lazy dog
s.TrimStart("THE", false); // case insensitive
// quick brown fox jumps over the lazy dog
TrimEnd()
Removes a string from the end of another string.
string s = "The quick brown fox jumps over the lazy dog";
s.TrimEnd("dog");
// The quick brown fox jumps over the lazy
s.TrimEnd("DOG", false); // case insensitive
// The quick brown fox jumps over the lazy
Capitalize()
Capitalizes the first letter of each word. Synonym for ToTitleCase()
string s = "the internet";
s.Capitalize(); // The Internet
ToTitleCase()
Capitalizes the first letter of each word. Synonym for Capitalize()
string s = "the internet";
s.ToTitleCase(); // The Internet
IsNullOrWhiteSpace()
Indicates whether a specified string is null, empty, or consists only of white-space characters.
string s = " ";
s.IsNullOrWhiteSpace(); // True
s = null;
s.IsNullOrWhiteSpace(); // True
s = string.Empty;
s.IsNullOrWhiteSpace(); // True
IsNumeric()
Determines if a string can be parsed into a number.
"1253".IsNumeric(); // True
"Abc".IsNumeric(); // False
ToBase64()
Converts a string into Base64 encoding.
string s = "Hello World!";
s.ToBase64(); // SGVsbG8gV29ybGQh (Utf8)
s.ToBase64(Encoding.UTF32); // Utf32
// SAAAAGUAAABsAAAAbAAAAG8AAAAgAAAAVwAAAG8AAAByAAAAbAAAAGQAAAAhAAAA
0 comments:
Post a Comment