Is volatile useful at all in a singlethreaded application in C#? [duplicate] - 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.

Related

C# - is it possible to modify values and call functions while the process is running? [duplicate]

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.

Is lock required for single writer situation? [duplicate]

This question already has answers here:
is locking necessary for Dictionary lookup?
(9 answers)
Closed 5 years ago.
I'm using .netcore with C#, I have a System.Collections.Generic.Dictionary<> that is shared and been access by multiple tasks, but there is only one task that is writing to and updating the key value pairs, and all other tasks(more than one) are only reading from the dictionary. In this situation, do I need to use a lock to protect it, in terms of thread safety?
Yes. Dictionary cannot be safely read from while a write is in process; the internal data storage is not in a valid state, and so exceptions or nonsensical results can arise.
Use a ConcurrentDictionary instead.

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

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.

C# lock vs Java syncronized - Is there any difference in runtime? [duplicate]

This question already has answers here:
Are there any differences between Java's "synchronized" and C#'s "lock"?
(3 answers)
Closed 9 years ago.
I'm wondering if there is any difference in runtime at lock vs syncronized.
I have learnd that syncronized is a slow operation and outdated at Java.
Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...
Thanks!
1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.
2 Locking is done at CPU level and there is no difference between Java and C# in this regard
see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

Singleton problem in c# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.

Categories