Advertisement
//C# You cannot return anything from a method with void return type
using System;
class A
{
//Defining the method, Message(), of class A
public void Message()
{
Console.WriteLine("Hello");
//Returning an int value from a method with void return type
return 1;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
ob.Message();
}
}
A.cs(13,2): error CS0127: Since 'A.Message()' returns void, a return keyword must not be followed by an object expression
//C# An empty return statement is perfectly fine in a void method
using System;
class A
{
//Defining the method, Message(), of class A
public void Message()
{
Console.WriteLine("Hello");
//An empty return statement is perfectly fine in a void method
return;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
ob.Message();
}
}
Hello
//C# A larger primitive return type of a method can be used to return a smaller primitive value.
//as long as it doesn't lead to a loss in the value of smaller primitive types.
using System;
class A
{
//Instance variable
byte b = 10;
//Defining the method, Message(), of class A
//With return type, int
public int Message()
{
Console.WriteLine("Hello");
//Returning a small value type i.e. byte
//From a method with a larger value type i.e. int
return b;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
Console.WriteLine(ob.Message());
}
}
Hello
10
Here we are returning a byte value out of a method that has an int return type, this works because -
Advertisement
//C# A larger primitive return type of a method can be used to return a smaller primitive value.
//That's, why a char value can be returned from a method with double return type
//Especially, because there will be no loss n the value of char
using System;
class A
{
//Instance variable
char ch = 'a';
//Defining the method, Message(), of class A
//With return type, double
public double Message()
{
Console.WriteLine("Hello");
//Returning a small value type i.e. char
//From a method with a larger value type i.e. double
//Especially, because there will be no loss n the value of char
return ch;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
Console.WriteLine(ob.Message());
}
}
Hello
97
//C# A floating-point value cannot be returned from a method with an integer return type
//Because the operation will lead to a loss in the value of smaller primitive types.
using System;
class A
{
//Instance variable
float fl = 10.4f;
//Defining the method, Message(), of class A
//With return type, long
public long Message()
{
Console.WriteLine("Hello");
//Returning a value type i.e. float
//From a method with a value type i.e. long
return fl;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
Console.WriteLine(ob.Message());
}
}
A.cs(20,9): error CS0266: Cannot implicitly convert type 'float' to 'long'. An explicit conversion exists (are you missing a cast?)
//C# The type of base class can be used to return an object of its derived class from a method
using System;
//Base class
class A
{
}
//Derived class
class B:A
{
public A Message()
{
Console.WriteLine("Hello");
//Currently executing object of derived class type, B
//is returned from a method with base class, A, return type.
return this;
}
public static void Main(String[] ar)
{
//An object of B is created
B ob = new B();
//The method Message() is called on this object of B
ob.Message();
}
}
Hello
//C# Returning an array from a method with the Object class as its return type
using System;
class A
{
//Method with Object class return type
public Object Message()
{
//Creating an int array
int[] arr = new int[]{1,2,3,4};
//returning an int array, which is also an object.
return arr;
}
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Casting the Object reference back to int array
int[] arr2 = (int[])ob.Message();
//Reading the int array
for(int i=0;i<arr2.Length; i++)
Console.WriteLine(arr2[i]);
}
}
1
2
3
4
//C# Returning a null reference from a method with a class return type
using System;
class A
{
//Defining the method, Message(), of class A
//With return type of class, A
public A Message()
{
Console.WriteLine("Hello");
//Returning nuill
//From a method with a return type of a class
return null;
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Using the object of class A to call its method Message()
ob.Message();
}
}
Hello
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement