ankitweblogic logo
W e b t u t o r i a l s

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Exercise-1

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

C# User-Defined Method

A method allows you to encapsulate a piece of code and call it from other parts of your code. There may be a situation where you want to repeat a piece of code, from multiple places, In that situation methods can be used.
To Use a Method, you need to
• Define the Method
• Call the Method

Example 1: Display message "Hello, World" using method

Output

Overloading Methods

In Method Overloading, we can create multiple methods with the same name, but with different parameters. The method call will be resolved by the number, type and sequence of parameters passed. In method overloading return type of method doesn’t matter.

Example 2: Input 2 Integer, Decimal, String Value and Sum them using Method Overloading

Output

Exercise: Find Area of Circle and Rectangle using method overloaing

Call by Value and Call by Reference Parameter

When we pass a value into a variable used by the function then any changes made to this variable in the function have no effect on the parameters specified in the function.

If we pass the parameter by reference we have to use ref keyword. The ref keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.

Example : Call by Value

class Class1
{
	public static void MyMethod (int val) 
	{
		val = val * 2;
		Console.WriteLine("val doubled={0}",val);
	}
	static void Main (string[] args)
	{
		int x=5;
		MyMethod(x);
		Console.WriteLine("x={0}",x);
		Console.ReadLine();
	}
}

Example : Call by Reference

class Class1
{
	public static void MyMethod (ref int val) 
	{
		val = val * 2;
		Console.WriteLine("val doubled={0}",val);
	}
	static void Main (string[] args)
	{
		int x=5;
		MyMethod(ref x);
		Console.WriteLine("x={0}",x);
		Console.ReadLine();
	}
}

Out Parameter

We can also specify that a given parameter is an out parameter using the out keyword, which is used in same way as ref keyword. However, there are important differences. It is illegal to use an unassigned variable in a ref parameter however, we can use in out parameter. Declaring an out method is useful when you want a method to return multiple values. A method can have more than one out parameter.

class Class1
{
    public static intTestOut(out char i) 
    {
        i = 'b';
        return -1;
    }
    static void Main(string[] args)
    {
        char i;   // variable need not be initialized
        Console.WriteLine(TestOut(out i));
        Console.WriteLine(i);
    }
}

Parameter Arrays

C# allows us to specify one special parameter for a method. This is known as parameter array. It is defined using params keyword. The params keyword lets you specify a method parameter that takes an argument where the number of arguments is flexible. No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration. If two parameters are specified in the function then params should be in last.


Ad
Java tutorial on core java, servlet, jsp, struts, spring, jsf, hibernate, junit, javamail api, quartz schedular, jdbc, generics, sql, log4j, iText, ant, jsoup, jaxb, json, javascript, xml parser, json, jar, war, oracle plsql, angularjs, apache maven, typescript

Optional Parameters

While calling a method we can omit its argument value. That argument is assigned with a value known as default value or optional parameter. C# now supports using optional parameters with methods, constructors, and indexers.


Recursion/Recursive Method

A Method which call itself again and again until the condition is satisfied is known as Recursion Method. In Recursion Method, we cannot use loops only if conditions are applied.

Example: Find Factorial

class Program
{
	static int fac(int n)
	{
		if (n == 1)
			return n;
		return n * fac(n - 1);
	}
	static void Main(string[] args)
	{
		int a = fac(6);
		Console.WriteLine(a);
		Console.ReadLine();
	}
}
Updated: 3-Apr-19