How to print multiple values in C# method?

10,665

Solution 1

If all you want is to input first name, last name and birth date and then output them, you can change the following three lines:

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

to the following:

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

then your program should run as you expect.

But since you mentioned out and ref, perhaps what you really want is the following:

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);
    }
}

Solution 2

I'm not quite sure what did you mean by print value only by method not class. But I guess below example can help you out.

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

Now while calling this function just use below syntax:

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. 
}
Share:
10,665
MOZ
Author by

MOZ

Updated on June 04, 2022

Comments

  • MOZ
    MOZ almost 2 years

    I want to print values from GetStudentInformation() method through PrintStudentDetails(string first, string last, string birthday) method. Please view the code below:

    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; }
        }
    }
    

    How can i print values using only methods not class? I've researched on it and found out about out and ref , any help or guidance will be much appreciated.

    Regards