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.
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.
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.
- C# | Add an object to the end of Collection<T>
- C# | Object Class
- C# | Add an object to the end of the ArrayList
- C# | Class and Object
- C# | Search in a SortedList object
- C# | Getting the Values in a SortedList object
- C# | Getting the keys in a SortedList object
- C# | Getting the key at the specified index of a SortedList object
- C# | Get an IDictionaryEnumerator object in OrderedDictionary
- C# | Getting the value at the specified index of a SortedList object
- Object and Dynamic Array in C#
- C# | Get object at the top of the Stack - Peek operation
- C# | Add an object to the end of the Queue - Enqueue Operation
- C# | OrderedDictionary.Item[Object] Property
- C# | Check if a SortedList object contains a specific value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.