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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:26:46+00:00 2026-05-14T19:26:46+00:00

I was under the impression that the C# compiler will implicitly type an array

  • 0

I was under the impression that the C# compiler will implicitly type an array based off a type that they can all be implicitly converted to.

The compiler generates
No best type found for implicitly-typed array

public interface ISomething {}

public interface ISomething2 {}

public interface ISomething3 {}

public class Foo : ISomething { }
public class Bar : ISomething, ISomething2 { }
public class Car : ISomething, ISomething3 { }

void Main()
{
    var obj1 = new Foo();
    var obj2 = new Bar();
    var obj3 = new Car();

    var objects= new [] { obj1, obj2, obj3 };
}

I know that the way to correct this is to declare the type like:

new ISomething [] { obj1, ...} 

But I’m after an under the covers type help here.

  • 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-14T19:26:46+00:00Added an answer on May 14, 2026 at 7:26 pm

    The C# compiler considers the set of types of all the specified elements. It does not consider common base types etc.

    You could cast one of the expressions:

    var objects= new [] { obj1, obj2, (ISomething) obj3 };
    

    … but personally I’d just use the explicit form:

    var objects= new ISomething[] { obj1, obj2, obj3 };
    

    Alternatively, if you explicitly declared any or all of obj1, obj2 and obj3 as type ISomething, that would work fine too without changing the array initialization expression.

    From the C# 3 spec, section 7.5.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.4.2.13) of the set of
    expressions in the array initializer.

    Section 7.4.2.13 looks like this:

    In some cases, a common type needs to
    be inferred for a set of expressions.
    In particular, the element types of
    implicitly typed arrays and the return
    types of anonymous functions with
    block bodies are found in this way.
    Intuitively, given a set of
    expressions E1…Em this inference
    should be equivalent to calling a
    method

    Tr M<X>(X x1 … X xm)
    

    with the Ei as arguments. More
    precisely, the inference starts out
    with an unfixed type variable X.
    Output type inferences are then made
    from each Ei with type X. Finally, X
    is fixed and the resulting type S is
    the resulting common type for the
    expressions.

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I like @Petar's approach, but another approach would be to… May 14, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer 1) I am not aware of any performance issues with… May 14, 2026 at 10:38 pm
  • Editorial Team
    Editorial Team added an answer I don't think you can do something like that. $(subPathFiles.Split(';'))… May 14, 2026 at 10:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.