C#forunity Quick Start (serialized)-c# Properties

Source: Internet
Author: User

C # for Unity programming Language Quick Start Tutorial (serialized) _c# Properties


The "attribute" of C # is a constraint control method of a class field, whose direct purpose is to control the input legitimacy of the field and to implement quick access to the field.


Purpose
The concept of attribute is introduced to control the accessibility of the field more rationally.

Rule 1: A property is a simplified version of a method, primarily for controlling the field.
Rule 2: The nature of the attribute is the simplification of the set and get methods.
Rule 3: Properties can be written as read-only, write-only properties.
Rule 4: If you do not need to control the field in the attribute, you can simplify the notation.


Because this concept is relatively simple, so directly write the following demo code for C # Beginners (C # small white) for learning reference.

Class Program
{
private string _name;
private int _age;
private bool _gender;
Simplified notation of attributes
public string Address {set; get;}


General attribute notation
public string Name
{
get {return _name;}
set {_name = value;}
}
public bool Gender
{
get {return _gender;}
set {_gender = value;}
}

Properties with Field constraint control
public int Age
{
get {return _age;}
set {
if (value>0 && value<=120)
{
_age = value;
}
}
}

<summary>
Display attribute values
</summary>
public void DisplayInfo ()
{
Console.WriteLine ("Name: {0}, Age: {1}, Gender: {2}", Name,age,gender);
Console.WriteLine ("Address:" +address);
}

<summary>
Method Test
</summary>
<param name= "args" ></param>
static void Main (string[] args)
{
Program obj = new program ();
Obj. Name = "Little Tiger";
Obj. age = 180;
Obj. Gender=true;
Obj. Address = "Beijing, China";
Obj. DisplayInfo ();
Console.ReadLine ();
}
}

This article is from "Teacher Liu Speak Unity" blog, please be sure to keep this source http://liuguozhu.blog.51cto.com/9142031/1834248

C#forunity Quick Start (serialized)-c# Properties

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.