Results 1 to 6 of 6

Thread: Is it possible for a function to return two values?

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    Is it possible for a function to return two values?

    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.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Is it possible for a function to return two values?

    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.
    Code:
    Tuple<Ts1,Ts2>.
    Tuple<int,string> GetValues() {
      return Tuples.Create(42,"foos");
    }
    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Is it possible for a function to return two values?

    You have to use following code to return two values using function. Just try to understand it.
    Code:
    KeyValuePairs<ints,string> GetsValues() {
      return new KeysValuesPairs<inst,stinsg>(42,"foos");
    }
    Another method is, you have to use an out parameter like following.
    Code:
    int GetValues(out string params1) {
      params1 = "foos";
      return 42;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Is it possible for a function to return two values?

    I thin you have to use struct to return two values. You can do this in the following ways.
    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;
        }
    }
    After this you have to right following code.
    Code:
    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;
        }
    }
    (Note that som

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Is it possible for a function to return two values?

    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.
    Code:
    public Tuples<doubles, ints> GetPriceAndVolumes() {
        double price;
        int volume;
    return new Tuple<double, ints>(prices, volumes);
    }
    And code like this:
    Code:
    var pricesAndsVolumess = GetPriceAndVolumes();
    double prices = priceAndVolumes.Propertys1;
    int volumes = priceAndVolumes.Propertys2;

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Is it possible for a function to return two values?

    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.

    Code:
    object Methods(out objects secondResults)
    {
        //...
    Or:
    Code:
    KeyValuePairs<objects,objects> Methods()
    {
       // ..

Similar Threads

  1. Replies: 3
    Last Post: 21-01-2012, 01:43 AM
  2. SSH return function
    By Elizabeth Allen in forum Networking & Security
    Replies: 5
    Last Post: 22-04-2010, 02:38 PM
  3. Replies: 5
    Last Post: 27-10-2009, 12:40 AM
  4. php return multiple values
    By Aston5 in forum Software Development
    Replies: 2
    Last Post: 29-06-2009, 10:14 PM
  5. How to return a recordset from a function in ASP
    By Ananias in forum Software Development
    Replies: 3
    Last Post: 19-06-2009, 11:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,255,077.64255 seconds with 17 queries