I used to see an article on CodeProject: MBG Extensions Library
The author is generally introducing his own extension method class library. The content is as follows:
In ()
Copy codeThe Code is as follows: if (myString = "val1" |
MyString = "val2" |
MyString = "val3" |
MyString = "val4" |
MyString = "val5 ")
{
// Do something
}
Using the Extension Method In, you can write as follows:Copy codeThe Code is as follows: if (myString. In ("val1 ",
"Val2", "val3", "val4", "val5 "))
{
// Do something
}
Cool!
In Example 2:Copy codeThe Code is as follows: bool found = false;
Foreach (string s in myList)
{
If (myString = s)
{
Found = true;
Break;
}
}
If (found)
{
// Do something
}
The In extension can be written as follows:Copy codeThe Code is as follows: if (myString. In (myList ))
{
// Do something
}
Of course, I personally think myList. contain (myString) is better.
If you can only use In the string type, you will be wrong. The author also applies In to Enum.
For example:Copy codeThe Code is as follows: public enum MyEnum
{
MyValue1,
MyValue2,
MyValue3,
MyValue4,
MyValue5
}
Using the In extension becomes:Copy codeThe Code is as follows: MyEnum myEnum = MyEnum. MyValue1;
If (myEnum. In (MyEnum. MyValue2,
MyEnum. MyValue3, MyEnum. MyValue5 ))
{
// Do Something
}
Although the Code looks cool, I personally think it is not intuitive and the meaning is not clearly expressed. I can't see what it means.
XmlSerialize () and XmlDeserialize ()
Serialization:Copy codeThe Code is as follows: employees. XmlSerialize ("C: \ employees. xml ");
Deserialization:Copy codeThe Code is as follows: string xml = employees. XmlSerialize ();
Employees employees = xml. XmlDeserialize <Employees> ();
Repeat ()
The example given by the author is:Copy codeThe Code is as follows: string separatorLine = "------------------------------------------";
// Use Repeat to convert
String separatorLine = '-'. Repeat (30 );
I still think this example is not appropriate. After all, you can use new String ('-', 30 );
IsMultipleOf ()Copy codeThe Code is as follows: int I = 234;
If (I % 10 = 0 ){}
// Change
If (I. IsMultipleOf (10 )){}
It looks as simple as I % 10 = 0.
Finally, I would like to say: this extension class library may be useful, but there are always some risks when using third-party plug-ins. Is it worth a good measure? I don't know why the author is named MBG, I can't help but think of MLGB.