Home

Incomplete Intellisense in object intialization

edited June 27

I searched and saw a number of reports of Intellisense having issues when used in object initialization. Mostly, those reports led to fixes. But I haven't seen an issue similar to what I'm seeing. Specifically, I'm not getting any Intellisense in an object initialization block other than the properties which can be set within that block.

In this image, it shows what I'm referring to. i'm trying to call a method to set the value within the object initialization block. Near the bottom you can see there's at least one method which match "get" (and there are plenty other methods in the code which start "Get"). I can't even use statement completion. Ctrl-Space just displays a list of available properties.

Comments

  • edited July 1

    Another notable type of property that is excluded are collections. It ought to display those collections and provide support for Collection Initialization. Perhaps add hints on what other Add() overloads that may exist for that collection type.

    class Foo
    {
        public List<int> Bars { get; } = new();
        public Dictionary<int, string> Bazs { get; } = new();
    }
    
    var foo = new Foo
    {
        Bars = // readonly List<int> property, within object initializer, can use collection initializer here
        {
            1,
            2,
            3,
            4,
        },
        Bazs = // readonly Dictionary<int, string> property, within object initializer, can use collection initializer here
        {
            { 1, "ONE" },
            { 2, "TWO" },
            [3] = "THREE",
            [4] = "FOUR",
            5.100M, // CTRL+SPACE on the value would show the Add() method info
        },
    };
    
    static class Extensions
    {
        public static void Add(this IDictionary<int, string> dict, decimal val)
        {
            var key = (int)Math.Floor(val);
            dict.Add(key, val.ToString());
        }
    }
    
  • Thanks - the issue with object initializers should be fixed in 8.5.1.

  • Good to know. Thank Joe.

Sign In or Register to comment.