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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:22:23+00:00 2026-05-28T13:22:23+00:00

There is no difference between these two lines, because the compiler, in the second

  • 0

There is no difference between these two lines, because the compiler, in the second line, understands that it is an array of type int.

var x = new int[] { 1, 2, 3 };   //Fine, x is int[]
var x = new [] { 1, 2, 3 };      //Fine, x is int[]

But why can’t I do this with different types? Why doesn’t the compiler convert my variable to type object?

var x = new object[] { 1, "df", 5 };   //Fine, x is object[]
var x = new [] { 1, "df", 5 };         //Error! "No best type found for implicity-typed-array"

EDIT:

Thanks for all your answers. But I still wonder, what are the pros and cons to make all expressions that the compiler can’t convert to type object? (Because I use var notation which means that it can’t be any type. I understand like this.) Why doesn’t the compiler find the nearest type of the array members by going up the inheritance tree?

  • 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-28T13:22:24+00:00Added an answer on May 28, 2026 at 1:22 pm

    The new [] notation is for saving you to type an explicit type of the array members (or allowing you to create arrays where its elements have an anonymous type), but its type inference is limited in that all elements must share the same type or be implicitly convertible to a common type shared by at least one member. See C# Specification, section 7.6.10.4:

    An array creation expression of the third form is referred to as an implicitly typed array creation expression. It is similar to the second form, except that the element type of the array is not explicitly given, but determined as the best common type (§7.5.2.14) of the set of expressions in the array initializer.

    The following are examples of implicitly typed array creation expressions:

    var a = new[] { 1, 10, 100, 1000 };                       // int[]
    var b = new[] { 1, 1.5, 2, 2.5 };                         // double[]
    var c = new[,] { { "hello", null }, { "world", "!" } };   // string[,]
    var d = new[] { 1, "one", 2, "two" };                     // Error
    

    The last expression causes a compile-time error because neither int nor string is implicitly convertible to the other, and so there is no best common type. An explicitly typed array creation expression must be used in this case, for example specifying the type to be object[]. Alternatively, one of the elements can be cast to a common base type, which would then become the inferred element type.

    Key point here is that the “best common type” can only be one of the types already present. As Damien_The_Unbeliever pointed out in a comment: “As Mr. Lippert is fond of pointing out around inference, whenever it’s looking for a best common type, it will only return one of the types that is already present – it doesn’t go hunting for the most-derived common ancestor.”.

    Just because every array could be an object [] doesn’t mean it should. From a compiler perspective that’d be a trivial last-resort choice, but a very counter-intuitive one for the developer, I guess.

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

Sidebar

Related Questions

Is there a difference between these two lines? var url = http://www.google.com/; window.location =
Is there any actual difference between these two lines of code? ini_set('max_execution_time', 20*60); set_time_limit(20*60);
In particular, is there any difference between these two lines in terms of their
In C#, what is the difference (if any) between these two lines of code?
Is there any difference between these two LINQ statements: var query = from a
1)Is there any difference between these two keywords for the elements of collections??( Copy
When accessing values from an SqlDataReader is there a performance difference between these two:
Is there a difference (performance, overhead) between these two ways of merging data sets?
Is there any substantial difference between those two terms?. I understand that JDK stands
What is the difference between these two pieces of code type IInterface1 = interface

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.