Classes in C#



Programming 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. Details 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.IOnamespace. 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!"); } } 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 classkeyword The class name The System.Console.WriteLine() method The Main() Method The first line of code that a C# compiler looks for in the 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. The class Keyword The classkeyword is used to declare a class. In the preceding code snippet, the classkeyword declares the class, Hello. The Class Name The classkeyword is followed by the name of the class. In the preceding code snippet, Hellois the name of the class defined by using the classkeyword. 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 myClassor 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

Sent from Windows Mail

Popular posts from this blog

ch11 review silberschatz operating systems concepts essentials 2nd ed