Monday, June 6, 2022
HomeWordPress DevelopmentWhat's Object Slicing and Why it Does not Occur in C#?

What’s Object Slicing and Why it Does not Occur in C#?


If we have now two lessons class A and sophistication B, and B is derived from A then after we assign an object of sophistication B to an object of sophistication A then all further attributes of sophistication B object are sliced off to type the bottom class object (class A object). In easy phrases, when additional elements of a derived class are sliced or not used and the precedence is given to the bottom class’s object that is termed object slicing. 

Now you’ll be questioning why we have now to prioritize the objects when all the things is saved within the class, however that is the place many coders go mistaken; the reminiscence worth is saved within the object of a category and by no means within the class in itself. Hypothetically, if we solely have 20 KB of area the place we have now so as to add the objects of each Class A and Class B and the thing of sophistication A is consuming area of 20 KB then the thing of Class B will likely be sliced routinely. 

Instance From the C++ Language:

C++

#embrace <iostream>

utilizing namespace std;

  

class A {

public:

    void fun1() { cout << "Operate 1" << endl; }

};

  

class B : public A {

public:

    void fun2() { cout << "Operate 2" << endl; }

};

  

void check(A slicedObject) { slicedObject.fun1(); }

  

int predominant()

{

    B fullObject;

    check(fullObject);

    return 0;

}

Output:

Operate 1

Clarification: Within the above code we made two lessons:

Class A: Accommodates one perform referred to as fun1 which prints to the console the sentence “Operate 1”.

Class B: Accommodates one perform referred to as fun2 which prints to the console the sentence “Operate 2”, and it inherits from class A so it might use fun1 additionally.

Now we have now the perform check which takes one parameter of kind A. If we attempt to cross an object of kind B to the perform it should succeed however with a price of what’s referred to as object slicing. Now if we attempt to execute fun2 inside the perform check it should generate a compile error as a result of it can’t discover the perform inside the object capabilities.

Motive: A baby class occasion can have a dimension in reminiscence greater than or equal to the mother or father class as a result of a toddler class accommodates all of the attributes and capabilities of the mother or father class and can even have further attributes or capabilities.

So, after we attempt to create an occasion of the mother or father class and assign it an instance of the kid class there will likely be inadequate reminiscence for the mother or father object to carry all the extra attributes and capabilities of the kid class occasion.

So, it slices all the extra attributes and capabilities off from the kid class occasion to type the mother or father class occasion which has solely attributes and capabilities discovered within the mother or father class.

For extra particulars please consult with the Object Slicing in C++ article.

Similar Instance in C#:

C#

utilizing System;

  

class A {

    public void fun1() { Console.WriteLine("Operate 1"); }

}

  

class B : A {

    public void fun2() { Console.WriteLine("Operate 2"); }

}

  

  

class Program {

    public static void Most important()

    {

        B fullObject = new B();

        check(fullObject);

    }

    public static void check(A notSlicedObject)

    {

        notSlicedObject.fun1();

    }

}

Within the above code, we created the identical instance we did beforehand however with C# this time.

We’ll discover that we nonetheless can’t use notSlicedObject.fun2(), so what’s the distinction?

The distinction is that notSlicedObject is a reference to an object of kind A, so it might solely use strategies or attributes bounded to that object kind.

However we are able to solid the kind of the reference to make it reference an object of kind B as an alternative of kind A and that method we are able to entry attributes and strategies certain to kind B.

C#

utilizing System;

  

class A {

    public void fun1()

    {

        Console.WriteLine("Operate 1");

    }

}

  

class B : A

{

    public void fun2()

    {

        Console.WriteLine("Operate 2");

    }

}

class Program

{

    public static void Most important()

    {

        B fullObject = new B();

        check(fullObject);

          

    }

    public static void check(A notSlicedObject)

    {

        ((B)notSlicedObject).fun2();

    }

     

}

Word: Casting the reference kind didn’t change the kind of the thing itself as an alternative the one change that occurred is to the reference to that object. The item itself didn’t should be sliced off as a result of it by no means modified.

We solely change the kind of the reference that’s referencing that object, in order that after we say the reference is referencing a sort A object then the reference has solely entry to attributes and strategies that an object of kind A would have, and after we say the reference is referencing a sort B object then the reference has solely entry to attributes and strategies that an object of kind B would have.

And be aware that we are able to solely do any such conversion or casting solely to lessons which have an inheritance relationship between them like the instance above, in any other case you must explicitly outline that conversion.

For extra details about user-defined conversion in C# refer this: Person-defined Conversion Operators

Why Object Slicing Doesn’t Occur in C#?

 C# class is a reference kind which implies when the thing is created, sufficient reminiscence is allotted on the managed heap for that particular object, and the variable holds solely a reference to the situation of mentioned object.

Instance:

C#

MyClass mc = new MyClass();

Right here the thing is saved on the heap whereas a reference to that object is saved within the “mc” variable.

Meaning after we attempt to assign a toddler class occasion to a mother or father class occasion what actually occurs is that we assign a reference worth of a kid class occasion to a reference variable of a mother or father class kind.

And since references shops solely the reminiscence handle of that object within the heap all references have the identical dimension in reminiscence (usually 4-bytes for 32-bit CPU structure and 8-bytes for 64-bit), we are able to simply assign or solid the kind of the reference with out slicing off the thing as a result of we don’t retailer it’s valued immediately however as an alternative we use references.

So, after we are coping with class objects we don’t consult with them immediately as an alternative we consult with them utilizing references or pointers to things and that’s why in C# object slicing doesn’t occur in contrast to different programming languages like C++ during which object slicing can occur.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments