Linked Questions

26 votes
1 answer
9k views

const vs. static readonly [duplicate]

Possible Duplicate: What is the difference between const and readonly? So from what I read, in C#, const and static readonly will both make a value unalterable during the execution of a program. ...
s5s's user avatar
  • 11.9k
21 votes
7 answers
6k views

what is the difference between const, readonly and get in a static class [duplicate]

I have a question. Lately I have caught myself using 3 different lines of code which upon closer inspection looks and feels the same. public static class constant { public static readonly int ...
Thomas Andreè Wang's user avatar
2 votes
8 answers
1k views

constant and readonly in c#? [duplicate]

Possible Duplicate: What is the difference between const and readonly? i have doubt in c# what is difference in constant and readonly explain with simple problem or any reference.
Surya sasidhar's user avatar
3 votes
1 answer
8k views

How can I create a constant hashset in c# [duplicate]

Right now I have a const array of strings and loop through to check if a value exists. But I want a more efficient way to store my values. I know there's a hashset which I can use like so: HashSet<...
DannyD's user avatar
  • 2,831
4 votes
3 answers
2k views

difference between ReadOnly and Const? [duplicate]

Possible Duplicate: What is the difference between const and readonly? are these interchangeable? can you show me code on how you would apply the two?
Alex Gordon's user avatar
  • 59.3k
5 votes
1 answer
503 views

How can I get the equivalent of C++'s "const" in C#? [duplicate]

In C++, if I want an object to be initialized at compile time and never change thereafter, I simply add the prefix const. In C#, I write // file extensions of interest private const List&...
user6048670's user avatar
  • 2,891
-2 votes
2 answers
1k views

Constant class variable in c# [duplicate]

I want to declare a variable in my class, that cannot be changed later like this: obj myobj=new obj() myobj.CONSTANT_VAR="Changed value" //ERROR!! ...but whose value can be accessed like: ...
Psychopathic Azula's user avatar
-3 votes
1 answer
378 views

Why does C# have 'readonly' and 'const'? [duplicate]

I come from a C++ background and am trying to become proficient in C#. It seems like C# always has 2 types of modifiers wherever C++ had one. For example, in C++ there is & for references and then ...
Subpar Web Dev's user avatar
1 vote
1 answer
180 views

How to use readonly modifier to implement right triangle portion of class in second examples? [duplicate]

Below are the two different examples from the book Illustrated C# 2012. In the first example Hypotenuse is declared as readonly. In second example PI and numberofsides are declared as readonly. Why is ...
Rayhose's user avatar
0 votes
1 answer
274 views

Unable to apply readonly keyword on method scope but const is working fine [duplicate]

Why we can not apply readonly key word on method scope.but const is working fine. public void show(){ const string test = "test"; } public void show(){ readonly string test="test"; } any ...
atul bisht's user avatar
0 votes
0 answers
125 views

get vs Readonly vs constant in c# [duplicate]

What is the difference between {get;} and Readonly in the following C# code? String name { get { return "Tom"; } } const String name = "Tom"; readonly String name = "Tom"; please explain with simple ...
v11's user avatar
  • 2,174
0 votes
0 answers
69 views

readonly vs const. How do I prove the theoretical explanation? [duplicate]

Theoretically I thought I knew the difference between the two. The most crucial difference I thought was that const are baked in where as readonly are decided at the runtime and could be assigned ...
haku's user avatar
  • 4,345
1603 votes
22 answers
489k views

'Static readonly' vs. 'const'

I've read around about const and static readonly fields. We have some classes which contain only constant values. They are used for various things around in our system. So I am wondering if my ...
Svish's user avatar
  • 156k
64 votes
2 answers
5k views

How to stop C# from replacing const variable with their values?

We have a project that's compiled into a DLL called consts.dll that contains something like: public static class Consts { public const string a = "a"; public const string b = "b"; public ...
Shmoopy's user avatar
  • 5,454
26 votes
2 answers
45k views

Lock() in a static method

I have a multi threaded application that writes to a settings xml file using a static method. I want to avoid that the file is being updated twice at the same time (causing accesss/write exception). ...
koen's user avatar
  • 1,057

15 30 50 per page