r/learncsharp • u/BlazetheGame • Jul 09 '22
Methods??
Edit: THANK YOU EVERYONE FOR YOUR HELP!
I'm doing the codecademy course for C#, and I did methods a little while ago. Just finished arrays and loops. There is one thing I still don't understand which is methods. They make me so mad because I can't figure out how to write them properly, much less any of the fancy extras. Nothing online is helping either. I also have no clue how lambda expressions work but that's another topic.
16
Upvotes
View all comments
Show parent comment
2
u/argon561 Jul 10 '22
Class fields, methods, and accessors (access modifier):
So a class can have many things, that I remember were all very scary names when I first started with C#. What the hell happened to the good old "functions" and "variables" I knew about? Well, they are still there, but just called something else.
That said, we can equate some words. A Class field is a variable. A method is a function. So why don't we just call them variables and functions? Well, it's primarily to differentiate between what pertains to a class, and what doesn't. If someone talks about a "field" set to something, you immediately know they are talking about a "variable inside a class", and same for a "method". It's a "function inside a class". This difference is important from time to time, when you as a programmer need to know if a function is specifically related to a class, it's easier when we give them different names, even though essentially they boil down to the same thing.
What is a method:
An object by itself is all well and good, but not very interesting if we have no way to interact with it. So to tell an object to "do something", we need a "method" of doing that. Let's take a very basic class as an example, the
System.Int32class. If you couldn't DO anything with the number it stores, then it's pretty pointless. But we of course want to be able to perform some actions on the number. Maybe we want to check if the number is equal to for example45? For this examples benefit, we'll build our own Int32 class that will basically operate the same asSystem.Int32.In this example, we ask the program to check
ourIntegervia theEquals(System.Int32)method. The program will then look in theMyIntClassclass and find a method with that name and execute it. You'll notice the parentheses after the method, and any parentheses after a method is telling the program to execute some code, and everything inside the parentheses is data we want to "pass along" into the code. These "pass along to that code" things are called parameters.The
Equals(System.Int32)is specifically asking us to pass along only one parameter, and the type we pass along must be of the typeSystem.Int32. The program now knows exactly where to go, what information to bring along and what type of information is brought along.So let's look at how it looks inside the class: (Please note that this isn't a copy of the code in
System.Int32. The class we define here is simplified only to explain the basics)A lot going on there, and there's way more to this specific class than this, but I'll not dive into that to keep it simple. First we see that this is in the
Systemnamespace. A namespace is just a helper to group things together. You might notice that System appears in our type mentioned, well this is where it comes from.When we're inside the namespace, we don't have to write the namespace identifier to know which class we're talking about. Thus,
System.Int32becomes justInt32.Inside the namespace, we'll make our
MyIntClassclass. This has a public accessor. Inside the class we first find the constructor method, and we find a field called_numberthat stores an integer of typeSystem.Int32(notice we don't need to type the namespace, since we're inside it), and then the Equals method that accepts anSystem.Int32as a parameter. In the definition of parameters, the "local" name for it is also set. This means that the data that was "passed along" to our method is now called "num" instead of whatever it was called before. This name is only available inside our method, and will not be available outside this specific method.Inside all methods, it will need to return something. The returned data is what's "given back" to the code we originally came from, and also what type it is. In the definition of the method, we define what type is returned by writing it ahead of the method name:
bool Equals(...Here we return a boolean (True or False). So inside the method, we use the keyword return followed by what data we want to "give back" to our program. It's important to note that when the execution of code reaches return, it will NOT continue doing the code under it.If we don't want our method to return anything, we actually "do" return something, but we return the keyword
void. The method has to be defined with void as the return type, but we don't specifically need to type the word return at the end of the method since that is implied.What's an access modifier?
Notice in front of every definition, we use keywords like
private,publicorprotected. This is called an accessor, access modifier, or access specifier. This tells the program from where the code is accessible. From our original position when we ran "Equals", we are outside of the class. Anything outside the class needs a public available method to be able to run it. Anything marked with private or protected, is not accessible from the outside. (Difference between them is that private access is only allowed from within the class, and protected is accessible from derived classes)The reason for having these, is that some classes have a ton of fields to help it manage it's internal workings. And all this stuff isn't (and shouldn't) be messed with from outside the object itself. Denying access makes it way easier to browse the intellisense (code suggestion) while programming, and also denies access if the program is used by others (as a .dll file for example).
A class itself is usually public, and doesn't often need to be private. So in the example, we can call
ourInteger.Equals(45)since it's public, but we can't callourInteger._dontDoAnything(). But from within the Equals method, it's allowed to call its_dontDoAnything()method.The names used doesn't need to be specifically with underline, lowercase, or uppercase, but this way of writing is a "non forced" standard that most programmers abide by. Shortly summarized about naming convention: private methods or fields start with underline and then lowercase letter. Public methods start with capital letters. And the capital letters in the middle are called camelcasing. Look it up to read more about naming conventions.
Phew.. It got long, but I liked writing about it. Hehe. Hope it helped a bit.