km89 t1_ixdadxz wrote
A "class" is a group of related code, typically organized such that you can instantiate an "object". For example, you might have a Fruit class that contains all the methods and properties that a fruit can do (ripen, rot, color, etc).
A "method" is something that the object can do. As noted above, a Fruit class might have methods for ripening or rotting; these methods would contain the code necessary to manipulate the object to do those things.
A "property", which you didn't ask about but which is important, is something about the object--not something that it can do, but something about its current state. A Fruit's Color property (Fruit.Color, for example) could be set to red, or orange, or yellow, or whatever else.
Here's an example of how properties and methods can work together.
class Program{
public static void main(String[] args){
Fruit banana = new Fruit(); //This calls the constructor, which currently doesn't do anything.
banana.Color = "Green"; //Sets the Color property of the Fruit object to green.
banana.Ripen(); //Calls the Ripen method, which for the sake of argument here changes the color
Console.WriteLine(banana.Color); //Would write to the console the color of the fruit.
//For the sake of argument, assume the Ripen() method
//changes the color from green to yellow, so this would
//write out "Yellow" to the console.
}
}
When you first instantiate an object, you call that object's "constructor". A constructor is a special method that contains all the logic needed to set the object up initially, before you begin to do anything with it. This will get slightly more technical, but imagine this:
class Recipe{
private List<string> ingredients;
Recipe(){
ingredients = new List<string>();
}
}
Above, the Recipe class has a private List of ingredients (and it's declared with List<string> because the List class can be of any type, so you're telling the compiler that all of the stuff in this List will be of the string data type). It's private, because nothing should be able to directly access it. But, also notice how it's not actually instantiated. The constructor, which is the one method in this class, instantiates the List object. An actually useful class would also have methods for getting and setting the ingredients, but they're not listed here.
Constructors are occasionally paired with "destructors." A destructor is another special method that handles any logic the object needs to do before it goes away. If you don't manually declare one, the compiler will put a fake one in for you, so don't worry about these unless you have to.
Structs are different from classes. They share a lot of similarities, but ultimately they're more limited and less able to change. The big difference is that they're not really objects and thus shouldn't--or can't--be changed after creating them, and they can't be used to inherit from.
Here's a good link that has a rundown of the differences between structs and classes, which I'll link rather than just typing out the differences here.
>https://www.c-sharpcorner.com/blogs/difference-between-struct-and-class-in-c-sharp
Viewing a single comment thread. View all comments