Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
What are Methods in C#?
2.
Method Returning an Object
2.1.
Example-1
2.1.1.
Output
2.1.2.
Explanation:
2.2.
Example-2
2.2.1.
Output
2.2.2.
Explanation:
3.
Frequently Asked Questions
3.1.
What is method overloading in C#?
3.2.
 What do you mean by Anonymous Method in C#?
3.3.
What is enum in C#? 
4.
Conclusion
Last Updated: Mar 27, 2024

Method returning an object in C#

Author Akash Nagpal
0 upvote

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.

Frequently Asked Questions

What is method overloading in C#?

One of the most effective methods for implementing polymorphism is method overloading. It is the capacity to rewrite a function in a variety of ways. Function overloading can be implemented by defining two or more functions in a class with the same name. Methods with the same name but different parameter lists (number of arguments, order of parameters, and data types of parameters) might exist within the same class. For detailed information, you can checkout Method Overloading in C#.

 What do you mean by Anonymous Method in C#?

An anonymous method does not have a name and was introduced in C# 2.0. It comes in handy when the user wishes to construct an inline method while simultaneously passing parameters in the anonymous method like other methods. The delegate keyword is used to specify an Anonymous method so that the user can assign this method to a new variable of the delegate type.

What is enum in C#? 

An enum is a value type that contains a set of related named constants known as an enumerator list. To declare an enumeration, use the enum keyword. It is a user-defined basic data type. An integer can be an enum type (float, int, byte, double, etc.). However, if you use it in addition to int, it must be cast.

In the.NET framework, an enum is used to construct numeric constants. All enum members are of the enum type. Each enum type must have a numerical value.

 Also Read About - singleton design pattern in c#

Conclusion

In this article, we have extensively discussed how Method returns an object in C#. We have also discussed returning objects with the help of various examples. We hope this blog has helped you enhance your knowledge regarding the C#. Some official documentation on big data that can help you improve your understanding is C# Generics and C# Inheritance.

If you would like to learn more, check out our articles on Operator Keyword in C#cloud platform comparison, C# interview questions for 5 years Experience, and 10 AWS best books

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass