当在C#中使用consol打印变量时,为什么它为空? [英] Why is it empty when the variable is printed in consol in C#?

查看:70
本文介绍了当在C#中使用consol打印变量时,为什么它为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想整理打印价值。但是值是1,但是当我调试时它是0.

并且屏幕不打印。



这可能是因为我歪曲了变量。



我的尝试:



I want to consol print in value. But value is 1, but when I debug it is 0.
And the screen does not print.

This may be because I misrepresent the variable.

What I have tried:

Students.cs

public List<string> students= new List<string>();

        public static string Stud()
        {
            List<string> students = new List<string>();
            students.Add("Mary");
        }   



Program.cs

static void Main(string[] args)
        {
           Stud_Information.Stud();
            for (var i = 0; i < students.Count; i++)
            {
                Console.WriteLine("[" + i.ToString() + "] " + students[i] );
            }
            Console.ReadKey();
}





输出: //空白



但我想输出: Mary



Output: //empty

But I want to output: Mary

推荐答案

这里有很多问题:

1)

There are a number of problems here:
1)
public List<string> students= new List<string>();

        public static string Stud()
        {
            List<string> students = new List<string>();
            students.Add("Mary");
        }

你有两个具有相同名称的变量 - 学生 - 而在Stud方法中声明的那个变量掩盖了第一级。因此,您对方法内部的学生所做的任何操作都不会影响类级别 - 因为它是在方法体内声明的,所以它是一个局部变量,并在方法退出时被丢弃。正如F-ES Sitecore所说,你根本不需要在方法中声明学生,而是使用类级版本。除了......

2)Stud是一个 static 方法,这意味着它无法访问任何基于实例的类成员 - 以及类级别集合学生不是 static 所以它是一个实例变量,只能通过实例方法访问,并且可以通过实例而不是类名来访问。

3)您无法直接从 Main 方法访问学生,因为它位于不同的类中:

You have two variables with the same name - students - and the one declared inside the Stud method "masks" the class level one. So any operations you do to students inside the method does not affect the class level one - and because it is declared inside the body of the method, it is a local variable and will be discarded when the method exits. As F-ES Sitecore says, you would need to not declare students within the method at all, use the class level version instead. Except ...
2) Stud is a static method, which means it cannot access any instance based members of the class - and the class level collection students is not static so it is an instance variable and can only be accessed via instance methods, and via an instance rather than the class name.
3) You can't access students directly from your Main method becuase it is in a different class:

static void Main(string[] args)
        {
           Stud_Information.Stud();
            for (var i = 0; i < students.Count; i++)
            {
                Console.WriteLine("[" + i.ToString() + "] " + students[i] );
            }
            Console.ReadKey();}

即使您已添加,也无法在Main中提供学生!

4)你的代码不会编译显示,因为你声明Stud返回一个字符串,但实际上并没有返回任何值!



所以,试试这个:

students just isn't available at all in Main, even if you had added to it!
4) Your code won't compiles shown, because you declare Stud as returning a string, but don't actually return any value!

So, try this:

public List<string> students= new List<string>();
public void Stud()
    {
    students.Add("Mary");
    }



Program.cs


Program.cs

 static void Main(string[] args)
    {
    Stud_Information si = new Stud_Information();
    si.Stud();
    for (var i = 0; i < si.students.Count; i++)
        {
        Console.WriteLine("[" + i.ToString() + "] " + si.students[i] );
        }
    Console.ReadKey();
}


我认为代码甚至不会编译,但在Stud中你定义了一个名为学生的变量是该方法的本地方法,仅存在于该方法中。当该方法停止执行变量并且其所有数据都被销毁时。您可能打算将Stud附加到您的全球列表中,也称为学生;



I don't think that code will even compile, but in Stud you define a variable called "students" that is local to that method and only exists inside that method. When that method stops executing the variable and all of its data is destroyed. You probably intend Stud to append to your global list also called students;

public List<string> students= new List<string>();

public static string Stud()
{
    students.Add("Mary");
} 


这篇关于当在C#中使用consol打印变量时,为什么它为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆