C# 结构体初始化

public struct MyStruct {
  public string s;
  public int length;

  public static implicit operator MyStruct(string value) {
    return new MyStruct() { s = value, length = value.Length };
  }

}

Example:

MyStruct myStruct = "Lol";
Console.WriteLine(myStruct.s);
Console.WriteLine(myStruct.length);

Output:

Lol
3

 

posted on 2013-04-30 18:58  武胜-阿伟  阅读(3410)  评论(0编辑  收藏  举报