Viewing a single comment thread. View all comments

Triabolical_ t1_ixg4zlo wrote

class vs struct

Structs are designed for very limited scenarios.

For what are called "value types" - types that behave like numbers or strings. They behave in the ways that we expect numbers to behave.

When you want to use system functions that expect a very specific layout of data in memory. I Windows, this is typically system stuff that is written with C or C++.

Unless it's one of those cases, you shouldn't use structs. You should use classes.

method vs constructor

Consider this code (written from memory, so it might have some errors ):

class MyNumber
{
private _number;

public MyNumber (int number) // constructor
{
_number = number;
}

public void PrintNumber()
{
Console.WriteLine(_number);
}
}

This class stores and prints out a number.

When this class is used, we always want it to have a number associated with it rather than a default value. If we try to write this:

MyNumber number = new MyNumber();

the compiler will complain. We need to write:

MyNumber number = new MyNumber(1345);

That ensures that instances of the class are in a known state - that is why we want to use constructors.

In this example, PrintNumber() is a method. It's not special the way a constructor is.

Hope that helps.

​

​

​

Methods are chunks of code that are associated with a specific class. The important part of a method is that it has access to the private class variables associated with a method

2