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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:00:26+00:00 2026-06-14T17:00:26+00:00

Given the following C# code: var a = new[] {123, 321, 1}; //no best

  • 0

Given the following C# code:

var a = new[] {"123", "321", 1}; //no best type found for implicitly typed array

And its counterpart in VB.NET:

Dim a = {"123", "321", 1} 'no errors

It appears that VB.NET is able to correctly infer type of a = Object(), while C# complains until the above is fixed to:

var a = new object[] {"123", "321", 1};

Is there a way to auto-infer type in C# for the above scenario?

EDIT: Interesting observation after playing with different types in a C# sandbox – type is correctly inferred if all elements have common parent in the inheritance tree, and that parent is not an Object, or if elements can be cast into a wider type (without loss of precision, for example Integer -> Double). So both of these would work:

var a = new[] {1, 1.0}; //will infer double[]
var a = new[] {new A(), new B()}; //will infer A[], if B inherits from A

I think this behavior is inconsistent in C#, because all types inherit from Object, so it’s not a much different ancestor than any other type. This is probably a by-design, so no point to argue, but if you know the reason, would be interesting to know why.

  • 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-14T17:00:28+00:00Added an answer on June 14, 2026 at 5:00 pm

    No. Implicitly typed arrays in C# require that the type of one of the expressions in the array initializer is of the target type. Basically, the compiler tries to find exactly one element type such that all the other types can be converted to it.

    You could cast any of the elements to object of course:

    var a = new[] { (object) "123", "321", 1}; 
    

    … but then you might as well just use an explicitly typed array initializer:

    var a = new object[] {"123", "321", 1}; 
    

    Or in cases where you really are declaring a variable at the same time:

    object[] a = {"123", "321", 1};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following C# code: var product = new List<int>(); for (int n1 =
Given the following piece of code: using(var data = new SomeDataContext(ConnectionString)) { data.ObjectTrackingEnabled =
Given the following Code. [TestMethod] public void CanResolveILoggerTest() { var Container = new Castle.Windsor.WindsorContainer();
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code: var myList = new List<string> { red, blue, green };
Given the following code: var myList = new List<string> { red shirt, blue, green,
Given the following code: using (var client = new WebClient()) { string url =
Given the following code: var a = 'somegarbage=http://somesite.com/foo/bar/&moregarbage'; var result = a.match(/http.+\//g); Produces the
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
given i have the following block of code (function(){ var mb = { abc:function(){

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.