-
-
Save robinli/9459a899089e64368198 to your computer and use it in GitHub Desktop.
Custom My String Type : 取值時將字尾的空白移除
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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