Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从方法返回信息_C# - Fatal编程技术网

C# 如何从方法返回信息

C# 如何从方法返回信息,c#,C#,我想从两个方法(学生和教师)获取信息,并将信息发送到另一个方法PrintStudentDetails-以打印到控制台-但是如何将字符串传递到打印方法?我试着把线传回去,但我没能让它工作 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment3 { class Progra

我想从两个方法(学生和教师)获取信息,并将信息发送到另一个方法PrintStudentDetails-以打印到控制台-但是如何将字符串传递到打印方法?我试着把线传回去,但我没能让它工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment3
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInfo();

        //    PrintStudentDetails();
        }   

        static void GetStudentInfo()
        {
            // Get student details
          Console.WriteLine("Enter the student's first name: ");
          string sfirst = Console.ReadLine();
          Console.WriteLine("Enter the student's last name");
          string lastName = Console.ReadLine();
          Console.WriteLine("Enter the students birthday");
          string studentsBirthday = Console.ReadLine();

        }

        static void GetTeacherInfo()
        {
            // Code to get teacher name, course, program and degree
            Console.WriteLine("Enter the teachers first name: ");
            string teacherfirstName = Console.ReadLine();
            Console.WriteLine("Enter name of course: ");
            string teacherlastName = Console.ReadLine();
            Console.WriteLine("Enter program: ");
            string programName = Console.ReadLine();
            Console.WriteLine("Enter Degree: ");
            string degreeName = Console.ReadLine();            
        }

        static void PrintStudentDetails(string first, string last, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }            
    }
}

您必须使用
return
类型作为
string
或任何其他要从函数返回值的数据类型

目前,您的函数属于
void
类型。也就是说,他们什么也不回。如果要从函数中获取字符串值,请将其声明为
string
类型并返回字符串值

比如:

static string GetTeacherInfo()
        {
            // Code to get teacher name, course, program and degree
            Console.WriteLine("Enter the teachers first name: ");
            string teacherfirstName = Console.ReadLine();
            Console.WriteLine("Enter name of course: ");
            string teacherlastName = Console.ReadLine();
            Console.WriteLine("Enter program: ");
            string programName = Console.ReadLine();
            Console.WriteLine("Enter Degree: ");
            string degreeName = Console.ReadLine();            
            return degreeName;
        }

希望这能有所帮助。

嗨,我猜您也在关注EDX DEV2014x MOOC;-)

即使对非初学者来说,这些说明也不那么容易理解。 只要调用你的方法

PrintStudentDetails(string first, string last, string birthday)
从方法上

static void GetStudentInfo()

祝您好运完成赋值

完成赋值的典型方法是使用
return
语句从函数返回收集的数据。由于您正在收集多个数据项,因此需要以某种方式将它们捆绑起来

处理此问题的一种方法是使用具有每个数据项属性的
,然后从函数返回该类的实例。(我将在下面展示学生信息的示例;教师信息是分析性的。)

class StudentInfo
{
    public string firstName;
    public string lastName;
    public string birthday;
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo);
}
然后,可以使用函数
GetStudentInfo()
创建并返回类
StudentInfo
的实例:

static StudentInfo GetStudentInfo()
{
  // Get student details
  StudentInfo studentInfo = new StudentInfo();

  Console.WriteLine("Enter the student's first name: ");
  studentInfo.firstName = Console.ReadLine();
  Console.WriteLine("Enter the student's last name");
  studentInfo.lastName = Console.ReadLine();
  Console.WriteLine("Enter the students birthday");
  studentInfo.birthday = Console.ReadLine();

  return studentInfo;
}
打印函数可以保持原样,要求调用方从
StudentInfo
实例中提取属性:

static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    Console.ReadLine();
}
可以这样称呼:

class StudentInfo
{
    public string firstName;
    public string lastName;
    public string birthday;
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo);
}
或者,您可以将
StudentInfo
直接传递到打印功能:

static void PrintStudentDetails(StudentInfo info)
{
    Console.WriteLine("{0} {1} was born on: {2}", info.firstName, info.lastName, info.birthday);
    Console.ReadLine();
}
这样称呼它:

class StudentInfo
{
    public string firstName;
    public string lastName;
    public string birthday;
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);
}
static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo);
}
或者更简单地说,无需创建中间
studentInfo
变量:

static void Main(string[] args)
{
    PrintStudentDetails(GetStudentInfo());
}
请注意,由于函数重载,实际上可以同时使用两个版本的
PrintStudentDetails()
。C#将根据参数类型调用正确的函数:

static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    // using separate arguments...
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);

    // or passing a StudentInfo instance...
    PrintStudentDetails(GetStudentInfo());
}

尽管如此,还可以使用其他数据结构来实现与类相同的效果,例如键/值映射、列表、元组或类似的数据结构。

谢谢!就这样!只有一个问题,我想返回3个字符串,但在返回teacherfirstName时检测到无法访问的代码;返回teacherlastName;返回degreeName;之后3
return
语句在单个代码块中不起作用。这是因为执行是在遇到第一个return语句后返回的。所以基本上是一对一的关系。一个函数-一个返回语句。如果要从一个代码块返回所有3个,可能需要将它们连接成单个字符串,或者创建一个
string
类型的数组并返回该类型。不,字符串连接很糟糕。而是使用数据结构(例如类、映射等)将单个字符串收集到一个对象中,并从函数返回。谢谢!!,是的,我不知道他们想要什么。现在就到,谢谢!6-从每个Get方法中调用Print方法。不,这不是一个好的形式。如果要直接从collection函数调用print函数,为什么不直接打印collection函数中的值呢?如果无法从收集函数中获取收集到的值,则打印函数将不会有用,因为它将不可重用。@RboDev:这是什么EDX DEV2014x MOOC东西?这是C#在线编程课程吗?看起来很有趣。