Saturday, 18 May 2013

PROGRAMMING PART OF C# - AN INTRODUCTION TO WRITTING PROGRAMS

AN INTRODUCTION TO WRITING PROGRAMS

           Now we are going for simple programming.   


USING C # FOR WRITING PROGRAMS

               Similar to the various programming languages, C# also has some predefined keywords that can be used for writing programs.  For example, class is a keyword in C# that is used to define classes.  Keywords are reserved words that have a special meaning.  Further, the syntax for C# for defines rules for grammatical arrangement of these keywords.

                 For example, the syntax of declaring a class in C# is:
    class <class name>
{
. . . . . .
}
                 In the preceding syntax, the braces, known as delimiters, are used to indicate the start and end of a class body.

Classes in C#
                 
                 C# classes are the primary building blocks of the language.  C# also provides certain predefined set of classes and methods.  These classes and methods provide various basic functionalities that you may want to implement in your application.

                 For example, if you want to perform some input/output operations in your application, you can use predefined classes available in the System.IO  namespace.  A namespace is a collection of classes.

                 In addition to using the predefined classes, you can also define your own classes.  Consider the following code snippet, which defines a class named Hello :
             
           public class Hello
{
                     public static void Main (string[] args)
                     {
                        System.Console.WriteLine("Hello, World! \n");
                        }
}
                  The preceding class declaration includes the method,  Main() that will display the message          " Hello World"  on your screen.  The following components in the preceding snippet build the complete class:
  • The Main() method
  • The class keyword
  • The class name
  • The System.Console.WriteLine() method
  • The escape character
           THE MAIN METHOD
               The first line in the code that C# compiler looks for int he compiled source file is the Main() method.  This method is the entry point for an application.  The Main() method is ideally used to create objects and invoke the methods of the class.
Note
      string[]args in the preceding snippet is an argument passed to the Main() method.  Arguments that are passed to methods will be explained later in the subsequent posts.

Note
       You must include a Main() method in the class to make it executable.

          THE CLASS NAME
The class keyword is used to declare a class.   In the preceding code snippet, the class keyword declares the class, Hello is the name of the class defined by using the class keyword.

Class Naming Conventions in C#

Class names should follow certain naming conventions or guidelines.  A class name:
  • Should be meaningful (Strongly recommended).
  • Should ideally be noun.
  • Can use either the Pascal case or Camel case.  In Pascal case, the first letter is capitalized and the rest of the letters are in lower case, such as Futureclass.  In Camel case, the first letter is in lower case and the first letter of each subsequent word is capitalized, such as futureClass or intFutureClass.
Rules For Naming Classes in C#

In addition to the conventions, there are certain rules that must be followed while naming classes in C#.  The name of classes:
  • Must begin with a letter.  This letter may be followed by a sequence of letters, digits(0-9), or '_'.  The first character in a class name cannot be a digit.
  • Must not contain an embedded space or a symbol like ?+-*/!@#$%^&*[]{}().:./';:".  However, an underscore('_') can be used wherever a space is required.
  • Must not use a keyword for a class name.  For example, you cannot declare a class called public. 

System.Console.WriteLine() Method

Console is a class that belongs to the System namespace.  The System namespace contains the Console class that includes a predefined WriteLine() method.  This method displays the enclosed text on the user's screen.  The Console class has various other methods that are used for various input/output operations.

The dot character(.) is used to access the WriteLine() method,  which is present in the console class of the System namespace.

Note

Referencing a method in this form is also known as referencing a method by using a fully qualified name.


The System.Console.WriteLine() statement can also be written as Console.Writeline() if the statement using System is included as the first line of code in the code snippet.
       The following code snippet is an example of Console.WriteLine() method:
     Console.WriteLine("Hello, World! \n");

The preceding code snippet will display the following message on the screen:

Hello, World!

Note

The WriteLine() method is used to write on the screen.  To read the input provided by the users,  you can use the ReadLine () method.  The method will be discussed later during the course.

No comments:

Post a Comment