Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is the different between this two script.

Discussion in 'Scripting' started by hizral, May 28, 2014.

  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there, I would like to know the different between this two script.

    The first one (First time seeing it. Is it the same as the second one, or there's a different)
    Code (csharp):
    1.  
    2. Transform gObject;
    3.  
    4. gObject= (Transform) Instantiate (floor, new Vector3 (iFloor, 0, iFloor2), Quaternion.identity);
    5.  
    The second one: (i've seen this alot and i'm using this as well)
    Code (csharp):
    1.  
    2. Transform gObject;
    3.  
    4. gObject= Instantiate (floor, new Vector3 (iFloor, 0, iFloor2), Quaternion.identity) as Transform;
    5.  
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    nothing.

    just a different style of casting


    note: not sure if underlying they are slightly different, but I expect they are not.

    in c# there is also an 'is' keyword, so it pairs up nicely with the second method

    Code (csharp):
    1.  
    2.  
    3. if(object is GameObject)
    4. {
    5.   (object as GameObject).stuff();
    6. }
    7.  
    8.  
     
  3. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Thanks JamesLeeNZ..now I understand.
     
  4. loveour

    loveour

    Joined:
    May 21, 2014
    Posts:
    2
    The second is recommend.
    for example,TypeA instanceA = (TypeA)InstanceB,if instanceB is not TypeA(can't be cast to TypeA),there will be an exception.the second ,"TypeA instanceA = InstanceB as TypeA",instanceA will be null.
    So,we can write code like this
    Code (csharp):
    1.  
    2. TypeA instanceA = instanceB as TypeA;
    3. if(instanceA != null)
    4. {
    5. do something;
    6. }
    7.  
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    What? Exceptions are good, they don't allow you to make incorrect casts. If you already know instanceB is not null, then you save an extra if check if you know what type it is. If you're in a position of not knowing if the object you are casting too is not the correct type, that's just sloppy programming.
     
  6. loveour

    loveour

    Joined:
    May 21, 2014
    Posts:
    2
    Exceptions are good just for some time.

    consider there's situation like this:
    TypeB TypeC inherate TypeA,you need to use TypeB or TypeC but there's instance of TypeA,we can write code like this:
    Code (csharp):
    1.  
    2. TypeB instanceB = instanceA as TypeB;
    3. if(instanceB != null)
    4. {}
    5. TypeC instanceC = instanceA as TypeC;
    6. if(instanceC != null)
    7. {}
    8.  
    Exceptions are good but not for every time.sometimes we just don't need exceptions.If we must be certain the type casting is valid, we can add else or use "is".exceptions are not needed.

    Code (csharp):
    1.  
    2. TypeB instanceB = instanceA as TypeB;
    3. if(instanceB != null)
    4. {}
    5. else
    6. {
    7. error report
    8. }
    9.  
    Bill Wagner wrote a book "Effective C#",Item 3: Item Prefer the is or as Operators to Casts