Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C#
Expertise: Beginner
Aug 16, 2005



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Navin Varma
Thanks for your registration, follow us on our social networks to keep up-to-date