Linked Questions
25 questions linked to/from What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
-5 votes
1 answer
1k views
How static variable can retain incremented value [duplicate]
I created a static variable in a class and incrementing in the constructor, thinking that whenever I created an instance to the class, the static variable will be reset to 0. To my surprise, the first ...
-7 votes
1 answer
1k views
Modifying static variables from other scripts in Unity [duplicate]
Does anybody know how to modify a static variable from a different script in Unity?
0 votes
1 answer
519 views
How do we access static properties in a class since the latter is not an object? [duplicate]
I am new to C# and I came across the following query. Since a class is not an object but just a blueprint(template), how is it possible to define static members on the class and access them by using ...
-4 votes
1 answer
548 views
I can't reach static class field from instance variable [duplicate]
I'm wondering I can't reach static class field from instance variable class aa { public static string b = "bb"; } Console.WriteLine(aa.b); //fine aa f = new aa(); f.b ...
3 votes
4 answers
45k views
Create a C# method to generate auto increment Id
I am new at programming and I am trying in vain to create a function that returns an auto increment id, however the code isn't working and I'm sure there's something missing even though I don't get ...
5 votes
5 answers
3k views
Is there reason why you can't declare a static variable within a C# method?
I've been working in C for the past couple of years and I've managed to get use to putting single-purpose, static variables near where they are used within my code. While writing a very basic method ...
6 votes
4 answers
2k views
In which case shouldn't I use static members in a class?
I have to ask this question because I feel that only experienced programmers can know the pros and cos of static members in a class. I've read books explaining about static members, I've also used ...
4 votes
1 answer
6k views
Static as a local variable inside function in C# [duplicate]
I came to know that, From MSDN: C# does not support static local variables (variables that are declared in method scope). And here: The static modifier can be used with classes, fields, methods,...
2 votes
4 answers
525 views
Private property changing it's value, without it's setter being called (also cross-session). Assuming .NET error
Update for future readers: If you find this text too long: This is a race condition due to a static variable. What you are reading below is showing how confusing this can become for someone who is ...
1 vote
1 answer
3k views
Why AsyncLocal is different from CallContext
Running the bellow code you can see that there is a different between CallContext and AsyncLocal. using System; using System.Runtime.Remoting.Messaging; using System.Threading; namespace ...
1 vote
3 answers
2k views
Static vs public property
i was writing code in MVC and i don't' understand behaviours public properties in my project!! my code its very simple, increment property. When i clik on button the property is overwrites. Its work ...
0 votes
3 answers
2k views
Using same Boolean with multiple objects in Unity 2D
I have four game objects with the same script and the same bool, when one of them get hit by ray the bool state is true, but the function will not start because the other three game objects is set to ...
0 votes
5 answers
684 views
System.StackOverflowException while filling the list
Im gonna paste the code first so you can see what Im talking about: namespace Radni_sati { public class Blagdani { public List<Blagdani> Blagdani_lista = new List<Blagdani>(); ...
1 vote
1 answer
708 views
Does the use of Static Variables in C# hinder secure coding practices?
I am new to C# programming and I am currently using many static variables in my code. Below is one example: class Program { public int I//Can be used to access the variable i anywhere from the ...
0 votes
4 answers
257 views
need clarification on C# static variables
I've tried searching this but I cant find a clear answer to my question. When can you actually alter a static variable? From my understanding you can only change it within the static constructor. ...