Assigning an instance clone to the base interface

Let's say:

public interface IBase
{
// Stuff
}

public class Derived : IBase
{
// Stuff
}

While doing

Derived instance_ = new CDrv(); 
Ibase ibase = instance_; // Line 1

Is this an implicit conversion or assignment?

Is it possible to create a clone instance_and assign / convert to ibase, possibly by overriding the conversion or assignment operator or some other method?

Or, in other words, is there a way to pass instance_by value and not make it struct? I do not want this to be a structure, because I have a number of functions that return Derived, and the specified conversion / assignment will rarely occur.

+3
source share
1 answer

There is no implicit conversion that clones an object.

, "" ( ICloneable, ), , :

IBase ibase = new Derived(instance_); // Using "copy constructor" you create

:

IBase ibase = instance_.Clone(); // ie: implement ICloneable, or similar mechanism
+6

Source: https://habr.com/ru/post/1792850/


All Articles