This question already has answers here:
What is the difference between const and readonly in C#?
(30 answers)
Closed 6 years ago.
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 in C# there is ref and out and I have to learn the subtle differences between them. Same with readonly and const, which are the topic of this thread. Can someone explain to me what the subtle differences are between the 2? Maybe show me a situation where I accidentally use the wrong one and my code breaks.
Readonly: Can only be set in the constructor.
Const: Is a COMPILE TIME CONSTANT. I.e. can not be determined at runtime.
Related
This question already has answers here:
How can I write on another process memory?
(4 answers)
How to remote invoke another process method from C# application
(2 answers)
Closed 3 years ago.
I've been starting to do some decompiling of my C# programs and got some interesting results by editing the dlls, but is it possible to change values and call functions in a running process given that I know what the names of the variables or functions are?
Doesn't any cheat to any game do exactly that?
I mean, if I understand you correctly, there is software called
Cheat Engine which allows you to modify process variables values, inject dll's and much more.
This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
This question already has answers here:
When should the volatile keyword be used in C#?
(11 answers)
Closed 7 years ago.
Is there any case in which volatile is useful in the context of single-threaded programming? I know it's used to make sure the value of the variable is always actually checked in memory so is there any case in which that value may change (in a ST app) in a way that the app/compiler won't notice?
No, it's not necessary. It is used to synchronize the memory content between the threads which in case you have only one it doesn't make sense.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't String.Empty a constant?
I wanted to use string.Empty in the Attributes of a few of my properties, but have since seen that it is not actually a Const but a static member.
Is there any reason Microsoft would do this?
I would say it's always a pretty bad idea using const in referenced assemblies.
The reason being the fact that the C# compiler treats constants as values and not as references, as I've said in this answer.
With this I mean that the C# compiler will replace all instances of the constant in you code and replace the "variable" with the value.
This means that even if you update the assembly GlobalConstants.dll and copy it to one of the applications you have, you will need to recompile that application. Not doing so, will cause the application to use the old constant values.
To overcome this problem, you can simply use public static readonly instead of public const as the readonly modifier differs from the const in that it is treated by the C# compiler as a reference in code rather than a value.
I think that the reason is: string is reference type, not value type. and it is faster to compare two references (when you use static member) than two instances of strings (when you use const)
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
CompilerServices.Operators equivalent on C#
I was looking for Microsoft.CSharp.CompilerServices.Operators but couldn't find it.
No there is no real equivalent to this in the C# runtime assembly.
However many these methods are essentially implementing the late bound operations for VB.Net in a declarative method (and indeed there are cases where the late binder simply just defers to these methods for operations). So these could be replicated in C# by defining methods which just explicitly defer to the C# dynamic binder.
For example, the rough equivalent of DivideObject in C# would be the following
public static dynamic DivideObject(dynamic left, dynamic right)
{
return left / right;
}