Поделиться через


Filtered Completion Lists in C#

IntelliSense removes unnecessary members from the completion list by using filters.

Visual C# filters the completion lists that appear for these items:

  • Interfaces and base classes.

  • Attributes.

  • as and is operators.

  • Catch clauses.

  • Object Initializers

  • Events

Interfaces and Base Classes

IntelliSense automatically removes items from the interface and base class completion lists, in both class declaration base and interface lists and constraint lists. For example, enums do not appear in the completion list for base classes, because enums cannot be used for base classes. The completion list of base classes only contains interfaces and namespaces. If you select an item in the list and then type a comma, IntelliSense removes base classes from the completion list because Visual C# does not support multiple inheritance. The same behavior occurs for constraint clauses also.

Attributes

When you apply an attribute to a type, the completion list is filtered so that the list only contains those types that descend from the namespaces that contain those types, such as Attribute.

as and is Operators

A filtered completion list is displayed automatically when you press the SPACEBAR after you have typed the as or is keyword.

Catch Clauses

For catch, the IntelliSense completion list only displays the relevant types. These types include those derived from Exception, types that include nested types, and types that are constrained on types that derive from Exception.

Object Initializers

Only members that can be initialized will appear in the completion list. For example:

class Cust
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class MyApp
{
    static void Main()
    {
        var customer = new Cust()
        {
            //Name and Age appear in completion list
            Name = "Sally",
            //Only Age appears in completion list
            Age = 30
        };
    }
}

The previous example first creates a class Cust with two auto-implemented properties. When we initialize the Name property, only Name and Age appear in the completion list. The completion list filters out entries that are not relevant to initializing the object. For information about object initializers, see Object and Collection Initializers (C# Programming Guide)

Events

When you type the keyword event (C# Reference), the completion list only contains delegate types.

See Also

Concepts

Completion Lists in C#

Pre-selected Completion List Items in C#

Other Resources

Visual C# IntelliSense