M
M
Multigame2018-10-04 15:50:27
C++ / C#
Multigame, 2018-10-04 15:50:27

How are Dictinaries passed in C#?

Good afternoon.
Tell me please, because dictionaries are always passed by reference?
Those. for example. There are two classes ClassA () and ClassB ()
We create a dictionary and pass it to the method of class A, where we change it.
Then we create and pull the method of class B, into which we also betray the dictionary.
Will we see in class B the changes made in the dictionary by the method of class A?
Will the asynchrony of class methods affect the result? await-async

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2018-10-04
@Multigame

Dictionaries are passed by reference. But if you really want to, you can pass them by value manually.
The async/await async allows dictionaries to be used without exceptions. (You're just executing the code in a different order.)
You may have confused asynchrony with multithreading, where it is desirable not to change dictionaries from different threads.

#
#, 2018-10-04
@mindtester

and writing a test in 5 lines is not fate?
in case of assigning a variable, a reference is assigned, then more than one variable can point to the same object instance
as method parameters , copies of objects are usually passed, I think Dictionary obeys the general rules * (I remind you - this is elementary to check - a console application on several lines)
to guarantee the transfer of a reference as a parameter, you should use the ref
ps * prefix - two checks confirmed the transfer by reference, well, it means by reference, sorry for the haste))

Will the asynchrony of class methods affect the result? await-async
I think here it is necessary to look for information about thread safety . for example
https://docs.microsoft.com/ru-ru/dotnet/standard/c...
Dictionary doesn't seem to be mentioned there, but ConcurrentDictionary is mentioned, maybe you need to look at the System.Collections.Concurrent namespace

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question