Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Last active September 20, 2021 08:46
Show Gist options
  • Save rruntsch/e2e05dd9ab16ca82511a6f6ed8055fd5 to your computer and use it in GitHub Desktop.
Save rruntsch/e2e05dd9ab16ca82511a6f6ed8055fd5 to your computer and use it in GitHub Desktop.
// C# introduction examples:
// Calling a method without and with parameters and
// without and with return values.
using System;
namespace hello_world
{
class Program
{
static void Main(string[] args)
{
// Call HelloWorld() method without input parameters and without a return value.
HelloWorld();
// Call Add() method with two input parameters (as literal values) and without a return value.
Add(30, 40);
// Call method Add() with two input parameters (as variable values) and without a return value.
int no1 = 30;
int no2 = 40;
Add(no1, no2);
// Call method Alert() and use default parameter value. The Alert() method does not return a value.
Alert();
// Call method Alert() with a custom parameter value. The Alert() method does not return a value.
Alert("The program crashed!");
// Call method Multiply() with two input parameters. Write the return value
// to the console.
int product = Multiply(20, 30);
Console.WriteLine("Product: " + product.ToString());
}
static void HelloWorld()
{
// Write "Hello World!" to the console window.
// This method does not accept any input parameters.
// Also, it does not return a value.
Console.WriteLine("Hello World!");
}
static void Add(int num1, int num2)
{
// Add two whole numbers and write the sum to the console window.
// This method accepts two input parameters.
// It does not return a value.
int sum = num1 + num2;
Console.WriteLine("Sum: " + sum.ToString());
}
static void Alert(string message = "Error!")
{
// Write the value of the input parameter message to the console window.
// If the method is called without a parameter value, write the
// default value "Error!" Else, write the value that the parameter message
// was set to when the method was called.
// This method accepts one input parameter with a
// default value of "Error!".
Console.WriteLine("Alert: " + message);
}
static int Multiply(int num1, int num2)
{
// Multiply two whole numbers provided as input parameters.
// Return the product set in the variable called output.
int output = num1 * num2;
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment