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

  • SEARCH
  • Home
  • 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 566251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:57:27+00:00 2026-05-13T12:57:27+00:00

In C#, the following type-inference works: var s = abcd; But why can’t the

  • 0

In C#, the following type-inference works:

var s = "abcd";

But why can’t the type be inferred when the variable is a constant?

The following throws a compile-time exception:

const var s = "abcd"; // <= Compile time error: 
                      //    Implicitly-typed local variables cannot be constant
  • 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-13T12:57:27+00:00Added an answer on May 13, 2026 at 12:57 pm

    I’m actually hoping Lippert pops by and and takes a look at the question

    If there’s something you want brought to my attention, you can leave my name in the text — not a comment — and I’ll find it eventually. Or, better, you can “tweet” to @ericlippert. Note that this does not constitute a service level agreement; I do this in my spare time.

    why can’t the type be inferred when the variable is a constant?

    “constant” and “variable” are opposites. const var gives me the shudders to type. A constant is a value that never changes and has no storage location; a variable is a storage location whose contents change. They’re completely different, so don’t attempt to combine them. The var syntax was chosen to call out “this is a variable”, and we’re sticking with it.

    var can stand in for a specific type declaration, but combining it with const severely muddies the picture of what the compiler does with the value. Therefore const var is disallowed to prevent this confusion and you have to explicitly type your constants.

    I would be perfectly fine with inferred constants that do not use var:

    const Pi = 3.14159;
    

    seems fine to me. However, I know of no plans to add this to C#.

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

Sidebar

Related Questions

Can somebody explain step by step type inference in following F# program: let rec
I've been thinking about how type inference works in the following OCaml program: let
In the following type and constant declaration, the last value in the array will
How does ML perform the type inference in the following function definition: let add
What kind of type inference does Typed Racket do? I found the following snippet
The following works fine, as one would expect in a Curry style polymorphic type
As some of you may know, the type inference errors in Haskell can be
I'm testing the following code to populate a dictionary recursively. However the type inference
I have the following: type KEY = (IPv4, Integer) type TPSQ = TVar (PSQ.PSQ
I have the following type of object : [RateInfo] => stdClass Object ( [@rateChange]

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.