Answer the question
In order to leave comments, you need to log in
What does the compiler do when passing an object by reference with explicit conversion (ref(SomeClass)anotherSomeClass)?
The method takes arguments, for example, of the Animal type, and you need to pass the Dog type there, while Dog is the successor of Animal.
The compiler swears at the line with the someMethod(ref (Animal)pet);
error "ref or out argument must be an assignable variable"
Googled it, found only posts in which they try to do something like this: someMethod(ref new Dog());
In connection with which the question arose. Does the compiler create a new object when passing arguments by reference?
Answer the question
In order to leave comments, you need to log in
In c#, objects are always passed by reference, but when passing ref and out, roughly speaking, a reference to a reference is passed. The ref parameter is allowed, but the out parameter must return a new object.
Why casting is not allowed for ref and out:
class Animal...
class Dog : Animal...
class Cat : Animal...
void main()
{
Dog dog = new Dog();
someMethod(ref (Animal)dog);
dog == ?
}
void someMethod(ref Animal pet)
{
if (!(pet is Cat)) {
pet.kill();
pet = new Cat();
}
}
What's stopping you from doing a type cast by the string above and sending a new parameter? Religious beliefs? Or a bonus for the minimum number of lines of code? If you pass reference types to a method, then ref is just a huge sign "Attention, the object can be changed inside." C# always passes reference types by reference.
UPD: Reading the documentation :
The argument that is passed to the ref parameter must be initialized before being passed. This is in contrast to out parameters, whose arguments do not need to be explicitly initialized before being passed.
A little from myself.
ref - passes not a link to a link or anything else.
ref - passes the original reference (if it is nulled or changed, the value of the variable passed through ref will also change).
(type A) type B - type casting (upcast or downcast) does not create a new object, and does not even change the current one, it simply interprets an object of one type as an object of another type. (method table is used)
> "ref or out argument must be an assignable variable"
they write to you - what you pass is not a variable (or rather, a variable to which you can assign something). A reference from the pet variable cast to Animal is already a "value", not a "variable". You can put something in a pet, but you can't put it in an (Animal)pet - because it is not a variable, but an expression whose value can be evaluated.
As already noted above, ref and out require the transfer of a VARIABLE into which you can (and should, in the case of out) write something. And it doesn't matter - the reference type has a variable or a value type. If you pass ref Dog, then you can write a reference to the Dog object into this parameter, if you pass ref int, you can write int here.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question