Home  >  Article  >  Backend Development  >  Detailed explanation and examples of C# class declarations

Detailed explanation and examples of C# class declarations

黄舟
黄舟Original
2017-03-28 11:55:211799browse

This article mainly introduces the declaration of the C# class in detail. It has a certain reference value. Let’s take a look at it with the editor.

The class is declared using the keyword class, as shown in the following example:

访问修饰符 class 类名 
 { 
 //类成员:
 // Methods, properties, fields, events, delegates 
 // and nested classes go here. 
 }

A class should include:

  • Class name

  • Members

  • Features

A class may contain declarations of the following members:

Example:

The following example shows how to declare the fields, constructors and methods of a class. This example also illustrates how to instantiate objects and how to print instance data. In this example, two classes are declared, one is the Child class, which contains two private fields (name and age) and two public methods. The second class StringTest is used to contain Main.

class Child
 {
 private int age;
 private string name;
 // Default constructor:
 public Child()
 {
 name = "Lee";
 }
 // Constructor:
 public Child(string name, int age)
 {
 this.name = name;
 this.age = age;
 }
 // Printing method:
 public void PrintChild()
 {
 Console.WriteLine("{0}, {1} years old.", name, age);
 }
 }
 class StringTest
 {
 static void Main()
 {
 // Create objects by using the new operator:
 Child child1 = new Child("Craig", 11);
 Child child2 = new Child("Sally", 10);
 // Create an object using the default constructor:
 Child child3 = new Child();
 // Display results:
 Console.Write("Child #1: ");
 child1.PrintChild();
 Console.Write("Child #2: ");
 child2.PrintChild();
 Console.Write("Child #3: ");
 child3.PrintChild();
 }
 }
 /* Output:
 Child #1: Craig, 11 years old.
 Child #2: Sally, 10 years old.
 Child #3: N/A, 0 years old.
 */

Note: In the above example, the private fields (name and age) can only be accessed through the public methods of the Child class. For example, you cannot print the name of Child using the following statement in the Main method:

Console.Write(child1.name);   // Error

Private members of the class can be accessed from Main only if Child is a member of Main.

Type declarations in option classes default to private without access modifiers, so the data members in this example would be private if the keyword was removed.

Finally, note that by default, the age field is initialized to zero for objects created with the default constructor (child3).

Note:

classes are single inherited in c#. That is, a class can only inherit from one base class. However, a class can implement more than one (one or more) interfaces. The following table gives some examples of class inheritance and interface implementation:

Inheritance Example
None class ClassA { }
Single class DerivedClass: BaseClass { }
None, implements two interfaces class ImplClass: IFace1, IFace2 { }
Single, implement an interface class ImplDerivedClass: BaseClass, IFace1 { }

The above is the detailed content of Detailed explanation and examples of C# class declarations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn