如何在C#方法中打印多个值

本文关键字:打印 方法 | 更新日期: 2023-09-27 18:01:09

我想通过PrintStudentDetails(字符串第一,字符串最后,字符串生日(方法打印GetStudentInformation((方法中的值。请查看以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Module3_HomeWork
{
    class Program
    {
        static void GetStudentInformation()
        {
            Console.WriteLine("Enter the student's first name: ");
            string firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            string lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthdate: ");
            DateTime birthdate = Convert.ToDateTime(Console.ReadLine());
            Console.WriteLine("Enter the student's address line 1: ");
            string add1 = Console.ReadLine();
            Console.WriteLine("Enter the student's address line 2: ");
            string add2 = Console.ReadLine();
            Console.WriteLine("Enter the student's city: ");
            string city = Console.ReadLine();
            Console.WriteLine("Enter the student's state: ");
            string state = Console.ReadLine();
            Console.WriteLine("Enter the student's zip code: ");
            int zip = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the student's country: ");
            string country = Console.ReadLine();
        }
        static void PrintStudentDetails(string first, string last, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
        }
        static void Main(string[] args)
        {
            GetStudentInformation();
            PrintStudentDetails(first, last, birthday);
        }
        public static string first { get; set; }
        public static string last { get; set; }
        public static string birthday { get; set; }
    }
}

如何只使用方法而不使用类打印值?我对它进行了研究,发现了outref,任何帮助或指导都将不胜感激。

问候

如何在C#方法中打印多个值

如果你只想输入名字、姓氏和出生日期,然后输出它们,你可以更改以下三行:

string firstName = Console.ReadLine();
string lastName = Console.ReadLine();
DateTime birthdate = Convert.ToDateTime(Console.ReadLine());

到以下位置:

first = Console.ReadLine();
last = Console.ReadLine();
birthday = Convert.ToDateTime(Console.ReadLine());

那么你的程序应该按照你的期望运行。

但既然你提到了outref,也许你真正想要的是以下内容:

class Program
{
    static void GetStudentInformation(out string first, out string last, out string birthday)
    {
        Console.WriteLine("Enter the student's first name: ");
        first = Console.ReadLine();
        Console.WriteLine("Enter the student's last name: ");
        last = Console.ReadLine();
        Console.WriteLine("Enter the student's birthdate: ");
        birthday = Convert.ToDateTime(Console.ReadLine());
    }
    static void PrintStudentDetails(string first, string last, string birthday)
    {
        Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    }
    static void Main(string[] args)
    {
        string first, last, birthday;
        GetStudentInformation(out first, out last, out birthday);
        PrintStudentDetails(first, last, birthday);
    }
}

我不太确定您所说的只通过方法而不是类打印值是什么意思。但我想下面的例子可以帮助你们。

void PrintDetails(ref string s1, ref string s2, ref string s3)
{ 
  s1 = "Some text goes here";
  s2 = myNumber.ToString();
  s3 = "some more text";
}

现在,在调用此函数时,只需使用以下语法:

public void SomeOtherFunction()
{
  string sMyValue1 = null;
  string sMyValue2 = null;
  string sMyValue3 = null;
  PrintDetails(sMyValue1, sMyValue2, sMyValue3);
  //Now you have all the values inside your local variable. 
}

如果您真的希望方法返回多个值,那么使用outref关键字将有助于您从方法返回多值,但使用属性可能是更好的解决方案。无论如何,这就是使用out关键字时的样子:

static void GetStudentInformation(out string firstName, out string lastName, out DateTime birthDate,
    out string add1, out string add2, out string city, out string state, out int zip, out string country)
{
    Console.WriteLine("Enter the student's first name: ");
    firstName = Console.ReadLine();
    Console.WriteLine("Enter the student's last name: ");
    lastName = Console.ReadLine();
    Console.WriteLine("Enter the student's birthdate: ");
    birthDate = Convert.ToDateTime(Console.ReadLine());
    Console.WriteLine("Enter the student's address line 1: ");
    add1 = Console.ReadLine();
    Console.WriteLine("Enter the student's address line 2: ");
    add2 = Console.ReadLine();
    Console.WriteLine("Enter the student's city: ");
    city = Console.ReadLine();
    Console.WriteLine("Enter the student's state: ");
    state = Console.ReadLine();
    Console.WriteLine("Enter the student's zip code: ");
    zip = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter the student's country: ");
    country = Console.ReadLine();
}
static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}
static void Main(string[] args)
{
    string firstName, lastName, add1, add2, city, state, country;
    DateTime birthDate;
    int zip;
    GetStudentInformation(out firstName, out lastName, out birthDate, out add1, out add2, out city, out state, out zip, out country);
    PrintStudentDetails(firstName, lastName, birthDate.ToShortDateString());
}

您也可以将所有out关键字替换为ref关键字,您将获得相同的效果。您只需要将变量初始化为某个值,然后通过ref将它们传递给GetStudentInformation方法。