Search code examples
c#visual-studio-2010idefind-all-references

Is it possible to find every occurrence of a particular variable being assigned to?


I am aware using 'Find all references' I can find every 'mention' of a variable - but is it possible to refine this search by only showing code that assigns a value to the variable in question?

For example:

var test = 1;
var test2 = test + 3;
test = 123;

Find all references with 'test' as the criteria would show all 3 lines.

The search I am after would show me only lines 1 and 3.


Solution

  • How about test\s*=\s*.*;? Use the find feature in the IDE and make sure regular expression is checked.

    This will search for test followed by any amount of white space followed by = followed by any amount of white space followed by anything followed by ;

    It will fail on multiline stuff though; e.g.

    test = \
    123;
    

    and the rather flashy trigraph sequence:

    test = ??/
    123;
    

    although I'm not certain C# has either of these constructs.