Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap