| By Hovhannes Avoyan | Article Rating: |
|
| April 2, 2012 06:00 AM EDT | Reads: |
1,426 |
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 Fields – Limit 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.
Published April 2, 2012 Reads 1,426
Copyright © 2012 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- Cloud People: A Who's Who of Cloud Computing
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- Windows Azure IaaS Reaches General Availability
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Basho Announces Open Source Riak CS and General Availability of Riak CS Enterprise v1.3
- Symphony EYC Appoints New Account Manager to Drive Global Opportunities
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Computing Is Simplifying Things
- Cloud Expo New York: Developing the World’s First IaaS Marketplace
- Cimtrek announces the general release of its Lotus Notes migrator for Microsoft’s SharePoint platform
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Best CIO Practices Shared from SHI’s Customers
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- Windows Azure IaaS Reaches General Availability
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- The PostOpen Event – Why It Is So Important
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- The Cover and the Epilogue of the Upcoming Book
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Small Cancers, Big Data, and a Life Examined
- Basho Announces Open Source Riak CS and General Availability of Riak CS Enterprise v1.3
- Cloud Expo NY: Calculating the True Value of Industry-Specific Clouds
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Where Are RIA Technologies Headed in 2008?
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"






















