.Net高级技术:Linq

1.系统类的扩展方法

//帮助类标记为static
public static class StringHelper
{
//扩展的方法也要标记为静态的
public static bool IsEmail(this string str)//this要紧跟扩展的类型
{
bool result = true;
if (!str.Contains("@"))
{
result = false;
}
return result;
}

public static string BoolToString(this bool b)
{
string str = string.Empty;
if (b)
{
str="真";
}
else
{
str = "假";
}
return str;
}

public static string BbQuote(this string str, string pre, string tag)
{
return pre + str + tag;
}

}

调用扩展的方法:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string email = "[email protected]";
bool b = email.IsEmail();
string msg= b.BoolToString();
MessageBox.Show(msg);
MessageBox.Show(email.BbQuote("_","|"));
}
}

猜你喜欢

转载自www.cnblogs.com/francis-ray/p/10161155.html