C# language: 8. Methods

in #programming7 years ago

CC.png

In the previous lesson, we learned how to encapsulate our code, but we do not know when we should add in our cake from previous lesson those components! That’s why today we will learn the function or the so-called methods, you know how look “Main” is a function.

Methods

Method is the same as the function only the function inside the class is called method.

Each method has the following scheme:

access specifier, return type,  method name(argument1, argument2)
{
     // method body - everything is done here
}

Let’s write it in the form of a code:

public void Example(int number)
{
    Console.WriteLine(number);
}

The above method has public access, does not return anything, is called Example and accepts only one argument, if we will call this method will execute the code in the clamps, the name of the method does not matter We can call it ourselves as we want, the arguments we can also call as we want.

According to the rules of code purity, the function can take up to three arguments, ideally if there are no arguments at all, because it is also possible, of course the function can take a lot more arguments than three, but in terms of code readability it will not be good.

Let’s create in our Cake class this method:

public void PourSalt()
{
    Console.WriteLine("Pour " + _AmoutOfSalt + "g salt");
}

And calling this method in the Main function in this way:

cake.PourSalt();

After entering the quantity of ingredients, and description of the product, we will see the text that we typed in the body of the function, this is how the function is used, I will show how to use a function that returns a type, example below:

Let’s put something like that in the Cake class and in the Main function:

//Main function
Console.WriteLine("Change the baking time of the dough");
 
string time = cake.BakingCake(cake.BakingTime = int.Parse(Console.ReadLine()));
Console.WriteLine(time);
            
//Cake class
             
public string BakingCake(int time)
{
    _bakingtime = time;
    string text = "I change the baking time dough on " + time.ToString() + "min";
    return text;
}

I think that there is nothing difficult here.

First, in the Main function, we introduce the baking time manually and call the BakingCake method which accepts our entered value, in the body of the function we change the value of the private field _bakingtime to the value we introduced, ie the variable time.

And by the way you have learned that private fields can also be changed using methods, java programmers do this because in java there are no accessors get and set

Then we create the variable text in return it in this function, and we go back to the function Main and we attribute the returned value to the variable time and display this variable on the screen, it is not too difficult.

Static and instant methods

So that we could call methods in the Cake class, we first had to create a cake object and then enter the name of the object and after the dot the name of the function, right?

However, you can do it in a different, easier way as long as the method is static. Static methods are created like this:

public static void PourSalt()
{
    Console.WriteLine("Pour " + _AmoutOfSalt + "g salt");
}

Also, the _HowMuchFlour variable must be changed to static:

private static int _HowMuchFlour;

And calling her looks like this:

Cake.PourFlour();

The difference is that you do not have to create a class object.

And that’s all, it is simple, right?

The whole Cake class code and Main function looks like this:

class Cake
{
    private double _AmountOfSalt;
    public double AmountOfSalt
    {
        get
        {
            if (_AmountOfSalt < 0 || _AmountOfSalt > 50)
                return 0;
            else
                return _AmountOfSalt;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You gave the wrong amount of salt!");
            else
                _AmountOfSalt = value;
        }
    }
 
    private int _BakingTime;
    public int BakingTime
    {
        get
        {
            if (_BakingTime < 0 || _BakingTime > 100)
                return 0;
            else
                return _BakingTime;
        }
        set
        {
            if (value < 0 || value > 100)
                Console.WriteLine("You have entered the wrong baking time!");
            else
                _BakingTime = value;
        }
    }
 
    private int _NumberOfEggs;
    public int NumberOfEggs
    {
        get
        {
            if (_NumberOfEggs< 0 || _NumberOfEggs> 50)
                return 0;
            else
                return _NumberOfEggs;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You have given the wrong amount of eggs!");
            else
                _NumberOfEggs = value;
        }
    }
 
    private int _HowMuchMilk;
    public int HowMuchMilk
    {
        get
        {
            if (HowMuchMilk< 0 || HowMuchMilk> 50)
                return 0;
            else
                return HowMuchMilk;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You gave the wrong amount of milk!");
            else
                HowMuchMilk = value;
        }
    }
 
    private static int _HowMuchFlour;
    public int HowMuchFlour
    {
        get
        {
            if (_HowMuchFlour< 0 || _HowMuchFlour> 50)
                return 0;
            else
                return _HowMuchFlour;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You have given the wrong amount of flour!");
            else
                _HowMuchFlour = value;
        }
    }
 
    public void PourSalt()
    {
        Console.WriteLine("Pour " + _AmoutOfSalt + "g salt");
    }
    public string BakingCake(int time) 
    { 
       _bakingtime = time; 
       string text = "I change the baking time dough on " + time.ToString() + "min"; 
       return text;
    }
    public static void PourFlour()
    {
        Console.WriteLine("Pour " + _HowMauchFlour + "g Flour");
    }
}
 
// Main function
 
static void Main(string[] args)
{
    Cake cake = new Cake();
    Console.WriteLine("Enter the amount of salt");
    cake.AmountOfSalt= double.Parse(Console.ReadLine());
 
    Console.WriteLine("Enter the baking time");
    cake.BakingTime = int.Parse(Console.ReadLine());
 
    Console.WriteLine("Enter the amount of eggs");
    cake.NumberOfEggs = int.Parse(Console.ReadLine());
 
    Console.WriteLine("Enter the amount of liters of milk");
    cake.HowMuchMilk = int.Parse(Console.ReadLine());
 
    Console.WriteLine("Enter the amount of flour");
    cake.HowMuchFlour = int.Parse(Console.ReadLine());
 
    Console.WriteLine("To produce this cake it's necessery to pour in " +
         cake.AmountOfSalt + "g salt, to pour in " +
         cake.HowMuchMilk + "l milk, add " + cake.NumberOfEggs + "eggs, to pour in " +
         cake.HowMuchFlour + "kg flour and put in the oven on "+cake.BakingTime+"min");
 
    cake.PourSalt();
    Console.WriteLine(<span class="pl-s"><span class="pl-pds">"Change the baking time of the dough</span><span class="pl-pds">"</span></span>);
    string time = cake.BakingCake(cake.BakingTime = int.Parse(Console.ReadLine()));
    Console.WriteLine(time);
    Cake.PourFlour();
    Console.ReadKey();
}

This content also you can find on my blog http://devman.pl/csharplan/c-language-methods/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

That’s it for today, in the next lesson we will learn about constructors and destructors, see you!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 64432.28
ETH 2648.26
USDT 1.00
SBD 2.78