SoFunction
Updated on 2025-03-07

Detailed explanation of the usage of the C# MBG extension method library

I saw an article on CodeProject before:MBG Extensions Library 
It is roughly the author introducing the extension method library he wrote, the content is as follows:
In()
Copy the 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 it like this:
Copy the codeThe code is as follows:

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

Cool!

In Example 2:
Copy the codeThe code is as follows:

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 it as:
Copy the codeThe code is as follows:

if ((myList))
{
   //Do something
}

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

If you can only use In on the string type, then you are wrong. The author also used In on Enum.
For example:
Copy the codeThe code is as follows:

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

Using In extension becomes:
Copy the codeThe code is as follows:

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

Although the code looks cool, I personally think it is not very intuitive and the meaning is not clearly expressed. I didn't see what the specific meaning was.

XmlSerialize() and XmlDeserialize()
Serialization:
Copy the codeThe code is as follows:

("C:\\");

Deserialization:
Copy the codeThe code is as follows:

string xml = ();
Employees employees = <Employees>();

Repeat()
The author's example is:
Copy the codeThe code is as follows:

string separatorLine = "------------------------------------------";
//Use Repeat to become
string separatorLine = '-'.Repeat(30);

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

IsMultipleOf()
Copy the codeThe code is as follows:

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

The same is true, it seems simple, but in fact it is not as pleasant as i % 10 ==0.

Finally, I would like to say: This extended class library may be useful, but using third-party plug-ins always faces certain risks. Whether it is worth measuring it well. I don’t know why the author named MBG, and I can’t help but think of MLGB.