Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all static variables in my c# project?

I want to run some part of my command line programm in parallel with multiple threads and I am afraid that there might be some static variable left that I must fix (e.g. by making it [ThreadStatic]). Is there any tool or easy way to find these in my project?

Of course simply searching for "static" does not help much: I have lots of static methods that work great and finde with any number of threads

like image 648
Christian Avatar asked Feb 10 '12 20:02

Christian


People also ask

Where are static variables stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

How do I find static instance variables?

We cannot access directly instance variables from a static method. Therefore, to access an instance variable, we must have an instance of the class from which we access the instance variable.

Are there static variables in C?

Syntax and Use of the Static Variable in COne can define a static variable both- outside or inside the function. These are local to the block, and their default value is always zero. The static variables stay alive till the program gets executed in the end.

Where are static and global variables stored in C?

A data segment is a portion of the virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.


2 Answers

Note: dasblinkenlight's answer works only on Visual Studio 2010 and older.

Below is the translation for Visual Studio 2012 and newer:

static(?([^\r\n])\s)+(\b(_\w+|[\w-[0-9_]]\w*)\b)(?([^\r\n])\s)+(\b(_\w+|[\w-[0-9_]]\w*)\b)(?([^\r\n])\s)*[=;]

Translation was made referencing: http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx

like image 128
Andy Kong Avatar answered Oct 18 '22 13:10

Andy Kong


Searching your files for static:b+:i:b+:i:b*[=;] with regexp option in Visual Studio should turn up static variables for you. It will also bring operators ==, but they should be relatively easy to filter out visually.

like image 27
Sergey Kalinichenko Avatar answered Oct 18 '22 14:10

Sergey Kalinichenko