当前上下文中不存在名称“...”

The name '...' does not exist in the current context

我的 Main() 中有一个列表,我正在尝试从一个变量中向该列表添加一个项目。但它抛出错误 "The name 'dogList' does not exist in the current context"

在我的 addDog() 方法中,dogList.Add() 由于上述原因无法正常工作。

namespace DoggyDatabase
{
    public class Program
    {
          public static void Main(string[] args)
        {
        // create the list using the Dog class
        List<Dog> dogList = new List<Dog>();

        // Get user input
        Console.WriteLine("Dogs Name:");
        string inputName = Console.ReadLine();
        Console.WriteLine("Dogs Age:");
        int inputAge = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Dogs Sex:");
        string inputSex = Console.ReadLine();
        Console.WriteLine("Dogs Breed:");
        string inputBreed = Console.ReadLine();
        Console.WriteLine("Dogs Colour:");
        string inputColour = Console.ReadLine();
        Console.WriteLine("Dogs Weight:");
        int inputWeight = Convert.ToInt32(Console.ReadLine());

        // add input to the list.
        addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);          
    }

    public static void addDog(string name, int age, string sex, string breed, string colour, int weight)
    {
        // The name 'dogList' does not exist in the current context
       dogList.Add(new Dog()
        {
            name = name,
            age = age,
            sex = sex,
            breed = breed,
            colour = colour,
            weight = weight
        });           
    }
}

public class Dog
{
    public string name { get; set; }
    public int age { get; set; }
    public string sex { get; set; }
    public string breed { get; set; }
    public string colour { get; set; }
    public int weight { get; set; }
}

}

dogList 仅存在于 Main 方法的范围内。如果您在一种方法中声明一个变量,它会变成 local 并且不能在另一种方法中访问。

您可以通过将必要的变量作为参数传递来解决它:

public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List<Dog> dogList) 

现在你像这样在调用中传递变量:

// add input to the list.
addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight, dogList);          

或者您可以在 class:

的范围内声明变量
public class Program
{
    // create the list using the Dog class
    static List<Dog> dogList = new List<Dog>();

在后一个版本中你需要将其声明为静态的,否则编译器将要求 class Program 的实例才能访问变量

dogList 是方法 Main 的局部变量。您要做的是将 dogList 放在该范围之外。

public class Program
{
    static List<Dog> dogList = new List<Dog>();

...

或者,您可以将列表发送到您的添加方法中。

dogList 变量在 Main 方法的局部作用域内,因此您的 class 中的其他方法无法访问它,您几乎没有办法使其正确,一种解决方案是将 dogList 以及参数传递给该方法,例如:

 // add input to the list.
 addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight,dogList);

并将 addDog 方法的签名也更改为:

public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List < Dog > dogList) 
{
}

如果您不想这样做,另一种解决方案是将您的 dogList 变量设置为 class 级别,即使其字段如下:

public class Program
{
   List<Dog> dogList = new List<Dog>();  
}

主要问题是您在 main 中本地声明了 dogList。您还将 addDog 声明为静态的。静态方法在当前对象之外。

将 Main 想象成您站在客厅中的客厅。现在把 addDog 想象成我站在里面的浴室。我们知道对方就在那里,所以我们无法沟通。

public class DogDb
{
    // DogDb contains a list of dogs
    public List<Dog> dogs { get; set; }

    public DogDb() {
        dogs = new List<Dog>();
    }
    // DogDb can control adding new dogs to its list of dogs.
    public void addDog(string name, int age, string sex, string breed, string colour, int weight)
    {               

        dogs.Add(new Dog()
        {
            name = name,
            age = age,
            sex = sex,
            breed = breed,
            colour = colour,
            weight = weight
        });
    }

    public class Dog
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
        public string breed { get; set; }
        public string colour { get; set; }
        public int weight { get; set; }
    }
}

public class Program
{
      public static void Main(string[] args)
    {

    // Create a new instance of our DogDB class.
    var DogDb = new DogDb();

    // Get user input
    Console.WriteLine("Dogs Name:");
    string inputName = Console.ReadLine();
    Console.WriteLine("Dogs Age:");
    int inputAge = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Dogs Sex:");
    string inputSex = Console.ReadLine();
    Console.WriteLine("Dogs Breed:");
    string inputBreed = Console.ReadLine();
    Console.WriteLine("Dogs Colour:");
    string inputColour = Console.ReadLine();
    Console.WriteLine("Dogs Weight:");
    int inputWeight = Convert.ToInt32(Console.ReadLine());

    // add input to the object.
    DogDb.addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);

}

@Ari....这是你如何做到的

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

namespace ConsoleApplication4
{
    namespace DoggyDatabase
    {
        public class Program
        {
            private static List<Dog> dogList = new List<Dog>();

            public static void Main(string[] args)
            {
                // create the list using the Dog class                

                // Get user input
                Console.WriteLine("Dogs Name:");
                string inputName = Console.ReadLine();
                Console.WriteLine("Dogs Age:");
                int inputAge = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Dogs Sex:");
                string inputSex = Console.ReadLine();
                Console.WriteLine("Dogs Breed:");

                string inputBreed = Console.ReadLine();
                Console.WriteLine("Dogs Colour:");
                string inputColour = Console.ReadLine();
                Console.WriteLine("Dogs Weight:");
                int inputWeight = Convert.ToInt32(Console.ReadLine());

                // add input to the list.
                addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);
            }

            public static void addDog(string name, int age, string sex, string breed, string colour, int weight)
            {
                // The name 'dogList' does not exist in the current context
                dogList.Add(new Dog()
                {
                    name = name,
                    age = age,
                    sex = sex,
                    breed = breed,
                    colour = colour,
                    weight = weight
                });
            }
        }

        public class Dog
        {
            public string name { get; set; }
            public int age { get; set; }
            public string sex { get; set; }
            public string breed { get; set; }
            public string colour { get; set; }
            public int weight { get; set; }
        }
    }
}

列表由于其保护而无法访问level.When你必须在另一个方法中使用列表然后你必须声明它first.Happy编码