15.7 C
New York
Tuesday, May 6, 2025

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

Share To Your Friends

[ad_1]

If we have now two lessons class A and sophistication B, and B is derived from A then once 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 pieces is saved within the class, however that is the place many coders go improper; 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 house the place we have now so as to add the objects of each Class A and Class B and the article of sophistication A is consuming house of 20 KB then the article 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 fundamental()

{

    B fullObject;

    check(fullObject);

    return 0;

}

Output:

Operate 1

Rationalization: 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 could actually 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 value 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.

Purpose: A toddler class occasion can have a measurement in reminiscence greater than or equal to the guardian class as a result of a toddler class comprises all of the attributes and capabilities of the guardian class and may also have further attributes or capabilities.

So, once we attempt to create an occasion of the guardian class and assign it an instance of the kid class there will likely be inadequate reminiscence for the guardian 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 guardian class occasion which has solely attributes and capabilities discovered within the guardian class.

For extra particulars please seek advice from 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 Principal()

    {

        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 are going to 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 could actually 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 a substitute of kind A and that means we are able to entry attributes and strategies sure 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 Principal()

    {

        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 article itself as a substitute the one change that occurred is to the reference to that object. The thing itself didn’t must 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 once we say the reference is referencing a kind A object then the reference has solely entry to attributes and strategies that an object of kind A would have, and once we say the reference is referencing a kind B object then the reference has solely entry to attributes and strategies that an object of kind B would have.

And word that we are able to solely do such a conversion or casting solely to lessons which have an inheritance relationship between them like the instance above, in any other case it’s important to explicitly outline that conversion.

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

Why Object Slicing Doesn’t Occur in C#?

 C# class is a reference kind which suggests when the article 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 article is saved on the heap whereas a reference to that object is saved within the “mc” variable.

Which means once we attempt to assign a toddler class occasion to a guardian class occasion what actually occurs is that we assign a reference worth of a kid class occasion to a reference variable of a guardian class kind.

And since references shops solely the reminiscence deal with of that object within the heap all references have the identical measurement in reminiscence (sometimes 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 article as a result of we don’t retailer it’s valued immediately however as a substitute we use references.

So, once we are coping with class objects we don’t seek advice from them immediately as a substitute we seek advice from them utilizing references or pointers to things and that’s why in C# object slicing doesn’t occur not like different programming languages like C++ during which object slicing can occur.

[ad_2]


Share To Your Friends

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles