Return Types in C#

In C# a method can return zero or one value. To return values from method to method call ‘return’ keyword is used. The return keyword causes to exit from executing method and control goes to next line after method call. If there are statements after return in method then these statements are skipped.   The type of value to be return is defined in the method signature. The method can return only value of type defined in method signature. The general form for returning value from method is

<access_specifier> <return_type>Method_Name(parameter_list)
{
            //Statements to be executed
            return <value>;
}

Here value may be literal or variable of type return_type. In the method call we get this value and we can assign this value to another variable like below
<Type> <variable>=Method_Name();
In above statement the type of variable must be compatible with the return type of method. One can also embed method call in expressions.

A method can return values of following type

1.  Void:
The void keyword is used to denote that method is not returning in any value. Void is not a type, void means nothing. Once method is defined with void keyword it is unable to return any value but one can still use return statement to exit from this method.

Example:
using System;
namespace VoidDemo
{
            class Demo
    {
            public void DisplayMessage()
        {
            Console.WriteLine("Hi! you are new user.");
        }
            public void ShowMessage()
        {
            Console.WriteLine("You may want to register.");
            return;     //Use return keyword to exit method
            Console.WriteLine("Your registration is completed.");  //Unreachable line
        }
    }
            class Program
    {
            static void Main(string[] args)
        {
            Demo demo1 = new Demo();
            demo1.DisplayMessage();
            demo1.ShowMessage();
            Console.ReadLine();
        }
    }
}

The output of above program is

Hi! you are new user.
You may want to register.

2.  Return Value types:
The method can return literal or variable of any built in value type using return keyword. To return variable, it must be initialized to some value. If you try to return unsigned variables then complier raises an error. We can also use expression after return keyword which produces value of type specified in method signature.

Example:
using System;
namespace ReturnValueType
{
            class Demo
    {
            public int ReturnLiteral()
        {
            return 100; //Returning integer literal
        }
            public int ReturnVariable()
        {
            int a = 200;
            return a;   //Returning variable
        }
            public int ReturnExpression()
        {
            int a = 20;
            int b = 12;
            return a + b;   //Returning result of expression
        }
    }
            class Program
    {
            static void Main(string[] args)
        {
            Demo demo1 = new Demo();
            Console.WriteLine(demo1.ReturnLiteral());
            Console.WriteLine(demo1.ReturnVariable());
            Console.WriteLine(demo1.ReturnExpression());
            Console.ReadLine();
        }
    }
}

The output of above program is

100
200
32

3.  Return reference types:
A method can also return reference types also including user defined classes. Below program shows the demonstration of returning object of user defined class.

Example:
using System;
namespace ReturnReferenceType
{
            class Employee
    {
            public string EmpCode;
            public string EmpName;
            public string City;
    }
            class Demo
    {
            public Employee GetEmployee(string empCode, string empName, string city)
        {
            Employee emp = new Employee();
            emp.EmpCode = empCode;
            emp.EmpName = empName;
            emp.City = city;
            return emp;
        }
    }
            class Program
    {
            static void Main(string[] args)
        {
            Demo demo1 = new Demo();
            Employee emp = demo1.GetEmployee("001", "Ram Sharma", "Mumbai");
            Console.WriteLine("Employee Code: {0}", emp.EmpCode);
            Console.WriteLine("Employee Name: {0}", emp.EmpName);
            Console.WriteLine("Employee City: {0}", emp.City);
            Console.ReadLine();
        }
    }
}

The output of above program is

Employee Code: 001
Employee Name: Ram Sharma
Employee City: Mumbai


4.  Returning array:
In C# arrays are implemented as object, so one can return array from method like object. The return type is same as we used to declare an array.  

Example:
using System;
namespace ReturnArray
{
            class Demo
    {
            public int[] GetArray()
        {
            int[] nums = { 10, 20, 30, 40, 50 };
            return nums;
        }
    }
            class Program
    {
            static void Main(string[] args)
        {
            Demo demo1 = new Demo();
            int[] numbers = demo1.GetArray();
            for(int i=0;i<5;i++)
            {
                Console.WriteLine(numbers[i]);
            }
            Console.ReadLine();
        }
    }
}

The output of above program
10
20
30
40
50
Returning List:      
A method can also return a list. In C# list is collection of variables. List can hold value types or reference types. Lists are dynamic in size.

Example:

using System;
using System.Collections.Generic;
namespace ReturnList
{
            class Employee
    {
            public string EmpCode;
            public string EmpName;
            public string City;
    }
            class Demo
    {
            public List<Employee> GetEmployees()
        {
            //Creating list of employees
            List<Employee> lstEmployee = new List<Employee>(); 
            Employee employee1 = new Employee();
            employee1.EmpCode = "001";
            employee1.EmpName = "Jhon";
            employee1.City = "Delhi";
            lstEmployee.Add(employee1); //Adding employees to list
            Employee employee2 = new Employee();
            employee2.EmpCode = "002";
            employee2.EmpName = "Thomson";
            employee2.City = "Newyork";
            lstEmployee.Add(employee2);
            Employee employee3 = new Employee();
            employee3.EmpCode = "003";
            employee3.EmpName = "Jack";
            employee3.City = "Washington";
            lstEmployee.Add(employee3);
            return lstEmployee; //Returning list
        }
    }
            class Program
    {
            static void Main(string[] args)
        {
            Demo demo1 = new Demo();
            List<Employee> lstEmp = demo1.GetEmployees();
            for(int i=0;i<lstEmp.Count;i++)
            {
                Console.WriteLine("Employee Code: {0}", lstEmp[i].EmpCode);
                Console.WriteLine("Employee Name: {0}", lstEmp[i].EmpName);
                Console.WriteLine("Employee City: {0}", lstEmp[i].City);
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

The output of above program is

Employee Code: 001
Employee Name: Jhon
Employee City: Delhi

Employee Code: 002
Employee Name: Thomson
Employee City: Newyork

Employee Code: 003
Employee Name: Jack

Employee City: Washington
Previous Post Next Post