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;
}
}
}