C# | Method returning an object
Last Updated :
10 May, 2019
In C#, a method can return any type of data including objects. In other words, methods are allowed to return objects without any compile time error.
Example 1:
CSharp
// C# program to illustrate the concept
// of the method returning an object
using System;
class Example {
// Private data member
private string str;
// Method to set the value of str
public void setdata(string s)
{
str = s;
}
// Method to display the value of str
public void Display()
{
Console.WriteLine("String is: " + str);
}
// Method that return object
public Example Astr(Example ex)
{
// Creating object of Example
Example obj = new Example();
// Adding the value of passed
// an object in the current object
// and adding the sum in another object
obj.str = str + ex.str;
// Returning the object
return obj;
}
}
// Driver Class
class GFG {
// Main method
static void Main()
{
// Declaring objects of Example
Example o1 = new Example();
Example o2 = new Example();
// Initialize the values to the objects
o1.setdata("Geeks");
o2.setdata("forGeeks");
// Adding value of both objects
// and the result will be
// assigned into third object
Example o3 = o1.Astr(o2);
// Display the data
o1.Display();
o2.Display();
o3.Display();
}
}
Output:
String is: Geeks
String is: forGeeks
String is: GeeksforGeeks
Explanation: In the above example, we have a class named as
Example.
Example class contains
setdata() method which is used to set the value of
str, and
Display() method is used to display the value of
str, and
Astr() is used to add the value of passed object in current object and adding the sum in another object. In Main method, three objects
o1,
o2, and
o3 of
Example class are created. In this statement,
Example o3 = o1.Astr(o2);, the value of
o1 and
o2 object is added and the result is assigned into
o3 object.
Example 2:
CSharp
// C# program to illustrate the
// concept that how method returns
// an object
using System;
class Triangle {
// Data member of class
int Base;
int Height;
// Constructor of class
public Triangle(int b, int h)
{
Base = b;
Height = h;
}
// Method return area of triangle
public int Area()
{
return ((Base * Height) / 2);
}
// Method display the dimension of triangle
public void Display()
{
Console.WriteLine("\nBase of the triangle is: "
+ Base + "\nHeight of the triangle is: "
+ Height);
}
public Triangle newdimension(int d)
{
return new Triangle(Base * d, Height * d);
}
}
class GFG {
// Main method
public static void Main()
{
// Creating and initializing object
Triangle t1 = new Triangle(2, 8);
// Display the dimensions and area of triangle
Console.Write("Dimensions of Triangle is: ");
t1.Display();
Console.Write("Area of Triangle is: {0}", t1.Area());
Console.WriteLine();
Console.WriteLine();
Triangle t2 = t1.newdimension(2);
Console.Write("New Dimensions of Triangle is: ");
t2.Display();
Console.Write("New area of Triangle is: {0}", t2.Area());
}
}
Output:
Dimensions of Triangle is:
Base of the triangle is: 2
Height of the triangle is: 8
Area of Triangle is: 8
New Dimensions of Triangle is:
Base of the triangle is: 4
Height of the triangle is: 16
New area of Triangle is: 32
Explanation: In the above example, we have a class named as the
Triangle. The Triangle class contains constructor
Triangle(), method
Area() to find the area of the triangle, method
Display() to display the dimension of the triangle, and method
newdimension() to provide a new dimension of the triangle. The value of the dimension is returned by the object. Now in the Main method there are two objects named as
t1 and
t2. In this statement
Triangle t2 = t1.newdimension(2);, the previous dimension, i.e. 2 and 8 of the triangle is enlarged by 2 and the result assigned to the
t2 object.
Similar Reads
C# | Object.GetTypeCode() Method with Examples This method is used to return the Type of the current instance. Here, Type Represents type declarations i.e. class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types. The System.Object class is
3 min read
C# | Object.GetHashCode() Method with Examples This method is used to return the hash code for this instance. A hash code is a numeric value which is used to insert and identify an object in a hash-based collection. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality. Syntax: public virtual int
2 min read
C# | Convert.GetTypeCode(Object) Method This method is used to return the TypeCode for the specified object.Syntax: public static TypeCode GetTypeCode (object value); Here, the value is an object that implements the IConvertible interface.Return Value: This method returns the TypeCode for value, or Empty if value is null.Below programs il
2 min read
C# | Operator Overloading Prerequisite: Operators in C# The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. It ena
4 min read
C# Methods A method is a block of code that performs a specific task. It can be executed when called, and it may take inputs, process them, and return a result. Methods can be defined within classes and are used to break down complex programs into simpler, modular pieces. Methods improve code organization, rea
4 min read