C # basic (class)

Source: Internet
Author: User
Class:
    1. Class is blueprint, used to describeWhat kinds of data the object holds and works, What the object can do what its functionality is
    2. Usually, your C # programs will define theirOwn classes, As well as use the classes that are provided by. NET Framework
    3. Class define two major things (Fields and Properties&Methords)
    4. Class can inherit attribute and methords from other clas
    5. Define class:
Class Myclass{// Fields to contain the class dataIntMyinteger;StringMymessage;// Methords that define the class functionalityPublic IntMyfunction (){ReturnMyinteger;// Whatever other logic there is}PublicMyclass (StringDefaultmsg) {mymessage = defaultmsg;// This is a constructor, which is generally used to assign values to fileds.}}

6: use Class: UseInstance. Method

Use new to create a class instance

 
MyclassMyobj =New Myclass("Hello");

7: use fields and methods of the class "."

 
IntMyresult = myobj. addnumber (5, 10 );

8: static: UseClass Name. Method

Console.Writeline ("Hello World ");

9: instance Member vs static member:

 1  Namespace  Definingaclass  2   {  3           Class  Myclass {  4               Int  Myintegar;  5               String  Memessage;  6               Public   Static   Int Mystaticint =100 ; //  Static variable  7   8               Public   Int  Myfunction (){//Instance MemberFunction  9                   Return  Myintegar;  10   }  11   12               Public Myclass (){ //  Constructor use to initial fields  13 Myintegar = 50  ;  14 Memessage = "  This is made from Constructor  "  15   }  16   }  17           Class Program  18   {  19                   Static   Void Main ( String  [] ARGs)  20   {  21 Myclass ACC = New Myclass (); //  Call Constructor  22 Console. writeline ( "  Calling instance member function: {0}  " , ACC. myfunction ()); //  Use instance Member  23 Console. writeline ( "  Calling static variable: {0}  " , Myclass. mystaticint ); //  Use static member  24  Console. Readline ();  25   }  26   }  27 }

10: access modifier:

PRIVATE: class itself only

Public: can be accessed by other objects

Protected: class itself and integrated subclass access

Internal: can be accessed by any class in the same assembly (package of code like a library or other program)

Eg:

 

Wine. CS  Namespace Accessmodifier //  Note that the created wine class is placed under the NS with the project name.  {  Class  Wine {  Public   String  Name;  Public   Decimal  Price;  Private   String Description;  Protected   Decimal  Dicount;  Public Wine ( String Winename, Decimal  Wineprice) {name = Winename; Price = Wineprice; dicount = 0.0 m  ;}}} 
  program. CS   namespace   accessmodifier {  class   program {  static   void  main ( string   [] ARGs) {wine mywine  =  New  wine ("   Jacky  " ,  100   ); mywine. name  =  "  zxx  "  ; mywine. price  =  200 m ;}} 

 

Property:
    • Property is like field, but he still has the logic behind it.
    • Externally, they are like member variables, but they are like member functions.
    • The definition is like field, which contains get, set,In get, return the value to the user who calls the property. Set uses a value to set a private variable. getter and setter are actually two abbreviated methods.
    • Property is a typical use of puiblic. Other types include methord and event. Other types are generally private, sealed, and protected.
    • Property can be read-only or write-only

Benefits

    • For people who use property, logical encapsulation is implemented. users do not have to know what logic is implemented inside the property. They only need to use it.
    • The internal logic of the property changes, and the external usage remains unchanged.
    • Property get, set can have logic in it to determine the value.

For example:

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.