Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C# explicit operators aren't that explicit :(

C# explicit operators aren't that explicit :(

Scheduled Pinned Locked Moved The Lounge
csharpcsshelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eric Lynch
    wrote on last edited by
    #1

    Usually, I'm a big fan of C#. Today it's irritating me a little bit :( I have a case where I would like to perform an implicit conversion for my class when a double is assigned to it, but not do one for an int. Yeah, I know, its an odd requirement! Suffice to say, it would make my life a little simpler, if I could get away with it. Initially, it seems like C# should allow me to do it. If I mark the operator for int as explicit, I get the error I want. Regrettably, when I then add the operator for double, which is implicit, the error goes away. I do understand why, and understand that C# "is-what-it-is", which is usually a good thing. However, today, I truly wish the explicit operator took precedence over the implicit operator! Oh well, there are plenty of work-arounds, but they end up being far less elegant in this particular code base. For your consideration, here's a code snippet that demonstrates the behavior. Comment out the line with the implicit operator and you get the error I want. Without it, the integer gets implicitly converted to a double and uses that operator, so no error.

    namespace ExplicitOperator
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    int value = 5;
    Test test = value;
    }

    public class Test
    {
      public Test(double value) => Value = value;
      public Test(int value) => Value = value;
      public double Value { get; }
      public static explicit operator Test(int value) => new Test(value);
      public static implicit operator Test(double value) => new Test(value);
    }
    

    }
    }

    B P 2 Replies Last reply
    0
    • E Eric Lynch

      Usually, I'm a big fan of C#. Today it's irritating me a little bit :( I have a case where I would like to perform an implicit conversion for my class when a double is assigned to it, but not do one for an int. Yeah, I know, its an odd requirement! Suffice to say, it would make my life a little simpler, if I could get away with it. Initially, it seems like C# should allow me to do it. If I mark the operator for int as explicit, I get the error I want. Regrettably, when I then add the operator for double, which is implicit, the error goes away. I do understand why, and understand that C# "is-what-it-is", which is usually a good thing. However, today, I truly wish the explicit operator took precedence over the implicit operator! Oh well, there are plenty of work-arounds, but they end up being far less elegant in this particular code base. For your consideration, here's a code snippet that demonstrates the behavior. Comment out the line with the implicit operator and you get the error I want. Without it, the integer gets implicitly converted to a double and uses that operator, so no error.

      namespace ExplicitOperator
      {
      public class Program
      {
      public static void Main(string[] args)
      {
      int value = 5;
      Test test = value;
      }

      public class Test
      {
        public Test(double value) => Value = value;
        public Test(int value) => Value = value;
        public double Value { get; }
        public static explicit operator Test(int value) => new Test(value);
        public static implicit operator Test(double value) => new Test(value);
      }
      

      }
      }

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      I think this would be an excellent post for the C# language forum !

      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

      1 Reply Last reply
      0
      • E Eric Lynch

        Usually, I'm a big fan of C#. Today it's irritating me a little bit :( I have a case where I would like to perform an implicit conversion for my class when a double is assigned to it, but not do one for an int. Yeah, I know, its an odd requirement! Suffice to say, it would make my life a little simpler, if I could get away with it. Initially, it seems like C# should allow me to do it. If I mark the operator for int as explicit, I get the error I want. Regrettably, when I then add the operator for double, which is implicit, the error goes away. I do understand why, and understand that C# "is-what-it-is", which is usually a good thing. However, today, I truly wish the explicit operator took precedence over the implicit operator! Oh well, there are plenty of work-arounds, but they end up being far less elegant in this particular code base. For your consideration, here's a code snippet that demonstrates the behavior. Comment out the line with the implicit operator and you get the error I want. Without it, the integer gets implicitly converted to a double and uses that operator, so no error.

        namespace ExplicitOperator
        {
        public class Program
        {
        public static void Main(string[] args)
        {
        int value = 5;
        Test test = value;
        }

        public class Test
        {
          public Test(double value) => Value = value;
          public Test(int value) => Value = value;
          public double Value { get; }
          public static explicit operator Test(int value) => new Test(value);
          public static implicit operator Test(double value) => new Test(value);
        }
        

        }
        }

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        How much grief does it cause if you make the double conversion explicit too? Just a random thought.

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        E 1 Reply Last reply
        0
        • P Peter_in_2780

          How much grief does it cause if you make the double conversion explicit too? Just a random thought.

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          E Offline
          E Offline
          Eric Lynch
          wrote on last edited by
          #4

          Unfortunately, it causes quite a bit of grief. The example I provided was the shortest code I could write to share my pain. Thank you for your thought, but not hoping for an actual solution here...simply ranting :) Regrettably, the actual problem is a lot more complex. It also involves collection initializers, method signature ambiguity, and the entire zoo of built-in numeric data types. I'm helping consumers of a framework avoid the detritus added by explicit casts / constructor invocations. I already have a solution...a separate method signature for each data type (in my code). Its not a huge deal, simply repellant to look at. It also snowballs a bit, to a few places, and forces me to carry test cases for each one. If C# had a different precedence for implicit/explicit, a far more elegant solution would be possible. That, and honestly, it annoys me that it favors an implicit convenience over something I've told it explicitly NOT to do.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups