shopify traffic stats

Ok, so I just created my first C# method (a.k.a – function).  It basically asks for 2 numbers, adds them and then returns the result.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Adder

{

class Program

{

static void Main(string[] args)

{

//ask for the first number

Console.WriteLine(“Please enter the first number”);

 

//grab the first number using Console.Readline and convert it to an int using Int32.Parse

int num1 = Int32.Parse(Console.ReadLine());

 

//ask for the second number

Console.WriteLine(“Please enter the second number”);

 

//grab the second number using Console.Readline and convert it to an int using Int32.Parse

int num2 = Int32.Parse(Console.ReadLine());

 

//call the AddTwoNumbers method (function) and pass the num1 and num2 variables to the method  

AddTwoNumbers(num1, num2);

Console.ReadLine();

}

//create a method (function) called AddTwoNumbers that requires 2 int’s

public static int AddTwoNumbers(int num1, int num2)

{

//create an int called sum and add num1 + num2

int sum = num1 + num2;

Console.WriteLine(“The total is {0}”, sum);

return sum;

}

}

}




The result is something like this:

c# method

Share This
%d bloggers like this: