15. C# - Structs
Structs hold values and can have constructors (a thing that takes input and changes default value of a struct).
Struct is value type, class is a reference type. They are somewhat similar except that difference.
Creating a basic struct:
struct Human {
public String Name;
public Int32 Age;
}
Initializing a human struct with a variable:
Human Todd;
Todd.Name = "Todd";
Todd.Age = 32;
Creating a struct with constructors:
struct Human {
public String Name;
public Int32 Age;
public Human(String N, Int32 A) {
Name = N;
Age = A;
}
}
Initializing a human struct with variable using constructors:
Human Todd = new Human("Todd",32);
Comments
Post a Comment