如何在c#中调用接受多个参数的方法

本文关键字:参数 方法 调用 | 更新日期: 2023-09-27 18:07:49

我是c#的新手,我在这个小程序中有一个问题我想在方法ClientsDetails中返回输入的信息,以便在方法Print()中使用它们。有什么帮助吗?

public static void Main(string[] args)
    {
        ClientsDetails();
        Print(???,???,???);
        Console.ReadLine();

    }
    public static void ClientsDetails()
    {
        Console.Write("Client's first name: ");
        string firstName = Console.ReadLine();
        Console.Write("Client's last name: ");
        string lastName = Console.ReadLine();
        Console.Write("Client's birthdate: ");
        string birthday = Console.ReadLine();
    }
    public static void Print(string first, string last, string birthday)
    {
        Console.WriteLine("Client : {0} {1} was born on: {2}", first, last, Birthday);
    }
}

如何在c#中调用接受多个参数的方法

可以使用struct:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    struct Person{
        public static FirstName;
        public static LastName;
        public static Birthday;
    }
    class Program
    {
        static void Main(string[] args)
        {                
            Person person = ClientsDetails();
            Print(person.FirstName, person.LastName, person.Birthday);
            Console.ReadKey();
        }
        public static Person ClientsDetails()
        {
            Person returnValue = new Person();
            Console.Write("Client's first name: ");
            returnValue.FirstName = Console.ReadLine();
            Console.Write("Client's last name: ");
            returnValue.LastName = Console.ReadLine();
            Console.Write("Client's birthdate: ");
            returnValue.Birthday = Console.ReadLine();
            return returnValue;
        }
        public static void Print(string first, string last, string birthday)
        {
            Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
        }
    }
}

或数组:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {                
            string person = ClientsDetails();
            Print(person[0], person[1], person[2]);
            Console.ReadKey();
        }
        public static string[] ClientsDetails()
        {
            string[] returnValue = new string[3];
            Console.Write("Client's first name: ");
            returnValue[0] = Console.ReadLine();
            Console.Write("Client's last name: ");
            returnValue[1] = Console.ReadLine();
            Console.Write("Client's birthdate: ");
            returnValue[3] = Console.ReadLine();
            return returnValue;
        }
        public static void Print(string first, string last, string birthday)
        {
            Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
        }
    }
}

或引用(按引用传递):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {       
            string firstName, lastName, birthday;
            ClientsDetails(ref firstName, ref lastName, ref birthday);
            Print(firstName, lastName, birthday);
            Console.ReadKey();
        }
        public static void ClientsDetails(ref string firstName, ref string lastName, ref string birthday)
        {
            Console.Write("Client's first name: ");
            firstName = Console.ReadLine();
            Console.Write("Client's last name: ");
            lastName = Console.ReadLine();
            Console.Write("Client's birthdate: ");
            birthday = Console.ReadLine();
        }
        public static void Print(string first, string last, string birthday)
        {
            Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
        }
    }
}

我刚刚按照你的要求修改了你的程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        private static String FirstName;
        private static String LastName;
        private static String Birthday;
        static void Main(string[] args)
        {
            ClientsDetails();
            Print(FirstName, LastName, Birthday);
            Console.ReadKey();
        }
        public static void ClientsDetails()
        {
            Console.Write("Client's first name: ");
            FirstName = Console.ReadLine();
            Console.Write("Client's last name: ");
            LastName = Console.ReadLine();
            Console.Write("Client's birthdate: ");
            Birthday = Console.ReadLine();
        }
        public static void Print(string first, string last, string birthday)
        {
            Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, Birthday));
        }
    }
}

有很多方法可以将所需的参数传递给方法,例如,您可以这样做:

String f = "first string";
String l = "last string";
String b = "birthday string";
Print(f,l,b);

BTW,在你的情况下,似乎你想要用户的输入传递给Print方法,所以一个简单的方法是在你的ClientsDetails方法中调用Print方法,像这样:

Print(firstName, lastName, birthday);

有关这方面的全面资源,您可以像往常一样参考文档。此时您可以忽略Async Methods部分。