Technical guide lines for .Net(C#, ASP.NET) programming
1) Sort all the namespaces alphabetically as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
2) Use #region for Segregation
#region Page Event Handlers
#region UI Event Handlers
#region Helper Methods
3) Always initialize String type with String.empty
Wrong:
String CardNo = "";
Correct:
String CardNo = String.empty;
4)Avoid parameters
When you call any method in the C# language that was not in-lined, the run time will actually physically copy the variables you pass as arguments to the formal parameter slot memory in the called method. It causes stack memory operations and incurs a performance hit. It is faster to minimize arguments, and even use constants in the called methods instead of passing them arguments.
5) Avoid local variables
When you call a method in your C# program, the run time allocates a separate memory region to store all the local variable slots. This memory is allocated on the stack even if you do not access the variables in the function call. Therefore, you can call methods faster if they have fewer variables in them.
6) Use arrays
In the .NET Framework, you have many options for collections, such as the List type, and various other types such as ArrayList. While these types are convenient and should be used when necessary, it is always more efficient to use a simple array if this is possible. The reason for this is that the more complex collections such as List are actually composed of internal arrays. They add logic to avoid the burden of managing the array size on each use. However, if you do not need this logic, or can adjust your code so that the logic is not needed, using an array will be faster.
7)Use StringBuilder
If you are doing significant appending of strings using the C# language, the StringBuilder type can improve performance. This is because the string type is immutable and cannot be changed without reallocating the entire object. Sometimes, using strings instead of StringBuilder for concatenations is faster; this is typically the case when using very small strings or doing infrequent appends.
8) Use static fields
Here, we note that static fields are faster than instance fields, for the same reason that static methods are faster than instance methods. When you load a static field into memory, you do not need the runtime to resolve the instance expression. Loading an instance field must have the object instance first resolved. In fact, even in an object instance, loading a static field is faster because no instance expression instruction is ever used.
9) Comparing Non-Case-Sensitive Strings
In an application sometimes it is necessary to compare two string variables, ignoring the cases. The tempting and traditionally approach is to convert both strings to all lower case or all upper case and then compare them, like such:
str1.ToLower() == str2.ToLower()
However repetitively calling the function ToLower() is a bottleneck in performace. By instead using the built-in string.Compare() function you can increase the speed of your applications.
To check if two strings are equal ignoring case would look like this:
string.Compare(str1, str2, true) == 0 //Ignoring cases
The C# string.Compare function returns an integer that is equal to 0 when the two strings are equal.
10) Use && and || operators
When building if statements, simply make sure to use the double-and notation (&&) and/or the double-or notation (||), (in Visual Basic they are AndAlso and OrElse).
If statements that use & and | must check every part of the statement and then apply the "and" or "or". On the other hand, && and || go thourgh the statements one at a time and stop as soon as the condition has either been met or not met.
Executing less code is always a performace benefit but it also can avoid run-time errors, consider the following C# code:
if (object1 != null && object1.runMethod())
If object1 is null, with the && operator, object1.runMethod()will not execute.
If the && operator is replaced with &, object1.runMethod() will run even if object1 is already known to be null, causing an exception.
11)Lazy Instantiation/Initialization
The Singleton design pattern is often used to provide a single global instance of a class. Sometimes it's the case that a particular singleton won't be needed during an application run. It's generally good practice to delay the creation of any object until it's needed, unless there's a specific need to the contrary - for instance, to pre-cache slow-initializing objects such as database connections. The "double-checked locking" pattern is useful in these situations, as a way to avoid synchronization and still ensure that a needed action is only performed once. Lazy initialization is a technique that can enhance the performance of an entire application through object reduction.
12)Working with Objects and Value Types
Objects are expensive to use, partly because of the overhead involved in allocating memory from the heap (which is actually well-optimized in .NET) and partly because every created object must eventually be destroyed. The destruction of an object may take longer than its creation and initialization,especially if the class contains a custom finalization routine. Also, the garbage collector runs in an indeterministic way; there's no guarantee that an object's memory will be immediately reclaimed when it goes out of scope, and until it's collected, this wasted memory can adversely affect performance.
It's necessary to understand garbage collection to appreciate the full impact of using objects. The single most important fact to know about the garbage collector is that it divides objects into three "generations": 0, 1, and 2. Every object starts out in generation 0; if it survives (if at least one reference is maintained) long enough, it goes to 1; much later, it transitions to 2. The cost of collecting an object increases with each generation. For this reason, it's important to avoid creating unnecessary objects, and to destroy each reference as quickly as possible. The objects that are left will often be long-lived and won't be destroyed until application shutdown.
13) Using the 'Sealed' Keyword
Wherever extensibility is not required, you should use the sealed keyword. This makes your design easier to understand, as someone can tell at a glance if a certain class or method isn't meant to be extended or overridden. It also increases the chances that the compiler will inline code.
15) Use Page.IsPostback to avoid unnecessary processing on a round trip.
We collect videos & images from online sites like youtube and some other websites which provide them. And most of the News is also gathered from other online websites. Any material downloaded or otherwise obtained through the use of the service is done at your own discretion and risk and that you will be solely responsible for any damage to your computer system or loss of data that results from the download of any such material. If you feel that the content on the site is illegal or Privacy please contact us at srinuchalla@gmail.com and such videos, images or any Content will be removed with immediate effect.