Introduction
C# is an object-oriented programming coding language by Microsoft that runs on the .NET Framework. The letter C# is pronounced as "C-Sharp" C# is connected to and has origins in the C family, as do other prominent languages such as C++ and Java.
In C#, a method is a code block that comprises a sequence of statements. By calling the method and giving any needed method parameters, a programme causes the statements to be performed. In this blog, we will be learning more about methods and different ways of returning an object.
What are Methods in C#?
Methods are often the blocks of code or statements in a code that allow users to reuse the same code, which saves memory, saves time, and, most significantly, improves code readability. A method is, in essence, a set of statements that do a given job and provide the outcome to the caller. A method can also do something particular without returning anything.
You can check out Methods in C# for detailed information about Methods.
Method Returning an Object
A method in C# can return any type of data, including objects. In other words, methods can return various objects without causing a compile-time error.
Let us understand this concept with the help of examples:
Example-1
//method returning an object
using System;
class CodeExample {
// Private members
private string str1;
public void setdata(string s)
{
str1= s;
}
// Displaying the value of str1
public void Display()
{
Console.WriteLine("String is: " + str1);
}
// Method for returning object
public CodeExample Astr(CodeExample ex)
{
// Creating object of CodeExample
CodeExample obj = new CodeExample();
// Adding the value of passed
// an object in the current object
// and adding the sum in another object
obj.str1= str1+ ex.str1;
// Returning the object
return obj;
}
}
// Driver Class
class CodingNinjas {
// Main method
static void Main()
{
// Declaring objects of CodeExample
CodeExample o1 = new CodeExample();
CodeExample o2 = new CodeExample();
// Initialize the values to the objects
o1.setdata("CODING ");
o2.setdata("NINJAS");
// Adding value of both objects
// and the result will be
// assigned into third object
CodeExample o3 = o1.Astr(o2);
// Display the data
o1.Display();
o2.Display();
o3.Display();
}
}
Output
String is: CODING
String is: NINJAS
String is: CODING NINJAS
Explanation:
In the above example code, we have a class called CodeExample. The setdata() function in the example class is used to set the value of str, the Display() method is used to display the value of str whereas the Astr() is used to add the value of the provided object in the current object and the sum in another object. Three objects of the Example class, o1, o2, and o3 are created in the Main function. In this sentence, o3 = o1.Astr(o2); the values of the o1 and o2 objects are added, and the result is allocated to the o3 object.
Example-2
//method returns an object
using System;
class Triangle {
// Data member
int Base;
int Height;
// Constructor
public Triangle(int b, int h)
{
Base = b;
Height = h;
}
// Method returning area of triangle
public int Area()
{
return ((Base * Height) / 2);
}
// Method displaying the dimensions
public void Display()
{
Console.WriteLine("
Base of the triangle is: "
+ Base + "
Height of the triangle is: "
+ Height);
}
public Triangle newdimension(int d)
{
return new Triangle(Base * d, Height * d);
}
}
class CodingNinjas {
// 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 called Triangle. The Triangle class includes the function Object() { [native code] } Triangle(), the method Area() for calculating the triangle's area, the method Display() for displaying the triangle's dimension, and the method newdimension() for adding a new dimension to the triangle. The object returns the dimension value. There are now two objects labelled t1 and t2 in the Main function. Triangle t2 = t1.newdimension(2); enlarges the previous dimension of the triangle, i.e. 2 and 8, by 2 and assigns the result to the t2 object.