C#forunity Quick Start (serial)-c# structure

Source: Internet
Author: User

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


C # 's "struct" is defined using the struct keyword, which is a programming unit that is parallel to "class" and "interface ." The main purpose of the design is to customize the value type, which allows the user to customize the value type.


Scope of application:
Structs are suitable for small data structures that contain data that is not modified after creation. Also suitable for data packaging, defining multiple variables at once.


There are many restrictions on the structure.
1> cannot be initialized unless the field is declared const or static.
The 2> struct does not have a default constructor.
A 3> struct can declare a constructor with parameters.
The 4> structure cannot inherit.
The 5> structure is copied at the time of assignment.
6> struct instantiation can not use the new operator.
The 7> structure can implement interfaces.

8> can be new or not new when used in a structural body. If not new, you must assign values to all member variables in the struct before use.


For C # Beginners, the structure is not a good distinction, and now the main differences between the structure and the class are summarized as follows:

1> If you need to use object-oriented features, you must use a class.

2> member variables are a lot of times, you must use classes.

The biggest difference between a 3> class and a struct is that the class is a reference type, so the instance object of the class is a reference pass value, and the struct type is a value type, so the struct instance object is passed value by value type.


In order to better illustrate the similarities and differences between the class and structure, we now provide the following code for your study and discussion:

Class Program
{

public void changevalues_1 (int intnum)
{
Intnum = 100;
}

<summary>
Test class object passing value (reference)
</summary>
<param name= "PC" ></param>
public void changevalues_2 (Pointclass pc)
{
Pc. Pointx = 1000;
Pc. Pointy = 1100;
}

<summary>
Test structural Body Transfer value (copy)
</summary>
<param name= "PS" ></param>
public void Changevalues_3 (pointstruct PS)
{
Ps. Pointx = 2000;
Ps. Pointy = 2200;
}


static void Main (string[] args)
{
/* Elicit a learning case (the "point" of the system) */
Point firstpoint = new Point ();
Firstpoint.x = 10;
Firstpoint.y = 20;

/* class vs. struct Test */
Reference type
Pointclass pc = new Pointclass (10,20);
Value type
Pointstruct PS = new pointstruct (50,60); Notation 1
Notation 2: Pay special attention to the use of methods or properties in the structure before
You must assign values explicitly to the fields of the struct, otherwise you must use new.
Pointstruct PS2;
Ps2._pointx = 23;
Ps2._pointy = 88;


Program obj = new program ();
int inttestnum = 50;
Obj. Changevalues_1 (Inttestnum);
Console.WriteLine ("number=" + inttestnum);

The results show that the reference type, the change in the method, and the effect on the result
Obj. Changevalues_2 (PC);

Console.WriteLine ("Pc.pointx={0},pointy={1}", PC. Pointx,pc. Pointy);

Obj. Changevalues_3 (PS);
Console.WriteLine ("Ps.pointx={0},pointy={1}", PS. Pointx, PS. Pointy);
}
}

The class defined
Class Pointclass
{
private int _pointx;
private int _pointy;

public int Pointx
{
get {return _pointx;}
set {_pointx = value;}
}
public int Pointy
{
get {return _pointy;}
set {_pointy = value;}
}

public pointclass (int px, int py)
{
_pointx = px;
_pointy = py;
}
}

Defined structure body
struct POINTSTRUCT
{
public int _pointy;
public int _pointx;


public int Pointx
{
get {return _pointx;}
set {_pointx = value;}
}
public int Pointy
{
get {return _pointy;}
set {_pointy = value;}
}

public pointstruct (int px, int py)
{
_pointx = px;
_pointy = py;
}
}




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

C#forunity Quick Start (serial)-c# structure

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.