در این بخش از سری پستهای سی شارپ به معرفی چند متد فوق العاده مفید برای برنامهنویسان و توسعهدهندگان نرم افزار با زبان سیشارپ خواهیم پرداخت.
شرط بررسی null بودن متغیر:
// Return true if obj variable is null.
public static bool IsNull(this object obj)
{
return obj == null;
}
// How to use.
public void ProcessData(DataSet input)
{
if (!input.IsNull())
{
// business logic here } }
قالب بندی رشتهها:
// Formatting strings
public static string FormatString(this string format, params object[] args)
{
return string.Format(format, args);
}
// How to use.
string message = "Welcome {۰} (Last login: {۱})".FormatString(userName, lastLogin);
شرط برسی با استفاده از عبارت باقاعده:
// This extension method is for pattern matching in any string using Regex
public static bool Match(this string value, string pattern)
{
Regex regex = new Regex(pattern); return regex.IsMatch(value);
}
// How to use.
if (myData.Match("[۰-۹]"))
{
// do someting }
منبع : omidnasri.com