C#

Parse, void, private, static, operation, conditional, Iteration, foreach

Naranjito 2023. 2. 10. 20:38
  • Parse

int.Parse(a) : Change type a to int by force.

    int a;
    string b="100";

    void Start()
    {
        a=int.Parse(b);
        print(a);        
    }
    
    >>>
    100

 

  • void

1. Default value must be at last in the function.

    void FloatToInt(string _stringParm="default must be at last", float _parameter, float _parameter2)

    {
        intValue=(int)(_parameter+_parameter2);
        print(intValue);
        print(_stringParm);
    }


    void Start()
    {
        FloatToInt(floatValue, floatValue2);    
    }

   >>>
   error CS1737: Optional parameters must appear after all required parameters

To fix it, put the optional parameter at last.

    void FloatToInt(float _parameter, float _parameter2, string _stringParm="default must be at last")

    {
        intValue=(int)(_parameter+_parameter2);
        print(intValue);
        print(_stringParm);
    }


    void Start()
    {
        FloatToInt(floatValue, floatValue2);    
    }
    
    >>>
    default must be at last

 

2. return

    int intValue;
    float floatValue=10.5f;
    float floatValue2=20.5f;
     
    
    int FloatToInt(float _parameter, float _parameter2)

    {		     
        # third operate	    # first operate, 31
        return Multiply((int)(_parameter + _parameter2));
    }

	
    int Multiply(int _parameter)
    {
    	# second operate, receive the value from above, 31 * 31
        return _parameter * _parameter;
    }


    void Start()
    {
        print(FloatToInt(floatValue, floatValue2));    
    }

 

  • private
  • static
public class part: MonoBehaviour
{
    private int a;
    public int b;
    public static int c; //public resource

    public void Abc()
    {

    }

    private void Abc2()
    {

    }

public class Test: MonoBehaviour{
    part aaa = new part();
    part bbb = new part();
    part ccc = new part();

    void Start()
    {
        AAA();
    }

    void AAA()
    {
        // aaa.a=5; //private int a CANNOT be called
        aaa.b=5;
        aaa.Abc();
        // aaa.Abc2(); //private void Abc2 CANNOT be called

        aaa.b=1;
        bbb.b=2;
        ccc.b=3;

        part.c=100;

        print(aaa.b); //public int b can be changeable
        print(bbb.b); //public int b can be changeable
        print(ccc.b); //public int b can be changeable
        print(part.c); //public static int c CAN be called DIRECTLY VIA CLASS       
    }
}
}

 

  • operation
    int a=0;
    int b=0;

    void Start()
    {
        print(++a); //first operate, and then show the result
        print(b++); //first show the value before operate, later operate
        print(b);
    }
    
    >>>
    1
    0
    1

 

  • conditional

conditional ? true : false;

public class part: MonoBehaviour
{
    int a=0;
    int b=0;

    void Start()
    {
        int temp=a==b ? 10 : 100;
        print(temp);
    }
}

>>>
10

 

  • Iteration
int num=0;

    void Start()
    {
        for(num=0; num<=8; num+=2)
        {
            print(num);
        }
    }
    
    >>>
    0
    2
    4
    6
    8

is same as below.

int num=0;

    void Start()
    {
        for(;;)
        {
            if (num>=10)
                break;
            print(num);
            num+=2;
        }
    }
    
    >>>
    0
    2
    4
    6
    8

 

  • foreach
string text="abcd";

    void Start()
    {
        foreach(char i in text)
        {
            print(i);
        }
    }
    
 >>>
 a
 b
 c
 d