A description of C # classes and examples

Source: Internet
Author: User
This article focuses on the declaration of C # classes in detail. Have a certain reference value, follow the small series below to see it

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

Access modifier class class name  {/  /class Member://Methods, properties, fields, events, delegates  //and nested classes go to here.  }

A class should include:

    • Class name

    • Members

    • Characteristics

A class can contain declarations for the following members:

    • constructor function

    • Destructors

    • Constant

    • Field

    • Method

    • Property

    • Indexer

    • Operator

    • Event

    • Commissioned

    • Class

    • Interface

    • Structure

Example:

The following example shows how to declare a class's fields, constructors, and methods. This example also shows how to instantiate an object and how to print the 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";}//Constructo  R:public Child (string name, int.) {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 A 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, years old. Child #2: Sally, ten years old. Child #3: N/A, 0 years old. */

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

Console.Write (child1.name);   Error

A private member of the class can be accessed from main only if child is a member of main.

The type declaration in the class does not use the access adornment defaults is considered private, so the data members in this example will be private if the keyword is removed.

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

Note:

Classes are single-inheritance in C #. In other words, a class can inherit only one base class. However, a class can implement more than one (or more) interface. The following table shows some examples of class inheritance and interface implementations:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.