Return Types and Void Methods in c#
In the C# programming language, return type and void method are two important and essential function program methods or concepts. They determine what and how a user-defined program method returns a value or function to its caller in the main program, and whether a function method returns a value to the program as void data.

So, let’s take a closer look at return type and void method in the C# programming language.
Return Type Methods in C#.
The return type of a method defined in a C# program indicates what kind of program value the method will return to the caller in the current program. Here, the return data type can be any valid data type, such as int, string, double, custom classes, etc., or a user-defined data type. If the function does not return any value to the program, it can be defined as a void function.
Defining Return Types in C# Programs.
The syntax for a method with a return type in a C# program is as follows:
returnType MethodName(parameter1Type parameterfirstName, parameter2Type parametereondName)
{
/ Here we can define a function method body
return value; // If the program return type is not void, then it returns any value
}
Element of Return Types in C# Programs.
- In a C# program, the returnType indicates what type of data type the method will return in the current program. For example, int, string, double, etc. are data types.
- If the return type of the method is defined as non-void, then it returns a value of that type in the program.
Example of a method with a return type in a C# program.
using System;
class ReturnType
{
static void Main()
{
int output = totalinteger(1, 8, 3);
Console.WriteLine(“the total is – ” + output); // Result is – 12
}
static int totalinteger(int p, int q, int r)
{
return p + q + r; // Here it returns the sum of p, q, and r in the current program
}
}
Here’s the return type in a C# program example.
- In this program, the return data type of the totalinteger method is defined as int, so it returns an integer value.
- The return p + q + r; statement is important because the return method returns an integer value.
Void in Methods in C#.
A void method in a C# program is a program method that does not return any value. Instead of specifying the return data type of a void program method, such as int, string, etc., C# uses the void keyword to indicate to programmers that the method defined in the current program does not return any value.
The syntax for a void method type in C# is.
void MethodName(parameterfirstType parameterfirstName, parametersecondType parametersecondName)
{
// Here you can define a void method.
// Here you can define a void method.
No return statement is needed.
}
Element of the void method type in C#.
The void keyword in a C# program indicates that the current program method will not return any value.
Example of a C# void method.
using System;
class VoidMethod
{
static void Main()
{
DisplayInfo(“Welcome to C# programming”);
}
static void DisplayInfo(string info)
{
Console.WriteLine(info); // This program shows there is no return value method
}
}
Here in this void method example.
- In this program, the return data type of the DisplayInfo method is void, as it simply prints a user-defined message as an action and does not return anything to the program.
- Remember, in a C# program, there is no need for a return statement in a void method.
Returning a Value from a Non-Void Method in a C# Program.
For a C# program method with a return data type other than the void keyword, the user-defined program method must return a value that properly matches the return data type indicated in the current program. Here is a simple example of returning a value from a multiple-individual data type value method in a C# program.
Returning an integer (int) value in a C# program example.
using System;
class Product
{
static void Main()
{
int output = Mul(4, 3,2);
Console.WriteLine(“The Multiplication of – ” + output); // Output – The Multiplication of – 24
}
static int Mul(int p, int q, int r)
{
return p*q*r; // here it Return the product of p * q * r
}
}
Example of returning a string data type value in a C# program.
using System;
class Program
{
static void Main()
{
string info = text(“Vcanhelpsu”);
Console.WriteLine(info); // Result – |Welcome to, Vcanhelpsu|
}
static string text(string comp_name)
{
return “|Welcome to, ” + comp_name + “|”; // Here it returns a company info
}
}
Behavior of Void Methods in C#.
Since void methods in C# programs do not return any value, you can easily use the void keyword in C# programs to print output messages, modify class objects’ data, or directly interact with other elements of the program.
Example of changing an object in a void method.
using System;
class Employee
{
public string Emp_Name;
}
class Program
{
static void Main()
{
Employee employee = new Employee { Emp_Name = “Harry” };
UpdateName(employee);
Console.WriteLine(employee.Emp_Name); // Result is Bhavishi
}
static void UpdateName(Employee e)
{
e.Emp_Name = “Bhavishi”; // Here we modify the employee’s name, even though the method is void
}
}
Changing an object in a void method explanation.
Even though the method UpdateName is void defined in this program, it still updates the state of the Employee object. In this case, the void method does not return any value. Instead, it changes the name of the Employee class object passed to it.
Returning a null value from a method in a C# program.
In a C# program, methods that define a reference data type as the return data type, such as String, Employee, can also return a null value to indicate that there is no data value, or that an uninitialized value is defined.
Example of a null value from a method in C#.
using System;
class NullReturn
{
static void Main()
{
string output = textinfo(false);
Console.WriteLine(output); // Result is – (null)
}
static string textinfo(bool isEligable)
{
if (isEligable)
return “You are eligible for C# programming”;
else
return null; // We return null, so there is no return type value
}
}
Here’s a null value from a method example.
Here in this program, if isEligible is defined as false, the program returns a null value to the method, indicating that there is no possibility of returning data in this type of program.
Void Method and Early Return in C# Programs.
As we know, void methods in C# programs do not return any value. However, C# programmers can use the return keyword to exit a program method immediately. The void method is mostly used to exit a method under some specific program condition.
Early Exit Example in Void Method in C# Program.
using System;
class Program
{
static void Main()
{
EnterValue(10); //Result is – You enter an invalid integer value
EnterValue(-3); // Result is – Display integer -3
}
static void EnterValue(int integer)
{
if (integer > 0)
{
Console.WriteLine(“You entered an invalid integer value”);
return; // Here it displays the early return value, and exits the program method
}
Console.WriteLine(“Display integer ” + integer);
}
}
A Summary of Return Types and Void Methods.
- Return Type – In any C# program, a function or program method with a defined return data type, such as int, double, or string, must return a value of that data type.
- Void Method – Remember, in a C# program, a function class method with the return type Void keyword does not return any program value and is used when you want the method to perform an action within the current program.
- Returning null – For reference data types in C# programs, programmers can also return a null value to the program to represent an empty or uninitialized method state.
- Void Method and Early Exit – In void methods in C# programs, C# programmers can use the return type keyword to perform an immediate exit from the method.
