Skip to content

Instantly share code, notes, and snippets.

@robinli
Last active December 19, 2015 00:19
Show Gist options
  • Save robinli/9459a899089e64368198 to your computer and use it in GitHub Desktop.
Save robinli/9459a899089e64368198 to your computer and use it in GitHub Desktop.
Custom My String Type : 取值時將字尾的空白移除
public class MyString
{
private string _Value;
//將建構子設為私有,代表無法用new關鍵字
private MyString(string value)
{
_Value = value.Trim();
}
//隱含轉換 接受string型別
public static implicit operator MyString(string expandedName)
{
return new MyString(expandedName);
}
//隱含轉換 回傳string型別
public static implicit operator string(MyString me)
{
return me._Value;
}
public override string ToString()
{
return _Value;
}
}
// Test code:
MyString str1 = "123 "; //字尾有空白
tring x1 = str1; //字尾無空白
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment