C# Classes

C# Classes
C# Classes

C# classes are the primary building blocks of the language. C# classes also provide a 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.

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# Classes 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# classes. 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 the 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.

The System.Console.WriteLine() Method

The 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.

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!

Escape Sequences

To display special characters, such as the new line character or the backspace character, you need to include appropriate escape sequences in your code. An escape the sequence is a combination of characters consisting of a backslash ( \ ) followed by a letter or a combination of digits.

The following table lists some of the escape sequences provided in C#.

C# Classes
The Common Escape Sequences in C#

The following code snippet depicts the usage of the new line escape sequence:

Console.WriteLine(“Hello \n World”);

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


Please enter your comment!
Please enter your name here