-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Method
- Method with returning value
- Method without returning any value
- Default Parameter Value Method
- Method Overloading in C#
C# Method
- C# method is a set of statements that are grouped together to perform an operation.
- Whenever user wants to perform any operation multiple times then they choose methods.
- A method is a block of code which only runs when it is called. User can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.
- The Main method is the entry point for every C# application and it's called by the common language runtime (CLR) when the program is started.
-
Basically, two types of User Defined Methods are available in C#.
a. Method with returning value
b. Method without returning any value - Information can be passed to methods as parameter. Parameters act as variables inside the method. They are specified after the method name, inside the parentheses. User can add as many parameters as they want, just separate them with a comma.
C# Program to illustrate how to create and access User defined method:
//Create a Method with returning Value and two parameters passed
public int add(int a, int b){
int result = a + b ;
return result;
}
//Create a Method without returning Value and without any parameter
public void helloWord()
{
Console.WriteLine("Welcome to Programming World");
}
//Create Object and access Method in the main method assumed Sample is the class name
Sample obj = new Sample();
int x = obj.add(10, 25);
Console.WriteLine(x);
Obj.helloWord();
Default Parameter Value Method
- User can also use a default parameter value, by using the equals sign (=). In case user call the method without an argument, it uses the default value defined in the method.
//declaring the method as Default Parameter Value
static void testCalendarDay(string calendarDay = "Monday")
{
Console.WriteLine(calendarDay);
}
static void Main()
{
testCalendarDay("Tuesday"); //Output Tuesday
testCalendarDay("Wednesday"); //Output Wednesday
testCalendarDay(); //Output Monday which is the Default Parameter Value
}
public int add(int a, int b)
{ int result = a + b;
return result;
}public int add(int a, int b, int c)
{
int result = a + b + c;
return result;
}public double add(double a, double b)
{
double result = a + b;
return result;
}public void helloWord()
{
Console.WriteLine("Welcome to Programming World");
}public void helloWord(string language)
{
Console.WriteLine("Welcome to Programming World : " + language);
}static void testCalendarDay(string calendarDay = "Monday")
{
Console.WriteLine(calendarDay);
}
Console.WriteLine("Welcome to C# World");
Console.WriteLine("Welcome to C# World");Console.WriteLine("Welcome to C# World");
testCalendarDay("Tuesday");
testCalendarDay("Wednesday");
testCalendarDay();
Program obj = new Program();
int c = obj.add(1, 2);
Console.WriteLine(c);
Console.WriteLine(obj.add(10, 15, 20));
Console.WriteLine(obj.add(10.677, 92.7880)); obj.helloWord();
obj.helloWord("CSharp");