Thursday, 1 May 2014

Standard Naming Convention for ASP.NET and C#?



Standard Naming Convention for ASP.NET and C#
In programming language naming convention have great benefits to reduce the effort needed to read and understand source code. It provides better understanding in case of reuse code after a long interval of time. It is an initial step for beginner to learn any programming language. It is a very important element. Here I have explained some criteria about naming convention for programmer.

There are different types of naming casing style. First let’s understand different types of casing styles.

  • Camel Case (camelCase): First letter of the word is lower case and then each first letter of the part of the word is upper case. Example: numberOfDays
  • Pascal Case (PascalCase): First letter of the word is upper case and then each first letter of the part of the word is upper case. Example: DataTable
  • Underscode Prefix (_underscore): The word begins with underscore singe and for the rest of the word use camelCase rule. Example: _strFirstName
  • Hungarian Notation: First letter of the word is about its data type and rest of the word is camelCase. Example: iStudentNumber (i=integer)
  • Uppercase: All letters of the word are uppercase. Example: ID, PI

Naming Convention Guidelines
1.      Private Variables
Use Camel Case for private variables.
Example: studentName

2.       Local Variables
Use Camel Case for local variables.
Example: studentName

3.       Method
Use Pascal Case for method name.
Example: public string HelloWorld { ... }

4.       Property/ Enumerations
Use Pascal Case for Property/Enumerations.
Example: StudentName, StudentAddress

5.       Parameter
Use Camel Case for parameter
Example: void SayHello(string studentName)
{
     string fullMessage = "Hello " + studentName;
}

6.       Namespace
Use Pascal Case for namespace. Use the company name followed by the technology name and optionally the feature and design as follows:
CompanyName.TechnologyName[.Feature][.Design]
Example: CybarLab.Database, System.Web.UI, System.Windows.Forms

7.       Class
Use Pascal Case for class
Example:
public class HelloWorld { ... }

8.       Interface
Use Pascal Case for interface. Use Prefix “I” with interface name, to indicate that the type is an interface. Do not use the underscore character (_).
Example: IServiceProvider, IMemberDirectory

9.       Event
Use Pascal Case for event.
Example: protected void Button1_Click(object sender, EventArgs e) {…….}

10.    Exception
Visual Studio .NET use “e” parameter for the event parameter to the call. To avoid conflicting please use “ex" as a standard variable name for an exception object.
Example:
catch (Exception ex)
{
                        // Handle Exception
}

11.    Constant
Use uppercase for constant variables with words separated by underscores. It is recommended to use a grouping naming schema.
Example (for group AP_WIN):
AP_WIN_MIN_WIDTH, AP_WIN_MAX_WIDTH
12.    Avoid abbreviations longer than 5 characters
13.    Avoid using abbreviations unless the full name is excessive
Example:
Good: string student
Not good: string stu

14.    Use meaningful, descriptive words for naming variables

15.    All member variables must use Underscore Prefix so that they can be identified from other local variables names

16.    Avoid naming conflicts with existing .NET Framework namespaces or types

17.    Do not include the parent class name within a property name
Example
Good: Customer.Name
Not good: Customer.CustomerName

18.    Use Pascal Case for file names

19.    Method name should tell you what it does

20.    A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.

Naming Convention of ASP.NET Control
In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.


ASP.NET Control
Abbreviation
Standard Controls
Button
btn
CheckBox
cbx
CheckBoxList
cbxl
DropDownList
ddl
FileUpload
fu
HiddenField
hdn
Hyperlink
lnk
Image
img
ImageButton
ibtn
Label
lbl
LinkButton
lbtn
ListBox
lb
Literal
lit
MultiView
mv
Panel
pnl
PlaceHolder
ph
RadioButton
rbo
RadioButtonList
rbol
Table
tbl
TextBox
txt
View
v
Data Controls
DataList
dtl
DataPager
dp
DetailsView
dtv
EntityDataSource
ets
FormView
fv
GridView
gv
LinqDataSource
lds
ListView
lv
ObjectDataSource
ods
QueryExtender
qe
Repeater
rpt
SiteMapDataSource
smd
SqlDataSource
sds
XmlDataSource
xds
Validation Controls
CompareValidator
cpv
CustomValidator
ctv
RangeValidator
rv
RegularExpressionValidator
rev
RequiredFieldValidator
rfv
ValidationSummary
Vs

In summery we can say that use Pascal Case for anything which is public. Try to avoid underscore “_” or hyphen “-”.
...............................................................................................................................................................
Pascal case is used in .NET for naming Namespaces, Classes, Methods, Properties etc.
Examples:
Namespaces:
Use company name then technology name and then feature name with Pascal case like XYZCompany.Media
Classes:
Use noun or noun phrase with Pascal case like ImageViewer.
Don't use C as prefix for classes.
Interfaces:
Use I as prefix with Pascal case like IViewer.
Variables:
Either use Camel case like userID or Pascal case like UserID
Controls:
Use 3 digit prefix with Camel case.
Textbox: txtName
Button: btnName
Label: lblName
Panel: pnlViewer
Image: imgName
Properties:
Use noun or noun phrase with Pascal case like UserID, Password etc.
Methods:

Use verb or verb phrase with Pascal case like InsertQuery(), GetUserInfo() etc.


C# - Data Types?

Value types are stored in the Stack.
Examples : bool, byte, chat, decimal, double, enum , float, int, long, sbyte, short, strut, uint, ulong, ushort.

Reference types are stored in the Heap. 

Examples : class, delegate, interface, object, string.
------------------------------------------------------------------------------------------------------------
In C#, variables are categorized into the following types:
  • Value types
  • Reference types
  • Pointer types

Value Types

Value type variables can be assigned a value directly. They are derived from the classSystem.ValueType.
The value types directly contain data. Some examples are int, char, float, which stores numbers, alphabets, floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.
The following table lists the available value types in C# 2010:
TypeRepresentsRangeDefault
Value
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
char16-bit Unicode characterU +0000 to U +ffff'\0'
decimal128-bit precise decimal values with 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
double64-bit double-precision floating point type(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32-bit single-precision floating point type-3.4 x 1038 to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-923,372,036,854,775,808 to 9,223,372,036,854,775,8070L
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
ushort16-bit unsigned integer type0 to 65,5350
To get the exact size of a type or a variable on a particular platform, you can use the sizeof method. The expression sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:
namespace DataTypeApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Size of int: {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}
When the above code is compiled and executed, it produces the following result:
Size of int: 4

Reference Types

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.
In other words, they refer to a memory location. Using more than one variable, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are:objectdynamic and string.

OBJECT TYPE

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. So object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.
When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.
object obj;
obj = 100; // this is boxing

DYNAMIC TYPE

You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time.
Syntax for declaring a dynamic type is:
dynamic <variable_name> = value;
For example,
dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables take place at run time.

STRING TYPE

The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class. It is derived from object type. The value for a string type can be assigned using string literals in two forms: quoted and @quoted.
For example,
String str = "Tutorials Point";
A @quoted string literal looks like:
@"Tutorials Point";
The user-defined reference types are: class, interface, or delegate. We will discuss these types in later chapter.

Pointer Types

Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as in C or C++.
Syntax for declaring a pointer type is:
type* identifier;
For example,
char* cptr;
int* iptr;

SQL - Data Types?

SQL Server offers six categories of data types for your use:

Exact Numeric Data Types:

DATA TYPEFROMTO
bigint-9,223,372,036,854,775,8089,223,372,036,854,775,807
int-2,147,483,6482,147,483,647
smallint-32,76832,767
tinyint0255
bit01
decimal-10^38 +110^38 -1
numeric-10^38 +110^38 -1
money-922,337,203,685,477.5808+922,337,203,685,477.5807
smallmoney-214,748.3648+214,748.3647

Approximate Numeric Data Types:

DATA TYPEFROMTO
float-1.79E + 3081.79E + 308
real-3.40E + 383.40E + 38

Date and Time Data Types:

DATA TYPEFROMTO
datetimeJan 1, 1753Dec 31, 9999
smalldatetimeJan 1, 1900Jun 6, 2079
dateStores a date like June 30, 1991
timeStores a time of day like 12:30 P.M.
Note: Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute accuracy.

Character Strings Data Types:

DATA TYPEFROMTO
charcharMaximum length of 8,000 characters.( Fixed length non-Unicode characters)
varcharvarcharMaximum of 8,000 characters.(Variable-length non-Unicode data).
varchar(max)varchar(max)Maximum length of 231characters, Variable-length non-Unicode data (SQL Server 2005 only).
texttextVariable-length non-Unicode data with a maximum length of 2,147,483,647 characters.

Unicode Character Strings Data Types:

DATA TYPEDescription
ncharMaximum length of 4,000 characters.( Fixed length Unicode)
nvarcharMaximum length of 4,000 characters.(Variable length Unicode)
nvarchar(max)Maximum length of 231characters (SQL Server 2005 only).( Variable length Unicode)
ntextMaximum length of 1,073,741,823 characters. ( Variable length Unicode )

Binary Data Types:

DATA TYPEDescription
binaryMaximum length of 8,000 bytes(Fixed-length binary data )
varbinaryMaximum length of 8,000 bytes.(Variable length binary data)
varbinary(max)Maximum length of 231 bytes (SQL Server 2005 only). ( Variable length Binary data)
imageMaximum length of 2,147,483,647 bytes. ( Variable length Binary Data)

Misc Data Types:

DATA TYPEDescription
sql_variantStores values of various SQL Server-supported data types, except text, ntext, and timestamp.
timestampStores a database-wide unique number that gets updated every time a row gets updated
uniqueidentifierStores a globally unique identifier (GUID)
xmlStores XML data. You can store xml instances in a column or a variable (SQL Server 2005 only).
cursorReference to a cursor object
tableStores a result set for later processing

Difference between char varchar and nvarchar in sql server?

Char DataType

Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted.


varchar DataType

Varchar means variable characters and it is used to store non-unicode characters. It will allocate the memory based on number characters inserted. Suppose if we declared varchar(50) it will allocates memory of 0 characters at the time of declaration. Once we declare varchar(50) and insert only 10 characters of word it will allocate memory for only 10 characters.

nvarchar DataType

nvarchar datatype same as varchar datatype but only difference nvarchar is used to store Unicode characters and it allows you to store multiple languages in database. nvarchar datatype will take twice as much space to store extended set of characters as required by other languages.

So if we are not using other languages then it’s better to use varchar datatype instead of nvarchar


Non-Unicode vs. Unicode Data Types: Comparison Chart
The primary difference between unicode and non-Unicode data types is the ability of Unicode to easily handle the storage of foreign language characters which also requires more storage space.
Non-UnicodeUnicode
(char, varchar, text)(nchar, nvarchar, ntext)
Stores data in fixed or variable lengthSame as non-Unicode
char: data is padded with blanks to fill the field size. For example, if a char(10) field contains 5 characters the system will pad it with 5 blanksnchar: same as char
varchar: stores actual value and does not pad with blanksnvarchar: same as varchar
requires 1 byte of storagerequires 2 bytes of storage
char and varchar: can store up to 8000 charactersnchar and nvarchar: can store up to 4000 characters
Best suited for US English: "One problem with data types that use 1 byte to encode each character is that the data type can only represent 256 different characters. This forces multiple encoding specifications (or code pages) for different alphabets such as European alphabets, which are relatively small. It is also impossible to handle systems such as the Japanese Kanji or Korean Hangul alphabets that have thousands of characters."1Best suited for systems that need to support at least one foreign language: "The Unicode specification defines a single encoding scheme for most characters widely used in businesses around the world. All computers consistently translate the bit patterns in Unicode data into characters using the single Unicode specification. This ensures that the same bit pattern is always converted to the same character on all computers. Data can be freely transferred from one database or computer to another without concern that the receiving system will translate the bit patterns into characters incorrectly.

Static Class

Static Class: Declared with Static keyword, methods in Static Class are also static along with variables of the class.
This class cannot be instantiated, i.e we cannot have objects of this class. To access methods of this class, you can directly use classname.method. Also this class cannot be inherited.

If a class is declared as static then the variables and methods should compulsorily be declared as static.
A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.
->The main features of a static class are:
  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.
static class CollegeRegistration
{
  //All static member variables
   static int nCollegeId; //College Id will be same for all the students studying
   static string sCollegeName; //Name will be same
   static string sColegeAddress; //Address of the college will also same

    //Member functions
   public static int GetCollegeId()
   {
     nCollegeId = 100;
     return (nCollegeID);
   }
    //similarly implementation of others also.
} //class end


public class student
{
    int nRollNo;
    string sName;

    public GetRollNo()
    {
       nRollNo += 1;
       return (nRollNo);
    }
    //similarly ....
    public static void Main()
   {
     //Not required.
     //CollegeRegistration objCollReg= new CollegeRegistration();

     //<ClassName>.<MethodName>
     int cid= CollegeRegistration.GetCollegeId();
    string sname= CollegeRegistration.GetCollegeName();


   } //Main end
}

How to improve applications performance which is hosted in cloud ?

Improving the performance of an application hosted in Microsoft Azure involves a combination of optimizing your application code, leveraging...