Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created May 8, 2020 17:20
Show Gist options
  • Save openroomxyz/4ee08af6a2e07271dafb0e1185be6a30 to your computer and use it in GitHub Desktop.
Save openroomxyz/4ee08af6a2e07271dafb0e1185be6a30 to your computer and use it in GitHub Desktop.
Unity C# : Structs vs CLass
public class FooClass
{
public string name;
}
public struct FooStruct
{
public string name;
}
public static class Example
{
public static void B(FooClass f)
{
f.name = "I am inside object and out of the box!";
}
public static void G(FooStruct F)
{
F.name = "I am inside struct and i am out of the box!";
}
}
FooClass foo = new FooClass();
FooStruct fooStruct = new FooStruct();
fooStruct.name = "I am struct variable looks like i was unable to be overriden from the thing in box";
foo.name = "I am class variable looks like i was unable to be overriden from the thing in box";
Example.B(foo);
Example.G(fooStruct);
Debug.Log("Class = " + foo.name);
Debug.Log("Struct = " + fooStruct.name);
//We get
//Class = I am inside object and out of the box!
//Struct = I am struct variable looks like i was unable to be overriden from the thing in box
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment