Thursday 1 September 2016

Classes in C#

 

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.

NOTES:-

"A namespace is a collection of classes. C# provides some predefined namespaces that contain classes, which provide commonly used functionality. To use the classes defined in these namespaces, you need to include the relevant namespace in your program.
You can also create your own namespaces that contain classes that you need to use across several programs."


In addition to using the predefined classes, you can also define your own classes. Consider the following code, 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 preceding code includes the following components:

  • The class keyword
  •  The class name
  •  The Main() method
  •  The System.Console.WriteLine() method
  •  Escape sequences

Let us discuss these components in detail.


The class Keyword


The class keyword is used to declare a class. In the preceding code, the class keyword declares the class, Hello.


The Class Name


The class keyword is followed by the name of the class. In the preceding code, 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 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                 or           intEmployeeDetails.

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 ? - + ! @ # % ^ &     *    ( ) [ ] { } . , ; : " ' */ and \. 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.

The Main() Method


  • The first line of code that a C# compiler looks for in the source file is the Main() method. This method is the entry point for an application. This means that the execution of code starts from the Main() method.
  •  The Main() method is ideally used to create objects and invoke the methods of various classes that constitute the program.
NOTES"-

"string[] args in the preceding code is an optional argument passed to the Main() method. Arguments that are passed to methods will be explained later in the
course"


"You must include a Main() method in a class to make your program executable."


The System.Console.WriteLine() Method


Console is a class that belongs to the System namespace. The Console class 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.

"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 the code.

The following code snippet is an example of the Console.WriteLine() method:

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

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

Hello, World!
"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."