45

It is easy to find all code that uses a property, however how do I find the code that just sets it?

(When I do a “find all reference” on the “set”, it just does a “find all reference” on the property itself, including code that just reads it.)

6

6 Answers 6

58

For what it's worth, this will be natively possible with VS2019.

Specifically the 'Find All References' window has a new 'Kind' column which can be filtered for 'Write' references:

enter image description here

The specific Github PR that added this feature is scheduled to be included in Visual Studio 2019 Preview 2 (16.0.P2) https://github.com/dotnet/roslyn/issues/22545

The full release of VS2019 is roadmapped for Q1 of 2019.

2
  • 3
    this should be the correct answer when VS 2019 gets officially released :) Commented Feb 7, 2019 at 21:01
  • Make sure "List View" button is unchecked. (not on screenshot, but should be next to Keep Results)
    – wondra
    Commented Nov 20, 2020 at 12:12
57

You can use Resharper.

Alternately, set the setter to private (Or comment out the setter completely) and recompile. You will get errors where you're trying to set the property.

8
  • 3
    I too like the compiler trick when ReSharper is not available. A step further would be to comment-out the setter entirely, so that even uses within the class will be surfaced by the compiler. Commented May 23, 2011 at 10:59
  • 1
    stackoverflow.com/questions/5981149/…
    – Jake Smith
    Commented Apr 16, 2016 at 17:48
  • 6
    Can you be please more specific related to the Resharper solution? Commented Jun 23, 2016 at 13:14
  • 10
    jetbrains.com/resharper/features/navigation_search.html After you bring up the Find Usages window in ReSharper, you can filter usages by "write usages" and "read usages". The screenshot under "Find Usages" section shows where they are. Commented Jul 19, 2016 at 3:54
  • Yay for compilers! Commented Feb 3, 2017 at 16:19
20

Try commenting the set part of property and build it gives error at all the places where it is used.

1

You could run a text search on propertyName = - you can try using regex search to allow for 0 to n spaces between the name and =.

1
  • 5
    Not much help if you have a load of classes each of which has a property called PropertyName.
    – Dave
    Commented Feb 20, 2018 at 14:47
0

AFAIK, this can't be done using the standard features of Visual Studio - it doesn't do anything special for properties to check whether they are being used on the left or right side when searching, and, to be sure, there's no option to tell it to do so.

To give an option without having to run extra regexes or install other software, you could just browse through the results window to let your eyes scan for left-side occurrences - maybe not the most productive but I'm not sure I see a great advantage over other suggestions.

Lastly, @Kamyar's suggestion to make the properties no longer accessible does seem worth a look, but this depends on how long it takes your project to compile, it could take even longer to find'em all - I'm not sure why you'd need Resharper to do this though.

0

Here's a fairly robust solution that'll work also for non-Properties using Visual Studio without 3rd party tools. Be sure to select the "Match Case" and "Use Regular Expressions" options in Find.

1. For all except Post-/Pre-fix Increment and Shift Assignments:

  (^|[^\w.])MyVariable\s*([\+\-\*/%&|\^]|)=[\w\s]

2. For Post-/Pre-fix Increment and Shift Assignments:

  ((^|[^\w.])MyVariable\s*(\+\+|--)|(\+\+|--)\s*MyVariable[^\w.]|(^|[^\w.])MyVariable\s*(<<|>>)=)

3. For Out / Ref Parameters (N/A for Properties):

  (^|[^\w.])(out|ref)\s+MyVariable[^\w.]

CAVEATS:

  1. C#.NET only.
  2. Visual Studio 2012+ only.
  3. Does not work if "=" is followed by an EOL.
  4. Does not work if "MyVariable" is followed by an EOL.
  5. Depending on starting point and scope of the Find and scope of the Variable / Property, may find more / less references than necessary. When in doubt, error on side of "more", so you won't miss anything.
  6. Does not work for "."-prefixed Variables / Properties. 6.1. Unless you include it as part of the "MyVariable" (i.e. "MyStructVariable.MyStructField" or "MyObjectVariable.MyObjectField") but you risk finding too few references since there may be other Struct or Object Variables used to make Assignments to the same Struct or Object Field or Property.
1
  • 1
    And the most important caveat: finds all "MyVariable"s; it obviously cannot distinguish between two variables with same name from different classes. One work-around is to temporarily rename the property you wish to search for, then do the search. Commented Oct 3, 2018 at 21:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.