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.
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:
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:
When should one use dynamic keyword in c# 4.0?
(7 answers)
What's the difference between dynamic (C# 4) and var?
(14 answers)
Closed 6 years ago.
dynamic d = "hello";
Console.WriteLine (d.ToUpper()); // HELLO
Console.WriteLine (d.Foo()); // Compiles OK but gives runtime error
I'm reading a book an stumbled in this section, in the third line it only throws error during runtime but it will compile even though Foo method doesn't exist.
Why not check it in compile time rather than in runtime?
Edit:
What is the significance, and when can I use this concept?
With dynamic, you are instructing the compiler to ignore it and let the runtime handle it. This is super helpful when dealing with COM and other interfaces that the compiler doesn't know about.
If you want implicit types use var instead. The compiler will infer the type and keep it strongly typed.
The dynamic-keyword causes this behaviour which is intended. When making a variable dynamic you can do everything with it making it not compile-time-safe. So by making it dynamic you completely bypass the compiletime-types - that´s why you should take care when using it.
Checking the members at runtime is the whole point of dynamic though - why should it exist otherwise?
I assume you intentionally wanted the var-keyword which gives you compiletime-safety. Have a look at this point for the difference on both. As to the when to use the keyword have a look at this post.
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.
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.