Use of the extended method library based on C MBG

  • 2020-05-10 18:42:53
  • OfStack

I saw an article on CodeProject before: MBG Extensions Library
 
The author is introducing the extended method class library he has written. The content is as follows:
In()

if (myString == "val1" ||
   myString == "val2" ||
   myString == "val3" ||
   myString == "val4" ||
   myString == "val5")
   {
      //Do something
   }

Using the extension method In, you can write:

if (myString.In("val1", 
"val2", "val3", "val4", "val5"))
{
   //Do something
}

Cool!

Example 2 of In:

bool found = false;
foreach (string s in myList)
{
   if (myString == s)
   {
      found = true;
      break;
   }
}
if (found)
{
   //Do something
}

Using the In extension, you can write:

if (myString.In(myList))
{
   //Do something
}

Of course, I personally think myList.contain(myString) is better.

If you can only use In on the string type, you are wrong. The author also used In on Enum.
Such as:

public enum MyEnum
{
   MyValue1,
   MyValue2,
   MyValue3,
   MyValue4,
   MyValue5
}

Using the In extension becomes:

MyEnum myEnum = MyEnum.MyValue1;
if (myEnum.In(MyEnum.MyValue2, 
MyEnum.MyValue3, MyEnum.MyValue5))
{
   //Do Something
}

Although the code looks cool, I think it is not intuitive and the meaning is not clear. I don't see what that means.

XmlSerialize() and XmlDeserialize()
Serialization:

employees.XmlSerialize("C:\\employees.xml");

Deserialization:

string xml = employees.XmlSerialize();
Employees employees = xml.XmlDeserialize<Employees>();

Repeat()
The author gives the following examples:

string separatorLine = "------------------------------------------";
// use Repeat Can be changed into 
string separatorLine = '-'.Repeat(30);

I still think this example is not appropriate, after all, you can new String(' -', 30);

IsMultipleOf()

int i = 234;
if (i % 10 == 0){ }
// become 
if (i.IsMultipleOf(10)){}

It looks simple, but actually it is not as cool as i % 10 ==0.

This extension library may be useful, but there is always a risk of using a third plug-in. It is worth weighing whether it is worth it. I don't know why the author named it MBG, but I can't help thinking of MLGB.


Related articles: