locked
Tuple modifies return parameter names RRS feed

  • Question

  • User572431586 posted

    Hi,

    I have a ApiController that returns a list of Tuples in JSON format.

        public class MyApiController : ApiController
        {
            public IEnumerable<Tuple<string, int, string>> Get(string id)
            {
                return new MyClass().TheMethod();
            }
        }

    When I receive the values the parameters has the name: "m_Item1", "m_Item2", "m_Item3".

    Where is the "m_" coming from? Shouldn't the name be "Item1", "Item2"...?

    It doesn't seems such a big problem but I just wanna make sure that the parameter names aren't gonna be changed later.

    Monday, December 21, 2015 12:13 PM

Answers

  • User-2057865890 posted

    Hi,AsusT9

    According to your description,I suggest you could create a class named "MyItem" instead of Tuple.

    Please try the following.

    public class MyApi2Controller : ApiController
        {
            public IEnumerable<MyItem> Get2(string id)
            {
                return new MyClass().TheMethod2();
            }
        }
    
    public class MyItem
        {
            public string Item1 { get; set; }
            public int Item2 { get; set; }
            public string Item3 { get; set; }
        }
    
    public class MyClass
        {
            public List<MyItem> TheMethod2 ()
            {
                var list = new List<MyItem>
                {
                    new MyItem {Item1="cat",Item2=1,Item3="cat" },
                    new MyItem {Item1="apple",Item2=100,Item3="apple" },
                    new MyItem {Item1="zebra",Item2=2,Item3="zebra" }
                };
                return list;
            }
        }

    Best regards,
    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 23, 2015 8:33 AM

All replies

  • User-2057865890 posted

    Hi,AsusT9

    According to your description,I suggest you could create a class named "MyItem" instead of Tuple.

    Please try the following.

    public class MyApi2Controller : ApiController
        {
            public IEnumerable<MyItem> Get2(string id)
            {
                return new MyClass().TheMethod2();
            }
        }
    
    public class MyItem
        {
            public string Item1 { get; set; }
            public int Item2 { get; set; }
            public string Item3 { get; set; }
        }
    
    public class MyClass
        {
            public List<MyItem> TheMethod2 ()
            {
                var list = new List<MyItem>
                {
                    new MyItem {Item1="cat",Item2=1,Item3="cat" },
                    new MyItem {Item1="apple",Item2=100,Item3="apple" },
                    new MyItem {Item1="zebra",Item2=2,Item3="zebra" }
                };
                return list;
            }
        }

    Best regards,
    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 23, 2015 8:33 AM
  • User572431586 posted

    Hi,

    Yes could of course create a separate class for the return values. But my question was "Where is the "m_" coming from?"

    Isn't the "m_" mark coming from the private variables from the Tuple?

    Wednesday, December 30, 2015 8:10 PM