none
many Method arguments with one type reference RRS feed

  • Question

  • I know my headline might not be as simple as the question really is:

    Im advanced in programming with c#, but I do not know if there is any way to shorten method heads concerning the arguments I want to pass into the method.

    So if I have a class HuiHuiHuiHuiHui (without any context), and I want to pass an object of this class into a method, I would do it like this: public void Example(HuiHuiHuiHuiHui x, HuiHuiHuiHuiHui y){}

    My question is, is there a way to pass in x,y without having to write HuiHui... twice, like one can declare 2 variables with "string x,y";

    I am sorry for the bad example but I hope I have might my problem clear and one of you knows the answer to my question.

    Tuesday, October 1, 2019 3:33 PM

Answers

  • Perhaps a language extension e.g.

    namespace SomeNamespace
    {
        public class Person
        {
            public int Identifier { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
        public static class Extensions
        {
            public static void Example(this Person sender, Person person)
            {
                // TODO
            }
        }
    
        public class Demo
        {
            public void ExampleOne()
            {
                var p1 = new Person()
                    { Identifier = 1, FirstName = "Karen", LastName = "Payne"};
    
                var p2 = new Person()
                    { Identifier = 2, FirstName = "Mary", LastName = "Smith"};
    
                p1.Example(p2);
                
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by NilsSharp Tuesday, October 1, 2019 5:46 PM
    Tuesday, October 1, 2019 4:07 PM
    Moderator
  • If you want to pass any number of objects of this type, then you write the type once:

       public void Example( params HuiHuiHuiHuiHui [ ] p)  

    Calls:

       Example( a );

       Example( a, b, c );

       Example( );


    • Edited by Viorel_MVP Tuesday, October 1, 2019 5:37 PM
    • Marked as answer by NilsSharp Tuesday, October 1, 2019 6:05 PM
    Tuesday, October 1, 2019 5:36 PM

All replies

  • Perhaps a language extension e.g.

    namespace SomeNamespace
    {
        public class Person
        {
            public int Identifier { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
        public static class Extensions
        {
            public static void Example(this Person sender, Person person)
            {
                // TODO
            }
        }
    
        public class Demo
        {
            public void ExampleOne()
            {
                var p1 = new Person()
                    { Identifier = 1, FirstName = "Karen", LastName = "Payne"};
    
                var p2 = new Person()
                    { Identifier = 2, FirstName = "Mary", LastName = "Smith"};
    
                p1.Example(p2);
                
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by NilsSharp Tuesday, October 1, 2019 5:46 PM
    Tuesday, October 1, 2019 4:07 PM
    Moderator
  • If you want to pass any number of objects of this type, then you write the type once:

       public void Example( params HuiHuiHuiHuiHui [ ] p)  

    Calls:

       Example( a );

       Example( a, b, c );

       Example( );


    • Edited by Viorel_MVP Tuesday, October 1, 2019 5:37 PM
    • Marked as answer by NilsSharp Tuesday, October 1, 2019 6:05 PM
    Tuesday, October 1, 2019 5:36 PM
  • So no standard c# keyphrase to do the job. I thought so. 

    The Extension class works grat though, thanks for the quick answer!


    Tuesday, October 1, 2019 5:48 PM