Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to return multiple value from method in c# where one is custom class list value and another is decimal.
Posted
Comments
ChauhanAjay 24-Aug-14 2:34am    
check these link
http://stackoverflow.com/questions/2918861/method-should-return-multiple-values
http://www.c-sharpcorner.com/UploadFile/mahesh/out_and_ref11112005002102AM/out_and_ref.aspx
may be it helps

There are different ways to do that, but all approaches are classified into 3 ways; and also you can combine them.

  1. One object can be returned as the result of the function.
  2. Additionally, one or more objects can be returned using out parameter. It also can be ref, but then return won't be mandatory, which could be a problem.
  3. Each of the ways to return some object mentioned above can be used to return some compound object. You can declare some type with the members representing more than one objects to be returned. So, you can return multiple objects composed in one. This compound type can be your own, or it could be one of collection classes. You can also return a pair of objects using the generic type System.Collections.Generic.KeyValuePair or 1-8 objects using the generic type System.Tuple:
    http://msdn.microsoft.com/en-us/library/5tbh8a42%28v=vs.110%29.aspx[^],
    http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx[^].


—SA
 
Share this answer
 
v3
The actual return value of a C# method can only ever be a single object instance - a class, or a list, or a double for example - but there are ways to get more values back to the calling method.

First you can encapsulate the values you want in a custom class, which holds just the custom class list value and a decimal.
C#
public class ReturnValue
   {
   public List<MyClass> List;
   public double Value;
   }

public ReturnValue MyMethod()
   {
   ReturnValue ret = new ReturnValue();
   ...
   return ret;
   }
That's a little clumsy, but it works.

Then you can use ref and out parameters on your method:
C#
public bool MyMethod(ref List<MyClass> list, out double d)
   {
   foreach(MyClass in list)
      {
      ...
      }
   list = new List<MyClass>();
   ...
   d = 1.23;
   return true;
   }
This can be a lot more flexible, but looks a little rough and ready sometimes.
 
Share this answer
 
You can return
1) An object of a class that contains two attributes ( listvalue and decimal).
2) You can return a KeyValuePair[^].
3) Return a Tuple[^] with multiple values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900