Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

One of my data types in SqlServer is smallint and this maps to short or Int16 in .NET. I was using a simple for loop with i being an int and it told me it couldn't implicitly convert between the two?

I was just wondering why this isn't possible? Is it just to prevent you from using a larger number than a short can hold? It seems strange to me because I'm around 80% sure I've never had to convert an int to a long ?

Sample:

public Int16 Test()
    int i = 2;
    return i;
                You won't need to convert an int to a long because a long is larger than an int in CLR types.
– Adam Houldsworth
                Mar 15, 2012 at 14:37

An implicit from short to int should exist because you are going from narrow to wide, wide to narrow is not supported implicitly because of potential data loss. The compiler allows you to ignore this, but requests that you cast explicitly in these cases to state that you've understood the potential and that you are OK with it.

Update:

Your code is a narrowing cast and cannot be done implicitly. Fix:

public Int16 Test()
    int i = 2;
    return (Int16)i;

Although, to be facetious, in this particular case the compiler could theoretically know that no data loss will occur. For the other 99.99% of the time it cannot know this at compile time, thus only explicit casting is supported where data loss might occur.

This clearly places the onus on the developer such that when a runtime error occurs due to data loss, the developer would have had to consciously make that decision prior and has no legal standing to hurl expletives at the computer.

VB.NET appears to allow you to circumvent even this restriction using Option Strict:

http://msdn.microsoft.com/en-us/library/k1e94s7e(v=vs.80).aspx

So I presume this is a C# specific design choice.

Just to piggyback what Adam has said here, going from a larger container to a smaller container guarantees a loss of data. Explicitly casting it tells the compiler that you are aware of that fact and are doing it on purpose. – DJ Quimby Mar 15, 2012 at 14:31 @danRhul I have answered that also, the fix is for clarity and other site visitors who may not know. And I know what alternative fixes there are. – Adam Houldsworth Mar 15, 2012 at 14:35

There is a predefined implicit conversion from short to int, long, float, double, or decimal.

http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=vs.71%29.aspx

It depends on what language you are using.

VB will convert without a problem by default as long as you dont overflow your Int16. If you enable Option Strict you will have to do the conversion yourself: Convert.ToInt16(yourInt32)

In c# you have to cast it: (Int16)yourInt32

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.