Click here to Skip to main content
15,892,005 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Mini CCCs 34 - Done Pin
Tim Deveaux27-Feb-19 21:35
Tim Deveaux27-Feb-19 21:35 
GeneralRe: Mini CCCs 34 - Done Pin
OriginalGriff27-Feb-19 21:40
mveOriginalGriff27-Feb-19 21:40 
QuestionAnyone working with Cisco IoT? Pin
Chris Maunder27-Feb-19 11:27
cofounderChris Maunder27-Feb-19 11:27 
AnswerRe: Anyone working with Cisco IoT? PinPopular
RossMW27-Feb-19 13:03
professionalRossMW27-Feb-19 13:03 
AnswerRe: Anyone working with Cisco IoT? Pin
Marc Clifton28-Feb-19 1:51
mvaMarc Clifton28-Feb-19 1:51 
JokeRe: Anyone working with Cisco IoT? Pin
Chris Maunder28-Feb-19 6:31
cofounderChris Maunder28-Feb-19 6:31 
AnswerRe: Anyone working with Cisco IoT? Pin
maze328-Feb-19 3:07
professionalmaze328-Feb-19 3:07 
RantC# explicit operators aren't that explicit :( Pin
Eric Lynch27-Feb-19 10:56
Eric Lynch27-Feb-19 10:56 
Usually, I'm a big fan of C#. Today it's irritating me a little bit Frown | :(

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.

C#
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);
    }
  }
}

GeneralRe: C# explicit operators aren't that explicit :( Pin
BillWoodruff27-Feb-19 11:59
professionalBillWoodruff27-Feb-19 11:59 
GeneralRe: C# explicit operators aren't that explicit :( Pin
Peter_in_278027-Feb-19 19:43
professionalPeter_in_278027-Feb-19 19:43 
GeneralRe: C# explicit operators aren't that explicit :( Pin
Eric Lynch28-Feb-19 1:30
Eric Lynch28-Feb-19 1:30 
GeneralThe Stone Age Pin
David O'Neil27-Feb-19 8:56
professionalDavid O'Neil27-Feb-19 8:56 
GeneralRe: The Stone Age Pin
Mike Hankey27-Feb-19 9:44
mveMike Hankey27-Feb-19 9:44 
GeneralRe: The Stone Age Pin
David O'Neil27-Feb-19 10:39
professionalDavid O'Neil27-Feb-19 10:39 
GeneralRe: The Stone Age Pin
kalberts27-Feb-19 22:34
kalberts27-Feb-19 22:34 
GeneralRe: The Stone Age Pin
enhzflep28-Feb-19 7:31
enhzflep28-Feb-19 7:31 
AnswerRe: The Stone Age Pin
ZurdoDev28-Feb-19 1:45
professionalZurdoDev28-Feb-19 1:45 
GeneralRe: The Stone Age Pin
Mike Hankey28-Feb-19 1:51
mveMike Hankey28-Feb-19 1:51 
GeneralRe: The Stone Age Pin
Rage27-Feb-19 10:06
professionalRage27-Feb-19 10:06 
GeneralRe: The Stone Age Pin
David O'Neil27-Feb-19 11:27
professionalDavid O'Neil27-Feb-19 11:27 
GeneralRe: The Stone Age Pin
dandy7227-Feb-19 10:59
dandy7227-Feb-19 10:59 
GeneralRe: The Stone Age Pin
David O'Neil27-Feb-19 11:21
professionalDavid O'Neil27-Feb-19 11:21 
GeneralReally, Amazon? Pin
Kornfeld Eliyahu Peter27-Feb-19 8:47
professionalKornfeld Eliyahu Peter27-Feb-19 8:47 
GeneralRe: Really, Amazon? Pin
David O'Neil27-Feb-19 8:57
professionalDavid O'Neil27-Feb-19 8:57 
GeneralRe: Really, Amazon? Pin
Ron Anders27-Feb-19 9:35
Ron Anders27-Feb-19 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.