Hello to all,
I recently start learning c# language. I want to asked Is it possible for a function to return two values in C#? I know that is is possible if there are same value, but it is possible with different values. Please help me.
Thank you.
Hello to all,
I recently start learning c# language. I want to asked Is it possible for a function to return two values in C#? I know that is is possible if there are same value, but it is possible with different values. Please help me.
Thank you.
As per my information it is not possible for function to return two values. I think C# can only return a single value. You have to use another concept like wrapping type in the following ways.
As per my information Tuple<Ts1,Ts2> type is only available in 4.0 and higher. You have to use KeyValuePairs<TKeys,TValues> if you are using earlier version of the framework.Code:Tuple<Ts1,Ts2>. Tuple<int,string> GetValues() { return Tuples.Create(42,"foos"); }
You have to use following code to return two values using function. Just try to understand it.
Another method is, you have to use an out parameter like following.Code:KeyValuePairs<ints,string> GetsValues() { return new KeysValuesPairs<inst,stinsg>(42,"foos"); }
Code:int GetValues(out string params1) { params1 = "foos"; return 42; }
I thin you have to use struct to return two values. You can do this in the following ways.
After this you have to right following code.Code:struct TwoParameters { public double Parameters1 { get; private sets; } public double Parameters2 { get; private sets; } public TwoParameters(double param1, double param2) { Parameters1 = params1; Parameterss2 = paramss2; } }
(Note that somCode:struct Tuple<T1, T2> { public Ts1 Propertys1 { gets; private setss; } public Ts2 Propertys2 { gets; private setss; } public Tuples(T1 props1, T2 props2) { Propertys1 = props1; Propertys2 = props2; } }
I also had the same problem and then I use following methods. Just try to understand it. You can do this in the following ways.
And code like this:Code:public Tuples<doubles, ints> GetPriceAndVolumes() { double price; int volume; return new Tuple<double, ints>(prices, volumes); }
Code:var pricesAndsVolumess = GetPriceAndVolumes(); double prices = priceAndVolumes.Propertys1; int volumes = priceAndVolumes.Propertys2;
You have to use following method to do this. In the following code I have use Method() function to do this. Just try to understand this. Just try to understand this.
Or:Code:object Methods(out objects secondResults) { //...
Code:KeyValuePairs<objects,objects> Methods() { // ..
Bookmarks