Saturday, January 23, 2016

C# - Guide

The first Program
using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

Main method : Each program in C# has a main method where execution starts from. Main
method is Static method, which do not need any object to access it.

C# Coding Guidelines C# language specification does not provide any set of rules that
is needed to abide while coding. But certain guidelines if followed adds values and
makes code consistent.
  • Use the default Code Editor settings (smart indenting, four-character indents, tabs saved as spaces)
  • Write only one statement per line.
  • Write only one declaration per line.
  • If continuation lines are not indented automatically, indent them one tab stop (four spaces).
  • Add at least one blank line between method definitions and property definitions.
  • Use parentheses to make clauses in an expression apparent, as shown in the following code.
Commenting Convention
  • Place the comment on a separate line, not at the end of a line of code.
  • Begin comment text with an uppercase letter.
  • End comment text with a period.
  • Insert one space between the comment delimiter (//) and the comment text

Language Guideline

String Data Type

  • To concatenate short string use + operator - Ex. string x=First+Last;
  • To append string in loop, use StringBuilder

Implicitly Typed Local Variables

  • Use implicit type when value is obvious from right side of assignment.
Ex- var x = 2+3;
  • DO not use var, and use explicit data type when type of right side is not obvious.
        Ex. int var4 = ExampleClass.ResultSoFar();
Avoid use of var in place of dynamic .

No comments:

Post a Comment