C #: class and Structure

Source: Internet
Author: User

1. Class and structure:

Class defines what data and functions each class Object (called an instance) can contain.

Relationship between structure and class:

  • The difference is that they are stored in the memory (classes are the reference types stored on the heap, while the structured values are stored on the stack), access methods and some features (for example, the structure does not support inheritance)
  • In syntax, the structure is declared with the keyword struct, and the class uses the keyword class.
  • The instance is declared with the keyword new.

2. class Members: Data (fields, constants, events) and functions in the class (some functions of data in the operation class are provided, including methods, attributes, constructors and terminators, operators, and indexers)

Public: You can directly access them outside the class.

PRIVATE: can only be accessed by other code in the class.

Protected: indicates that a member can only be accessed by its class and its derived class.

  • Data Member: static data (associated with the entire class), instance data (each instance of the class has its own data copy)
  • Constructor: A function automatically called when an object is instantiated. They must have the same name as their classes and cannot have a return type.

Using system;

Namespace consoleapplication3

{

Class Program

{

Staticvoid main (string [] ARGs)

{

// Calling some static functions

Console. writeline ("Pi is" + mathtest. getpi ());

Int x = mathtest. getsquareof (5 );

Console. writeline ("square of 5 is" + x );

 

// Calling non-static methods

Mathtest math = newmathtest ();

Math. value = 30;

Console. writeline ("Value Field of math variable contains" + math. value );

Console. writeline ("square of 30 is" + math. getsquare ());

}

}

Class mathtest

{

Public int value;

Public int getsquare ()

{

Return Value * value;

}

Public static int getsquareof (int x)

{

Return x * X;

}

Public static double

Getpi ()

{

Return 3.14159;

}

}

}

 

 

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.