c# new function code example

Example 1: How to make a function in C#

public void SayHello(string name) 
{
    Console.WriteLine("Hello")  
}


Console.WriteLine("What is your name?")
string name = Console.ReadLine() ; 

SayHello(Adam)

Example 2: how to make a function inside a function c#

public static void Main(string[] arg) {
 	Console.WriteLine("hello");
  	void World() {
     	Console.WriteLine("world") 
    }
  	world();
}

/*
output:
hello
world
*/

Example 3: c# new keyword

The new operator creates a new instance of a type.
  
In the example below an object is created for the class using the new.
   Calculate c = new Calculate();

You can also use the new keyword to create an instance of the array.
  double[] points = new double[10];

The new keyword is also used to create object of a collection.
  SortedList sl = new SortedList(); // SortedList
  List<string> myList = new List<string>() // List