Properties and methods for C # classes

Source: Internet
Author: User

This article reprinted from http://blog.csdn.net/tianyao9hen/article/details/50890827 thanks to original

Property

Setting properties for a class allows you to get information through get and set, so that you can set a private element.
General attributes start with capital letters (EP:NAME)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacestudy1_repeat{classProgram {Static voidMain (string[] args) {Classtext ct=NewClasstext ("Li Gang"); Console.WriteLine (Ct.            Name); Ct. Name="Liu Ming"; Console.WriteLine (Ct.            Name);        Console.ReadLine (); }    }    //Add a class    classClasstext {//Private Elements        PrivateString name;  PublicClasstext () {Console.WriteLine ("Ceshi"); }         PublicClasstext (String name) { This. Name =name; }        //Setting Properties//after you set the properties, you can call the name method         PublicString Name {Get{//return element, get property                returnname; }            Set {                //causes the name to get the corresponding element, set the property//Correspondence: Ct. Name = "Liu Ming";Name =value; }        }    }}
Method

Modifier return type method name (parameter list)
{
Method body;
}

If you write a return type, you must return a value that is the same as the return type with return.
If you do not write the return type, you will use void.

Static methods and Instance methods

Using the Static keyword

    • Static methods belong to the class itself and do not belong to any object. A static method can only access static members in a class and cannot manipulate specific objects. Therefore, this is not available in static methods. When a static method runs, the object does not necessarily exist.
    • Instance methods can use any member of the class. Instance methods can access static and instance members.

Instance variable:
int I
Static variables:
static int I
Example method:
void example () {}
static method:
static void Example () {}

Properties and methods for C # classes

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.