C #: from the basics to the medium level (Class, Struct, Array)
We seen how work Classes, here I simply make a little review.
To create a new class here just declare it below (or above) the Program class
we can create a filed inside Class, as name and lastName
In Unity we drag and drop various script into objects, here obviously we can’t drop everything in one script, even if we divide in various class because after few times is literally impossible understand what happen.
So we simply click with Alt + Enter over the name of Class
In the solution we now see Person class is add to our project
To use this class, we need to create it with new keyword.
And we can access to fields or methods if we have.
Nothing so different from Unity.
Sometimes we don’t need to create a full Class.
The alternative is a Struct
A struct can have fields and can have methods, so, what’s the difference?
Just to be clear, 99% of the time we probably we use the Classes and not Struct, but is good to know they exist, and the differencebetween them.
In my experience, and thanks to the people who taught me, struct are good for define a really small object, as ColorRgb. Or maybe a point in the screen, where we need a X point and Y point.
In other cases, use classes without problems.
I’m back to talk about struct when we talk about Reference type and Value type.
We seen the Array in Unity, the declaration and use is exactly the same here.
So I’m not waste too much time to talk about it now, but in the Collection section we will talk in deep about Arrays.
Last thing for this article are strings.
There are some good ways to manipulate string.
Most of the times at beginning we see this
We can obtain same result with these syntax
We can use string.Join to create for example, a list divided by a separator