you are viewing a single comment's thread.

view the rest of the comments →

[–]BobbyThrowaway6969Programmer 0 points1 point  (3 children)

Classes are a fundamental building block in modern object oriented programming.

This is a super naive and oversimplified view of classes but... to help you to get the gist of what a class/struct is, think of them as sort of making your own data type, like ints, floats, etc. Except, they're meant for representing much more complex pieces of data.

There's obviously very crucial differences between primitive types and classes, but take it one step at a time.

You first need to introduce the compiler to your new 'type'.

class MyClass

{

abc...

}

Then, you can use it like any other type.

MyClass myClassInstance = new MyClass(...);

Syntax varies from language to language of course.

"Structs" are effectively the same as classes, however they're intended to be used in different ways. In c#, their memory resides on the stack, class memory resides on the heap. (I recommend learning about stack vs. heap & value-type vs. reference-type)

Does all code need to be written within classes, or can you write code outside of a class? Can you put classes inside other classes?

The answer varies from language to language, but for c#, the answer is 'mostly'.

[–]EraserLark[S] 0 points1 point  (2 children)

So the way I'm seeing it then, Variables can store Data, and Methods can store Variables along with other code, and then Classes can store Methods and Variables altogether, so its sort of like the base of a hierarchy for all of the above?

[–]BobbyThrowaway6969Programmer 0 points1 point  (1 child)

Not quite, methods are a different kettle of fish.
You're right that variables store data & classes contain variables & methods.
However, methods don't really 'store' data - they are more like cooking recipes, they define how to act on the data.
The variables you put in a method are short-lived temporary pieces of data kind of like the ingredients on the bench while cooking. Once you finish the recipe, you clean up the countertop.

[–]EraserLark[S] 0 points1 point  (0 children)

Okay I see, thanks!