0

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?

  • You use classes to declare reference types and structures to declare value types. In order to understand how they differ, you need to search about how reference types are different from value types. Good luck! (and welcome to the club) – Shahryar Saljoughi Jun 02 '21 at 16:26
  • 2
    Its like ordering a pizza. You can talk about that actual pizza (even when it hasn't taken physical form), but you cant actually do anything with that pizza because until you have the physical pizza. An `instance` of pizza would be the physical pizza that exists, you can `reference` 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 . – hijinxbassist Jun 02 '21 at 16:31
  • @hijinxbassist's Pizza analogy works. When you order a "The Works" pizza, you are doing something like `var myPizza = new TheWorks(Size.Large)`. In this case, you are asking for a new instance of a TheWorks to be created, and `myPizza` 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 abstract `Pizza` class. Re Value vs Ref types... You might get something out of my answer to this question: https://stackoverflow.com/questions/65418824/object-passing-reference-vs-value. – Flydog57 Jun 02 '21 at 16:38
  • [C# MSDoc](https://docs.microsoft.com/dotnet/csharp) | [Level 0](https://w3schools.in/csharp-tutorial) | [Level 1](https://www.tutorialspoint.com/csharp/index.htm) | [Level 2](https://tutorialsteacher.com/csharp/csharp-tutorials) | [Level 3](https://guru99.com/c-sharp-tutorial.html) | [Level 4](https://geeksforgeeks.org/csharp-programming-language) | [Linq 1](https://www.tutorialspoint.com/linq/index.htm) | [Linq 2](https://www.webtrainingroom.com/linq) | Beginning Visual C# 2008 Programming | Professional C# 7 and .Net Core 2.0 | Professional Visual Studio 2015 (Vol I) and 2017 (Vol II) –  Jun 02 '21 at 18:38
  • Does this answer your question? [What happens in memory when creating objects](https://stackoverflow.com/questions/25163715/c-sharp-what-happens-in-memory-when-creating-objects) and [Is everything in .NET an object?](https://stackoverflow.com/questions/436211/is-everything-in-net-an-object/67609162#67609162) –  Jun 02 '21 at 18:47

1 Answers1

-1
Info APersonsAccount;

Creates a reference of type Info but the reference doesn't point to anything. If you try to access any member of this reference, you will get a NullReferenceException.

APersonsAccount = new Info();

Creates an instance of Info in the heap and assigned the reference APersonsAccount to that object in memory.

These two concepts can be combined into a single statement

Info APersonsAccount = new Info();

The reference itself is stored in the stack for the executing method where as the object in memory is stored in the heap as long as something is referencing it. Once all references to the object are removed (for example, we exit Main in your example), the garbage collector will eventually free up that memory.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • 1
    This is probably a stupid question, but what does the 'instance' of a class mean and how is it different from a reference? – CodingBeginner Jun 02 '21 at 16:26
  • 1
    An 'instance' is just an object in the heap (.NET's memory space) and can vary in size based on the members of the class and their values. The 'reference' is of constant size (64 bits for 64-bit OS) and has the address of the object in the heap. – Babak Naffas Jun 02 '21 at 16:32