About unity in C # Basic Learning

Source: Internet
Author: User
Tags modifiers

First, the program contains

1. Data: Generated during the operation

2. Code: Code directive

Both the data and the code are stored in memory, and the code instruction is put into memory when the program is loaded, the data is dynamically generated in memory when the program is running, and is recycled at any time, and variables are defined to hold the data.

The smallest unit of memory storage is bytes, in memory is a binary number, only 1 or 0, called bits, 1 bytes can hold 8 bit,8 bits binary 11111111

II. basic data types for C #

1. Integer (sbyte 1-byte integer with symbol, byte unsigned 1-byte integer) (short signed 2-byte integer/ushort unsigned 2-byte integer) (int with signed 4-byte integer/unit unsigned 4-byte integer) (zero). (Long signed 8-byte integer/ulong 8-byte integer without a symbol)

2. Floating-point number (float 32bit=4 byte/doouble 64bit=8 bytes)

3. Logic number (TRUE/FALSE)

4. Character type (16-bit Unicode)

5. A reference variable of a complex type (pointer in c), with a 100-byte variable A and variable b pointing to variable A, then B is called a reference variable of a complex type (64-bit. NET then 64-bit, 32-bit. NET then 32-bit)

Like Monobehaviour is a complex type, string-defined strings are also complex types

Third, C # permission modifiers

1.public class and modifiers for type members

Modifiers for 2.private type members

Modifiers for 3.protected type members

4.internal class and modifiers for type members

A type member consists of a data member (an instance of a class) and a method of a class (a function member), a data member that does not belong to a class, an instance of a class, how many data members are defined in a class, and how many data member properties are in an instance of the class. The method of the class is logical, generic, belongs to the class, the code exists in a fixed place, only a single piece of logic, no matter how many instances are called this logic

Human

Data members: mouth, teeth, tongue

Class method: Eat (call mouth, tooth, tongue swallow meal)

Iv. definition of classes in C #

permissions [public external can use class, internal only internal use class] +class + class name

public class Gamescene {

}

A component is also a class, and a class is a complex data type

V. Definition of data members in C #

permissions [public external can be accessed directly, private external (outside the curly braces that define its class) cannot be accessed directly, protected inherited classes can be accessed, internal almost useless]+ type + name of data member

private int age;

private string name;

private int sex;

Vi. definition of methods for C # classes

Permissions + return value type + Name of the method of the class

public int Setage (int.) {

Logic

}

Input raw material, produce milk

Vii. examples of classes in C #

1. Data members (each instance is assigned a set of data members and occupies their own memory, not the same, the group of data members of an instance is packaged as a complex data object, the reference variable points to a complex data type of memory, using reference variables to access memory)

2. Common logic (common, only one, put in memory, black box, input what output what)

MyPerson person; The person here is not an instance of the class, just a reference variable that defines a class instantiation that must be created with the New keyword

Person=new MyPerson (); Now is the instantiation of the class, and the person reference variable points to the instance

You can use an instance of a class to reference a variable to access the instance's data member + call the class's function method

person.age=10;

Person.init_person ("Xiaohong");

Viii. definition of variables in C #

1. Define a member variable (data member) of a class

private int age;

2. Define a local variable (a variable defined inside a function that is valid when the code executes to the function, invalid when leaving the function, memory recycle)

void Start () {

int A;

}

Ix. This object for C #

The function of a class is like a box that enters something into it, executes the logic inside the box, and then outputs or does not output anything.

A method of a known class occupies only a single memory in memory, and all instances of the class are functions that invoke a common class, so how do you know which instance is calling this common function and how to know which instance of the data member is being processed when the logic is being processed?

A This object is required at this time

MyPerson Xiaohong;

Xiaohong=new MyPerson ();

Xiaohong.init_person ("Xiaohong"); Point the This object to the memory that the Xiaohong reference variable points to, specifying that instance together

MyPerson xiaoming;

Xiaoming=new MyPerson ();

Xiaoming.init_person ("Xiaohong");//Point the This object to the memory pointed to by the Xiaoming reference variable, specifying that instance together

function definitions in the MyPerson class

public void Init_person (int age,string name) {

This.age=age; Xiaohong.age=age

This.name=name; Xiaohong.name=name

}

The This object, which is equivalent to passing a variable in the function arguments, (int age,string Name,myperson this), when called Is Xiaohong.init_person ("Xiaohong", Xiaohong);

Through this access to the data of the instance of the specified class within the function, the data member is also accessible by private permission, because the function is inside the class.

X. Recycling of instances

In the function

MyPerson xiaoming;

Xiaoming=new MyPerson ();

MyPerson person=xiaoming;//equals the definition of a person reference variable, also point to the Xiaoming instance, when two reference variables point to the same instance

When the function execution is finished, xiaoming and person are released, equal to now no reference variable to the Xiaoming instance, this instance will be automatically reclaimed by the garbage collector, the original instance object occupied by the memory is also recycled, the programmer is not collected by the tube

Xi. memory model

Generated when the program runs, depending on whether a variable or object is encountered in the execution, whether it is placed in a stack or in heap memory

Stack: Store local variables, recycle after function return

Heap: A complex object that stores new, a data member package, and no reference variable points to the instance memory when it is recycled

Load it into memory, when you click on the exe file

Data segment: Store data (global variables, static data). Habitual memory, never recycled, unless the programmer himself deletes

Code Snippets: Store function instructions, each function has a unique instruction stored in the code snippet, Init_person (), all instances common, one strip execution. Habitual memory, never recycled, unless the programmer himself deletes

About unity in C # Basic Learning

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.