Post #6 Methods, procedures or encapsulation

Methods: Let me just give a base definition of a method. A method is a program that can take parameters and does something usually. The greatest of these is our main method which will always run when we run the program. The main method is also a built in method. You can have built in methods or you can build your own. Built in methods are super useful like the main method. It can also be good to build your own so that the method does what you want. Static Methods are methods that do not change. They don't normally allow methods that are not static to be called. However you can simply create a separate class. Instance Methods are methods that operate normally on a given instance of a class. Virtual Methods are when an instance method has an virtual modifier. That's the basic definition my books are giving me. Override Methods are when an instance method has an override modifier. Override Methods can override virtual methods. Sealed methods similar to virtual and override but when an instance method has a sealed modifier. Sealed methods prevent overrides. Abstract Methods are basically a way to create a method without actually stating what it does. That is really weird. Partial methods are when a method has a partial modifier. Extension Methods are when a method has an extension modifier. Method Body usually consists of a block or a semicolon. If this paragraph bores you to death feel free to comment below. Just kidding but we need to have some fun otherwise this is just another assignment. If you have comments or questions for methods, I will try my best to answer but I may not fully understand all of these methods.

Parameters: Parameters are something you input into a method. It can be almost any variable. Variables accepted are like int, Boolean, String, I think any array, etc. Value parameters are parameters that get created when you call a method. Reference parameters reference a variable so therefore as stated by the book I'm using a reference parameter does not create a new storage location but instead references the location of that variable. Output parameters are similar Reference Parameters but not entirely the same. They have a tricky definition so I'm going to just leave it at that. Array Parameters do exist and are just arrays. If you have questions, ask below.

Procedures and Encapsulation: Encapsulation is basically what methods do. Loops and ifs are also a part of it. Encapsulation is basically just capturing data. Procedures are set formal ways of doing things. Procedures for methods are actually simple. The generic way to create a method is basically saying (public or private), type, name of method, inside parenthesis can be parameters, and then curly braces. It's usually good practice to say public or private but is not required. Public allows other classes to access the method. Private only allows the class it's in to access it. Type is required. Name can be almost anything but you can't start with numbers or symbols. Inside the parenthesis is where Parameters are. Inside curly braces is where all the magic happens the method will do whatever you want it to. Order is important for creating a method because everything must be put as it is above. 

This is our program today. 
using System;

namespace Methods
{
    class Program
    {
        static void Main(string[] args){
            // creates a Container
            Container method = new Container();

            // adds two numbers
            Console.WriteLine(method.add(25));

            // says yes or no depending on true or false
            Console.WriteLine(method.yesNo(false));

            // multiplies two numbers
            Console.WriteLine(method.multiply(55));

            // tells you if your first number is completely divisible by your second 
number
            Console.WriteLine(method.mod(402));
        }
       
    }

    class Container{
        public int add(int aint b){
            int x = a + b;
            return x;
        }

        public String yesNo(Boolean tf){
            if(tf == true){
                return "yes";
            }
            return "no";
        }

        public int multiply(int aint b){
            int x = a * b;
            return x;
        }

        public Boolean mod(int aint b){
            if(a % b == 0){
                return true;
            }
            return false;
        }
    }
}
If you please notice the above, there's something mildly intriguing. This was all
taken from one program. If you haven't caught it 1 program in C# can have multiple
classes. This program uses two classes so that it can run methods without worrying
about them being static. First class is basically a driver in Java. I just had it run
the main method. Second is just another class but the class has 4 methods and code
for each individual method. The program creates a container so that it can access
methods in main. It then goes through the methods while printing the result. The
methods all have a comment saying what they do. I only used basic methods but as I
said there are more. Leave comments and questions if you need to.

This is the end of this blog post.

I used the book The C# Programming Language Fourth Edition By Anders Hejlsberg, Mads
Torgersen, Scott Wiltamuth, and Peter Golde for information in this post.

Resources: Same Resources

MICROSOFT VISUAL C# .NET by Mickey Williams
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
Visual Studio Code Environment:

Link to get started:

Comments

Popular posts from this blog

Post #11 Complete Arcade

Post #10 Arcade

Post #2 Hello World!