Web.com Web Hosting
Sign up for PayPal and start accepting credit card payments instantly.


Hello If you are new here, so you might want to subscribe for my feed so Subscribe in a reader. Or bring updates In Your Email

Tuesday, October 26, 2010

How To Declare Classes in C#, Tutorial,

In previous article we discussed What is Compilers? . Now we will Ho To Declare Classes In C#.

Consider the following code example, which defines a class :

public class Hello
public static void Main(string [ ] args)


System.Console.WriteLine ("Hello, World! \n");



The preceding class declaration provides a method Main ( ) that will display the message "Hello, World!" on your screen. The parts of the preceding code need to be examined.

The Main ( ) Function

The first line of code that a C# compiler looks for in the source file compiled is the Main ( ) function. This function is the entry point of the application. The Main ( ) function is ideally used to create objects and invoke member functions.

The class Keyword

The class keyword is used to declare a class. Keywords are reserved words that have a special meaning. Here, the class keyword defines the class Hello. The braces, known as delimiters, are used to indicate
the start and end of a class body.

Example :
class Hello




The Class Name

The class keyword is followed by the name of the class. In the preceding example, Hello is the name of the class defined by using the class keyword. When you create classes, you must consider the following naming conventions and rules.

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 a 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 Myclass. In Camel case the first letter is in lower case and the first letter of each subsequent word is capitalized, such as myClass.

Rules for Naming Classes in C#

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 any embedded space or symbol like ? - + ! @ # % ^ & * ( ) [ ] { } . , ; : " ' / and \. However, an underscore ('_') can be used wherever a space is required.
  • Must not use a keyword for class name. For example, you cannot declare a class called public.

System. Console. WriteLine( )

Console is a class that belongs to the System namespace. A namespace is a collection of classes. The System namespace contains the method WriteLine ( ), which displays the enclosed text on the screen. The Console class has other methods, which are used for various input/output operations. The character (.) is used to access the function, WriteLine(), which is coded in the Console class of the System namespace.
The preceding line can also be written as Console.WriteLine( ) if the statement using System is included as the first line of code.

The following code is an example of

Console.WriteLine( )

Console.WriteLine ("Hello World \n");

The preceding code will display on the screen.

Hello World

The Escape Characters

To display special characters such as the New line character or the backspace character, you need to use escape characters.

the following table lists the escape characters used in C#.

Example of the New line escape sequence

Console.WriteLine(""Hello \n World");

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


Image Credit :- Astahost

No comments:

Post a Comment

Related Posts with Thumbnails