Hey, everybody.
In this video, we’ll be talking about structs.
A struct is a value type that you would usually or typically use to group variables that are
related together within it.
And typically, you also want to make your structs immutable.
Here is an example that I’ve created.
public struct Person {
public string Firstname { get; set; } public string Lastname { get; set; }
} static void Main(string[] args)
{ Person person = new Person();
person.Firstname = “Avetis”; person.Lastname = “Ghukasyan”;
ChangeFirstName(person);
Console.WriteLine(person.Firstname); Console.WriteLine(person.Lastname);
}
static void ChangeFirstName(Person person) {
person.Firstname = “John”; }
I have created a struct.
It has an access specifier.
In this case, it’s public.
And then you have struct reserved keyword and then whatever name you want.
This is the alias.
In this case, it’s the Person.
And then we have two properties inside.
Looks awfully lot like a class.
However, there is a major difference.
Structs are value types and classes are reference types.
And you already know about the difference.
But once again, I will remind you.
Reference type store their information in two segments.
One is in the stack and one is in the heap.
The actual data goes in the heap.
And in the stack they have the address that points to the actual information that is sitting
on the heap.
And for value types, it just goes on the stack.
And you also remember how they behave.
Remember the Boolean example a few tutorials back?
There I passed that into the method and we changed it there and it went back up to the
Main method.
It was the same as the one that was declared on top.
We declared it as true.
Then the method changed it to false.
And then it came back and it was still true.
Why?
Because it was a value type.
Here we have a similar example.
It looks like a class because structs and classes look pretty similar.
What do you think?
If we had a class here, we pushed it into the method ChangeFirstName(), it would change
the Firstname and came back up, the Firstname would be different.
But this is a struct and it’s a value type.
So what do you think is going to happen?
Will the Firstname change?
Let’s see.
Let’s run this.
The output is: Avetis
Ghukasyan So it didn’t change to “John”.
Why?
Because it’s a value type.
Now let’s change this struct to class.
public class Person {
public string Firstname { get; set; } public string Lastname { get; set; }
} static void Main(string[] args)
{ Person person = new Person();
person.Firstname = “Avetis”; person.Lastname = “Ghukasyan”;
ChangeFirstName(person);
Console.WriteLine(person.Firstname); Console.WriteLine(person.Lastname);
}
static void ChangeFirstName(Person person) {
person.Firstname = “John”; }
They’re so similar that all we have to change is just one keyword and it becomes a class.
Now the person object is going to be passed into ChangeFirstName() by reference because
this is a reference type.
Let’s try this now.
The output is: John
Ghukasyan Why?
Because the Firstname in the ChangeFirstName() method was the same as the Firstname in the
Main method.
Since the address was saved on the stack, person parameter was referencing the same
object.
You already know that.
I don’t need to explain that again.
Let’s change it to struct again.
I just wanted to show you the difference between the value type and the reference type in the
context of structs and classes.
Structs, just like classes, can also have constructors.
They can have constants, fields, properties, as you’ve already seen.
However, there are some differences as well.
For example, when it comes to inheritance, you can’t do it on structs, but you can
use inheritance with classes.
There are other differences as well.
But the major difference is by reference and by value.
And you’ve already seen and learned about that.
The other question would be why use structs.
As I mentioned before, you would use it for grouping variables that are related together
and try to make them immutable.
That’s the best practice for when using structs.
When using structs or when creating structs, you can create objects that behave like the
built-in value types.
So that’s another great thing.
And just to give you an example, DateTime is a built-in struct.
It’s a built-in value type.
You can create your own custom value types that behave like the built-in value types,
but they are your custom ones for whatever purpose you want, and try to make them immutable.
That’s the key word here, trying to make them immutable.
Remember that they are passed in by value because they are value types.
Even though they look like classes, they don’t necessarily behave like classes because classes
are reference types.
Remember that.
That’s it.
Please give it a thumbs up.
Don’t forget to subscribe.
And leave questions on the bottom, and I will get back to them as soon as possible.
Thank you for watching.