Objects, classes, and structures

Source: Internet
Author: User
An object is a programming structure with data, behavior, and identifier.
Object Data is contained in the fields, attributes, and events of the object. The object behavior is defined by the methods and interfaces of the object.

Objects have the following features:

  • C # uses all objects, including Windows Forms and controls.

  • Objects are instantiated. That is, objects are created from templates defined by classes and structures.

  • Objects use properties to obtain and modify information they contain.

  • Objects usually have methods and events that allow them to perform operations.

  • Visual Studio provides a tool for operating objects: Use You can change the properties of an object (such as a Windows form. Use Check the object content.

  • All C # objects are inherited from .

Class has the following features:

  • C # only supports single inheritance: A class can only be inherited from one base class.

  • A class can implement multiple interfaces.

  • Class definition can be split between different source files. Partial.

  • A static class is a sealing class that only contains static methods.

  • The class is defined using the clsss keyword.

    Class
    Public class person
    {
    // Field
    Public string name;

    // Constructor
    Public Person ()
    {
    Name = "unknown ";
    }

    // Method
    Public void setname (string newname)
    {
    Name = newname;
    }
    }
    Class testperson
    {
    Static void main ()
    {
    Person person1 = new person ();
    System. Console. writeline (person1.name );

    Person1.setname ("John Smith ");
    System. Console. writeline (person1.name );
    }
    }

     

The structure has the following features:

  • The structure is the value type, and the class is the reference type.

  • When a structure is passed to a method, the structure is passed by passing values, rather than being passed as a reference.

  • Unlike classes, the new operator can be used for structure instantiation.

  • The structure can declare constructors, but they must contain parameters.

  • A structure cannot inherit from another structure or class, and cannot be the base of a class. All structures are directly inherited fromSystem. valuetypeThe latter inherits fromSystem. Object.

  • Structure can implement interfaces.

  • It is incorrect to initialize the instance field in the structure.

  • The structure is defined by the struct keyword.

  • Although the static fields of the structure can be initialized, the structure instance field declaration still cannot use the Initial Value Setting item.

  • The default constructor (constructor without parameters) or destructor cannot be declared in the structure.

    Structure
    Public struct coords
    {
    Public int X, Y;

    Public coords (INT P1, int P2)
    {
    X = p1;
    Y = P2;
    }
    }

     

StructType suitable for representationPoint,RectangleAndColorAnd Other lightweight objects. Although a vertex can be expressed as a class, the structure is more effective in some cases.

 

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.