C#

namespace, struct, enum, property, indexer

Naranjito 2023. 2. 23. 20:52
  • namespace

Take the classes, the variables, the functions that someone has already created.

// using julia.Studio;
using julia; //In order to use the namespace, it needs to be imported

namespace julia
{
    public class Youtube //same class name
    {
        public int subscribe;
    }
    namespace Studio
    {
        public class Youtube //same class name
        {
            int like;

            public void SetLike(int value)
            {
                like=value;
            }

            public bool IsLike()
            {
                return like!=0;
            }
        }
    }
}

public class part: MonoBehaviour
{
    // Youtube julia=new Youtube();

    julia.Youtube julia; //Define which class you are going to use if there are same name of classes 

    void Start(){
        // julia.SetLike(5);
        // print(julia.IsLike());

        julia=new julia.Youtube();
        julia.subscribe=5;
        print(julia.subscribe);
    }    
}

>>> 5

 

  • struct
public struct Youtube
{
    public int a;
    public int b;
    public int c;

    public Youtube(int _a, int _b, int _c) //initiator
    {
        a=_a;
        b=_b;
        c=_c;
    }
}

public class part: MonoBehaviour
{
    Youtube julia=new Youtube(1,2,3); //the 'class' is processed internally at the same time as the declaration.
    Youtube julia2=new Youtube(4,5,6); //as soon as class is called, a, b, c can be replaced by the values of all parameters. because there is an initiator.    
}

 

  • enum

Can be chosen one of the listed.

public enum Item
{
    Weapon,
    Shield,
    Cannon,    
}

public class part: MonoBehaviour
{
    Item item;
    void Start(){
        item=Item.Shield;
        item=Item.Weapon; //Input the value.

        print(item); //The last value is output.
    }
}

>>>
Weapon

 

  • property

It provides a flexible mechanism to read, wirte, compute the value of a private field.

{get { } set { }}

first, get { } variable.

second, set value to { } variable. Must be used 'value'.

 

Ex 1. SalaryProperty can be accessed by other class.

private int salary;

private int bonus=10;

public int SalaryProperty {get {return salary;} set {salary=value;}}

 

Ex 2. SalaryProperty cannot be accessed by other class.

public int SalaryProperty {get {return salary;} private set {salary=value;}}

 

Ex 3. SalaryProperty has conditional.

public int SalaryProperty {get {return salary+bonus;} set {if (value<0) salary=10; else salary=value;}}

 

 

  • indexer

It allows you to create a class like that can allows its items to be accessed an array.

public class Record 
{
    public int[] temp = new int[5];

    public int this[int index] //'this' indicates 'class Record' and 'indexer'
    {
        get {if(index>=temp.Length)
        {
            Debug.Log("index is too big");
            return 0; //'return' is must

        }
        else
        {
            return temp[index];
        }
    }
        set {if(index>=temp.Length) Debug.Log("index is too big"); else temp[index]=value;} 
        //'print' is possible inherited from 'MonoBehaviour' only.
        //therefore it used 'Debug.Log' same as 'print' function.
    }  
}  

public class part : MonoBehaviour
{
    Record record=new Record();
 
    void Start(){
        record[3]=5;
        record[5]=5;

        print(record[3]);
        print(record[5]);
    }
}

>>>
5
index is too big
0