So I started learning Classes in C#, and can't understand what classes exactly are and how they differ from structures (I know that they are reference and value types resp. but I'm not exactly sure what these types are).
I have the following code:
class Info
{
public string Name;
public int Age;
}
class Program
{
static void Main()
{
Info APersonsAccount;
APersonsAccount = new Info();
APersonsAccount.Name = "NameOfPerson";
APersonsAccount.Age = 23;
Console.Write("Name: {0,-15} Age: {1,-20}", APersonsAccount.Name, APersonsAccount.Age);
}
}
What I don't understand is how the code works. More precisely, what these lines mean-
Info APersonsAccount;
APersonsAccount = new Info();
The variable of the class Info is declared in the first line so why do we need to write the second line? What exactly happens at each step when this code is executed?
instance
of pizza would be the physical pizza that exists, you canreference
that pizza anywhere such as talking with your friends about that pizza, they know its that same pizza because that is the thing you are referring to .var myPizza = new TheWorks(Size.Large)
. In this case, you are asking for a new instance of a TheWorks to be created, andmyPizza
is a reference to the one that will be delivered in 30 min or less. In this case,TheWorks
is likely a sub-class of the abstractPizza
class. Re Value vs Ref types... You might get something out of my answer to this question: stackoverflow.com/questions/65418824/….