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 8505589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:21:51+00:00 2026-06-11T02:21:51+00:00

The MSDN page for byte says that you can declare a byte like this:

  • 0

The MSDN page for byte says that you can declare a byte like this:

byte myByte = 255;

and that

In the preceding declaration, the integer literal 255 is implicitly
converted from int to byte. If the integer literal exceeds the range
of byte, a compilation error will occur.

So I’m struggling to understand why the following gives me a compile error of ‘cannot implicitly convert type ‘int’ to ‘byte’)

byte value = on ? 1 : 0; // on is defined as a bool earlier

I’m compiling this on VS 2012 as a Windows Store App project, if that makes any difference.

  • 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-06-11T02:21:52+00:00Added an answer on June 11, 2026 at 2:21 am

    Because this:

    on ? 1 : 0
    

    Isn’t an integer literal. It an expression that returns an integer. Moreover, this expression cannot be evaluated until runtime.

    When there’s a literal, the compiler can evaluate it at compile time and ensure it satisfies any range requirements – as the page says, it’s up to the compiler to produce an error if the value is out of range.

    And from your same page:

    You cannot implicitly convert non-literal numeric types of larger storage size to byte.


    Per @Jeppe Stig Nielsen’s comment – it does also work if the value is a constant (it doesn’t have to be a literal as the first page says). C# spec says:

    6.1.9 Implicit constant expression conversions

    An implicit constant expression conversion permits the following conversions:

    • A
      constant-expression (§7.19) of type int can be converted to type
      sbyte, byte, short, ushort, uint, or ulong, provided the value of the
      constant-expression is within the range of the destination type.

    • A
      constant-expression of type long can be converted to type ulong,
      provided the value of the constant-expression is not negative.

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

Sidebar

Related Questions

I can compile this code that I got from an MSDN page : using
There's this ... MSDN page, IQueryable(Of T) Interface . Can you do a better
This MSDN page mentions that there're nothrow versions of new and delete . nothrow
I'm using something similar to example C on this MSDN page: http://msdn.microsoft.com/en-us/library/ms190307.aspx DECLARE @tableHTML
On this MSDN page it is stated that nested installations (also called concurrent installations)
The external MSDN documentation page for types that have NO generic arguments can be
The T-SQL MSDN page states The timestamp syntax is deprecated. This feature will be
So, as described on this msdn page , when you define a Connection String
I this MSDN Magazine article , the author states (emphasis mine): Note that boxing
Just reading through the MSDN page about new .NET 4.0 feature SpinLock and can

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.