1

I'm new to OOP and C#. I have a strong Python background and I was wondering is there an equivalent in C# for this

#Python

def outputSmth():
    num1 = 3
    num2 = 3
    str1 = "Hi"

    return (num1, num2, str1)  #Returning a tuple that can later be accessed
                               # by index

If there is no direct equivalent, which I doubt there is, what is the most proper way to do it?

Here's my function :

//C#    
static tuple PrintUserCreationMnu()
    {
        Console.Clear();
        Console.WriteLine("----  Create the user ----\n");
        Console.Write("User ID               : "); string usrID = Console.ReadLine();
        Console.Write("First Name            : "); string f_Name = Console.ReadLine();
        Console.Write("Last Name             : "); string l_Name = Console.ReadLine();
        Console.Write("Expected leaving date : "); string l_Date = Console.ReadLine();

        return ; //Here I'd like to return all my inputted values
    }

Thank you!

0

2 Answers 2

2

You could simply return a string[] or List<string>:

static string[] PrintUserCreationMnu()
{
       // ...
    return new[]{ usrID, f_Name, l_Name, l_Date};
}

But in general it would be better to create a custom type User with meaningful properties and return that. Then you can access them via property instead of by index:

static User PrintUserCreationMnu()
{
       // ...
    return new User{ UserId = usrID, FirstName = f_Name, LastName = l_Name, Date = l_Date};
}

Even better would be to use the correct types, int for an Id and DateTime for the date. You can use the ...TryParse methods(f.e. int.TryParse) to ensure that the format is valid.


For the sake of completeness, yes, .NET has also tuples. But i wouldn't use them often because it's not clear what Item4 is. No one knows that before he looks at the source code. So it's not a good type to return from a method(even less if it's public). However, here it is:

static Tuple<string, string, string, string> PrintUserCreationMnu()
{
       // ...
    return Tuple.Create(usrID, f_Name, l_Name, l_Date);
}
Sign up to request clarification or add additional context in comments.

5 Comments

My ID really needs to be a string as IDs are formatted like U001ABCD but yeah I think I will create a User class.
@Gaboik1: note that i've edited my answer to show C# tuples
"You could simply return a string[] or List<string>" - this will not cater for the numeric type in the OP's question without having to add conversions.
@MurrayFoxcroft: sure, you have to parse the strings to the desired type. That's what i've suggested with the User-class and the "correct types"-hint. The .NET4 tuples already support different types, so you could also return Tuple<string, string, string, DateTime> if desired. But your C#7 tuples are better, unfortunately they don't exist now
Yep, living in a dream world. Check my answer for a dynamic option. Still messy tho.
0

Tuples are coming in C# 7 (current is 6.0). So, unfortunately not yet.

I cant wait for them, especially when dealing with methods that return more that one parameter (without having to set up structures to deal with them).

For now you need to return something like a List, Dictionary or a Tuple type but you have to new them up and assemble them in a method call and the break then apart on receiving them from the call. Further, you are limited to a single type in most of these constructs so you would have to box in and out of something like object. A bit of a pain!

You can find more on C# 7.0 features here.

I think your best option is going to be a dynamic expando object. I am afraid it's the best I can do!

Method:

public object GetPerson()
{
    // Do some stuff
    dynamic person = new ExpandoObject();
    person.Name = "Bob";
    person.Surname = "Smith";
    person.DoB = new DateTime(1980, 10, 25);
    return person;
}

Call:

dynamic personResult = GetPerson();
string fullname = personResult.Name + " " + personResult.Surname;
DateTime dob = personResult.DoB;

10 Comments

Yes it's a pain. And thanks for the link! When is C# 7 set to come out?
My rough guess is 6-12 months. Here is a thread discussing related bits: strathweb.com/2016/03/…
Even better - here is a link to the Roadmap! github.com/dotnet/roslyn/blob/master/docs/…
@MurrayFoxcroft: tuples exist since .NET 4. msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx
@TimSchmelter there is a subtle difference between the Tuples you are thinking of and the way they are implemented in Python, which is the behaviour C# 7 will cater for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.