Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:29:58+00:00 2026-05-13T13:29:58+00:00

In the following: public class p { short? mID; short? dID; } short id

  • 0

In the following:

public class p
{
  short? mID;
  short? dID;
}

short id = p.mID ?? -p.dID.Value;

The compiler gives me the error:

Error 21 Cannot implicitly convert type ‘int’ to ‘short’. An explicit conversion exists (are you missing a cast?)

I have to change the code to the following for it to work:

short id = p.mID ?? (short)-p.dID.Value;

It’s as if the compiler is doing something like (int)0 – p.dID.Value, or that Int16.operator – is returning Int32s…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:29:58+00:00Added an answer on May 13, 2026 at 1:29 pm

    I refer you to section 7.6.2 of the specification, which states:


    For an operation of the form –x, unary operator overload resolution is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined negation operators are:

    Integer negation:

    int operator –(int x);
    long operator –(long x);
    

    The result is computed by subtracting x from zero. If the value of of x is the smallest representable value of the operand type (−2^31 for int or −2^63 for long), then the mathematical negation of x is not representable within the operand type. If this occurs within a checked context, a System.OverflowException is thrown; if it occurs within an unchecked context, the result is the value of the operand and the overflow is not reported.
    If the operand of the negation operator is of type uint, it is converted to type long, and the type of the result is long. An exception is the rule that permits the int value −2147483648 (−2^31) to be written as a decimal integer literal .

    If the operand of the negation operator is of type ulong, a compile-time error occurs. An exception is the rule that permits the long value −9223372036854775808 (−2^63) to be written as a decimal integer literal .

    Floating-point negation:

    float operator –(float x);
    double operator –(double x);
    

    The result is the value of x with its sign inverted. If x is NaN, the result is also NaN.

    Decimal negation:

    decimal operator –(decimal x);
    

    The result is computed by subtracting x from zero. Decimal negation is equivalent to using the unary minus operator of type System.Decimal.


    As you can see, there is no unary minus operator defined on shorts; overload resolution picks the one on ints because that one is the best match of all the available unary minus operators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following short code snippet. namespace B { public class Foo { public
I have the following class: public static class MyClass { private static readonly Dictionary<Type,
I got the following code for my object (short version) : public class PluginClass
Lets say I have the following class: namespace myNamespace { [TypeLibType((short)2)] [ClassInterface((short)0)] [ComImport] public
Following is my Model for Product public class Product { public int Id {
I have the following: public class Address { public string Email { get; set;
Suppose I have the following: public class MyObject { public string Name {get; set;}
I have a class that looks like the following: public class MyClass { private
We have a class a class that looks something like the following: public class
Suppose I write a library with the following: public class Bar { /* ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.