Welcome!

.NET Authors: Matt Hester, Liz McMillan, Elizabeth White, Lori MacVittie, Adine Deford

Related Topics: .NET

.NET: Blog Feed Post

Improving .NET Application Performance

Part 3: Class Design Considerations

Use the sealed keyword – If you don’t need or want to extend your base classes, consider marking them with the ‘sealed’ keyword. If you derive a class from a base class with virtual members and want to prevent further extensibility of the derived class, use the ‘sealed’ keyword with the virtual members of your derived class. Sealing virtual methods allows for inlining and other compiler optimizations.

public class MyClass{

 

  protected virtual void SomeMethod() { ... } 
}

Overdiding and seal a method in a derived class.

public class DerivedClass : MyClass { 
  protected override sealed void SomeMethod () { ... } 
}
 

Avoid unnecessary virtual members – Virtual members are more expensive to call because of the virtual lookup table and don’t work for certain runtime optimizations.

Use overloaded methods – Instead of using a method with a variable number of parameters, use overloaded methods instead. Just be aware that COM clients can’t work with overloaded methods. In that case use methods with different names.

//method taking variable number of arguments

void GetCustomers (params object [] filterCriteria)

//overloaded methods

void GetCustomers (int countryId, int regionId)

void GetCustomers (int countryId, int regionId, int CustomerType)

Overriding the Equals method – overriding the Equals method that is provided by System.Object can further improve performance. An Equals method that is specific to your value type can do a comparison much more cheaply then using the standard implementation which uses Reflection to perform the comparison.

public struct Rectangle{
  public double Length;
  public double Breadth;
  public override bool Equals (object ob) {
  if(ob is Rectangle)
    return Equals((Rectangle)ob);
  else
    return false;
 
  }
  private bool Equals(Rectangle rect) {
    return this.Length == rect.Length && this.Breadth==rect.Breadth;
  }
}

Accessing a Class Property – Properties look like fields but they are not and have a hidden cost. Be aware that if you access a property additional code might be executed and accessing a property might be slower than accessing a field directly. Of course, the additional code behind a property is generally there for good reason; for example, to validate data.

If your object is designed for remote access, use methods with multiple parameters instead of requiring the client to set multiple properties or fields, reducing round trips. Also, it is considered bad practice to use properties that hide complex business rules or other costly operations; callers of properties generally expect the cost to be inexpensive.

Private vs. Public Member Variables - Avoid unnecessary public members to prevent any serialization overhead when you use the XmlSerializer class. As you well know, this class serializes all public members by default.

Limit the Use of Volatile FieldsLimit the use of the volatile keyword because volatile fields restrict the way the compiler reads and writes the contents of the field. The compiler generates the code that always reads from the field’s memory location instead of reading from a register that may have loaded the field’s value. This means that accessing volatile fields is slower than nonvolatile ones because the system is forced to use memory addresses rather than registers.

These are the most important class design considerations that can help you to optimize your managed code. In our next article we’ll focus on Garbage Collection.

More Stories By Hovhannes Avoyan

Hovhannes Avoyan is the CEO of Monitis, Inc., a provider of on-demand systems management and monitoring software to 50,000 users spanning small businesses and Fortune 500 companies.

Prior to Monitis, he served as General Manager and Director of Development at prominent web portal Lycos Europe, where he grew the Lycos Armenia group from 30 people to over 200, making it the company's largest development center. Prior to Lycos, Avoyan was VP of Technology at Brience, Inc. (based in San Francisco and acquired by Syniverse), which delivered mobile internet content solutions to companies like Cisco, Ingram Micro, Washington Mutual, Wyndham Hotels , T-Mobile , and CNN. Prior to that, he served as the founder and CEO of CEDIT ltd., which was acquired by Brience. A 24 year veteran of the software industry, he also runs Sourcio cjsc, an IT consulting company and startup incubator specializing in web 2.0 products and open-source technologies.

Hovhannes is a senior lecturer at the American Univeristy of Armenia and has been a visiting lecturer at San Francisco State University. He is a graduate of Bertelsmann University.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.