Why can't I define both implicit and explicit operators?

后端 未结 2 449
旧巷少年郎
旧巷少年郎 2020-12-29 03:19

Why I cannot define both implicit and explicit operators like so?

    public class C
    {
        public static implicit operator string(C c)
        {
             


        
2条回答
  •  梦谈多话
    2020-12-29 03:36

    I don't know the technical limitation that prevents this, but I think they would have done this to prevent people from shooting their own feet off.

    string imp = new C(); // = "implicit"
    string exp = (string)new C(); // = "explicit"
    

    That would drive me bonkers and makes no sense, C should only cast to a string 1 way, not 2 different ways.

提交回复
热议问题