devxlogo

Structs with Care: Boxing

Structs with Care: Boxing

Consider an employee service, say in an SOA-based implementation, which exposes the following method:

Employee    getEmployeeInfo(int employeeId)

Looking at this signature, Employee can be defined as struct because it would be faster and lighter weight compared to defining Employee as a class:

struct Employee{int employeeId;int department;string name;string designation;string address;....}

In order to reduce network overhead, the Employee service should also provide the following overload:

Employee [] getEmployeeInfo(int[] employeeId)

Now, should Employee be a struct or a class (a value type or a referenece type)?Since the array is a reference type, boxing will kick in to convert the value type to a reference type at runtime and performance overhead will be incurred. This makes it sensible to make Employee a class rather than a struct.

Although this explanation cites an SOA-based implementation, it is applicable in any scenario involving boxing.

See also  5 Benefits of Using Time Tracking Software
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist