Usage of Class Libraries Based on C # MBG Extension Method

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.