This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Why ref and out in C#?
(7 answers)
Closed 9 years ago.
As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.
Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.
Because the semantics of the two are completely different.
An out parameter is used to indicate that it will be used to return (output) a value, nothing more.
A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.
Related
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.
This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Passing By ref and out
(1 answer)
Closed 4 years ago.
In C#, ref and out keyword.
How to it affect memory management? Is there any difference in memory management for ref and out keyword?
Even though the mechanism used behind the scene is the same, the difference between the two keywords is in what the compiler must verify about each parameter:
If you pass a parameter with ref keyword, the compiler checks that you have initialized it before making the call
If you pass a parameter with out keyword, the compiler checks that the method that you call has made an assignment to the corresponding argument before exiting.
This difference allows for the out var construct, which has been added to C# 7.0. This feature would not be possible with ref alone because of the initialization requirement.
There is no difference between the two as far as the memory management is concerned: in both cases the reference itself is passed by value, and the code using the reference is adding an extra level of dereference.
This question already has answers here:
Read-only ("const"-like) function parameters of C#
(4 answers)
Closed 5 years ago.
If I pass an object in C++ like so:
template<typename MyType>
[public] MyType myMethodOrFunction(const MyObject& obj) {}
Would the following be equivalent in C#:
[public] MyType myMethodOrFunction<T>(MyObject obj) {}
?
Exactly the same question was asked here:
Const function parameter in C#
No, it won't have exactly the same semantics. The C# code you showed would have the same semantics as passing a pointer by value in C++, rather than passing a value by reference. You can pass a parameter by reference in C# using ref or out, but you cannot pass a read only reference; such references will always be mutable, thus there is no way of providing identical semantics to that C++ code in C#. That said, while the semantics aren't identical, that should probably be a class (having value types is quite rare in C#) and it should probably be passed by value, it's again rare to need otherwise, just realize that it won't have exactly the same semantics.
Not exactly.
I think the closest you are going to get is this:
public T myMethodOrFunction<T>(ref MyObject obj) {}
You don't have const in this context in C#. And it is important to note that obj could be a reference or a value type. The difference, in C++ terms, is that a reference type is essentially a pointer (MyObject*) whereas a value type would just be the value (MyObject). So if obj is a value type, then that is closer to the C++ version.
This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 5 years ago.
I've been trying to understand passing by reference and passing by value and trying to understand it lead me to trying to understand reference data types. In all the explanations I've seen online they define a reference data type to be referencing data as opposed to containing data, and i'm wandering what is the difference?
What i think so far is that the variable doesn't contain data itself but it is referencing a method, but i don't feel like that is a satisfactory understanding. Hopefully someone could clear this up for me.
When you pass the reference of an variable, you are actually sending the memory address of that variable, whoever getting the address will be able to change your variable's value. but if you are passing by value, you are sending a copy of your variable, so even if the receiver changes the value of that variable, you will still get the original value of your variable.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?
Why in C# string is a class/ref type , where as int/double are value/struct- any specific reason or it is by design
Integral types have the important property of being accessible as a whole by the processor in one go. It is not the case for a string which may be composed of thousands of bytes, so in all languages, strings have always been pointed to, because the computer cannot really do it any other way.
In an object language like C#, it is canonical to create a class to point to a memory location: that's actually what an object is about.
So yes, strings are classes, because they can't be integral types.