Create a class in C # (CSharp)
Introduction to class CSharp
With C # everything must be contained in a class. A field, a method must therefore belong to a class. This tutorial, simple and short, aims to explain the basic rules to create and use a class.
Explanations
Hello Word
C # program must include at least one class and the class must necessarily contain the static Main method which is the entry point of the program.
Here's how the program is written minimum "Hello Word" in a console application in C #:
Create and use a class
C # can obviously contain several classes. So if you delegate the message "Hello Word" Message to a class our program becomes:
In this example of the Main method does Class Program itself over the message but called the Display method of Class Message. To do this we must first create an instance of the object by Message: Message msg = new Message ();
Re: Create a class in C # (CSharp)
Create a file. Cs
Another method would be to create the class file. Cs specific.
In Visual Studio 2005 click on the Project menu / Add a class:
Naming the class:
Right in the solution explorer, a file message.c appears and Visual Studio automatically creates a vacuum that class is full of our code:
Re: Create a class in C # (CSharp)
- Give the class a meaningful name. This name will be used as the identifier for the class when creating instances of the class or creating new subclasses. Although it is not required, C# convention calls for capitalization of the first letter in a class name.
- Precede the class name with the "class" keyword. The class keyword lets the compiler know that you are defining a new class and that the following code block is not a function definition. For instance: "class Vehicle"
- Enclose the body of the class in curly braces. The body will include any data member declarations, class methods, or nested internal classes, as demonstrated in the following example:"class Vehicle{ // data members and method definitions go here }"
- Precede the class keyword in the header with one of seven modifier keywords if access to the class is required. Internal is the default access for a class in C#. If no modifier is used, a class has internal access. Internal classes are only accessible from other classes in the same assembly.