Tuesday, 13 December 2016

In Depth Of C# : PART 1




Class Declaration

 A class is a template for what an object looks like and how it behaves.A class is a container which have data(attributes) and functions(methods). C# is a true object-oriented language and therefore, ‘everything must be placed inside a class'.Each class is responsible for a piece of functionality.  Class is a keyword and declares that a new class definition follows.  
class Program    
{

} 

Code Blocks

A code block is a grouping of statements. This is done by enclosing the statements between opening and closing curly braces. Once a block of code has been created, it becomes a logical unit that can be used any place a single statement can.  Note that there is no semicolon after the closing brace.  Example:   
                 {  
        
  } 

The Main Method

Every C# executable program must include the Main() method in one of the classes.  This is the ‘starting point’ for executing the program.  A C# application can have any number of classes but ‘only one’ class can have the Main method to initiate the execution. In C#, the method body begins with an opening brace ‘{‘ and ends with a corresponding closing brace ‘}’ (i.e, block).  
 Example: 

         

The Main() method begins with the keyword static. A method that is modified by static can be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void indicates that Main( ) does not return a value. The empty parentheses that follow Main indicate that no information is passed to Main( ). 

Main() Returning a Value 

Main() method can also return a value to the system if the return type is declared as int and including return statement at the end of the method.  The purpose of this code is to allow communication of success ore failure to the execution environment.  Example: 

Namespaces

As the number of  classes increases we need them to segregate into reasonable groupings.  In C#, a namespace defines a declarative region. Thus namespace is a container for relative classes.The use of namespaces is to keep one set of names separate from another. In essence, names declared in one namespace will not conflict with names declared in a different namespace.
Example:    




In this, System is the namespace (scope) in which the Console class is located.  A class in a namespace can be accessed using the dot operator as illustrated in the statement above.
C# supports a feature known as using directive that can be used to import the namespace System into the program.  Once a namespace is imported, we can use the elements of that namespace without using the namespace as prefix.
Example:      using System;
This tells the compiler to look in the System library for unresolved class names.  When the compiler parses the Console.WriteLine() method, it will understand that the method is undefined.  However, it will then search through the namespaces specified in using directives and, upon finding the method in the System namespace, will compile the code without any complaint. 



Adding Comments 

Comments are used to enhance readability and understanding of code.  All programs should have information such as implementation details, change history and tasks performed.  The contents of a comment are ignored by the compiler.  C# permits two types of comments, namely, 

• Single-line comments
• Multiline comments

Single-line comments
Single-line comments begin with a double backslash (//) symbol and terminate at the end of the line.  We can use // on a line of its own or after a code statement.  This can also be used to comment out an entire line or part of a line of source code.  Everything after the // on a line is considered a comment.  Example:
// Program to add two numbers


Multiline comments

The multiline comment starts with the /* characters and terminates with */.   Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment can be several lines long. This comment can also occur within a line. 

Example:
/*         Program to add two numbers  

*/ 

using /*namespace */ System; 
     for(i=1; i<count /* Total number of Records */; i++)      
{   
  
}





MORE COMING UP!

No comments:

Post a Comment