How to return three values from a function in c#?

Posted on 2014-07-17
Last Modified: 2014-07-17
Hi Experts,

I am writing a function which should return three string values and my code looks like this.
private String GetAttDataType(string dt)
             string attDa = null;
             string attDe = null;
           string attDep = null;
            string ConString = "User Id=sys;Password=sys;Data Source=T";
            using (OracleConnection con = new OracleConnection(ConString))
                OracleDataAdapter adapter = new OracleDataAdapter();
                OracleCommand command = new OracleCommand("SELECT ATT_DATATYPE,ATT_DEFVALUE, DONID FROM T.ATTRIBUTES WHERE ATTRIBUTE_NUMBER=" + dt, con);
                    adapter.SelectCommand = command;
                    DataSet dataset = new DataSet("ATTDATATYPE1");
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                        attDefValue = dr["ATT_DEFVALUE"].ToString();
                        attDependsOnID = dr["DONID"].ToString();
            return attDa;

Open in new window

Is there any work around for this? Or Can you please share some ideas on how to resolve it?

Question by:o0JoeCool0o
    LVL 22

    Accepted Solution

    you can return a tuple
    LVL 43

    Expert Comment

    A function can only return one value - but it is up to you what type of object that is, it could be a class.

    An alternative is to use the parameter list (either as ref or as out keywords), see:

    string dt1, dt2, dt3
    GetAttDataType(dt1, dt2, dt3)

    private void GetAttDataType(out string dt1, out string dt2, out string dt3)
    //now fill in the values into dt1, dt2 and dt3
    LVL 14

    Expert Comment

    As p_davis suggested, using a tuple would work (

    If for some reason a tuple doesn't work, you could also
    1. Return an array
    2. Return a string separated by some delimiter such as |and use String.Split (
    3. Change your function to use pass by reference variables ( You would then pass in three additional strings to your function and set those. The code that calls this function would have the values.

    Tuple is best, but providing a few other options just in case. Order preference would probably be tuple, array, delimited string, and, only if you have to, reference variables.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now