Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace Dictionary<,>.Keys.Contains with ContainsKey #35343

@drewnoakes

Description

@drewnoakes

This is a proposal for a new analyzer/codefix for:

// given
Dictionary<int, string> dic = ...;

dic.Keys.Contains(1);
dic.Values.Contains("");

// Could instead be
dic.ContainsKey(1);
dic.ContainsValue("");

I found this code in some design-time build targets today.

EDIT I'd previously incorrectly stated that the former form was O(N), but System.Linq.Enumerable.Contains does type test for ICollection<> and delegate to ICollection<>.Contains, which is also O(1). There is some overhead associated with that however, which could be avoided.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Collectionscode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions