无法将 list<int> 对象分配给 C# 中的 list<int> 对象。

huangapple go评论60阅读模式
英文:

unable to assign list<int> object to list<int> object in c#

问题

第3行出现了'不存在'错误。请帮助我解决这个问题。提前感谢。

List<int> salaryList = new List<int>() { 100, 200, 300, 4000};
List<int> DescSalaryList = new List<int>() {};
DescSalaryList = salaryList;

我想在C#中将List对象分配给另一个List对象。

英文:

I tried below Code. But in the 3rd line , I am getting 'does not exist' error. Please help me to solve this. Thanks in advance.
Image of the code

List&lt;int&gt; salaryList = new List&lt;int&gt;() { 100, 200, 300, 4000};
List&lt;int&gt; DescSalaryList = new List&lt;int&gt;() {};
DescSalaryList = salaryList; 

I want to assign List<int> object to List<int> object in C#

答案1

得分: 1

List 本身不存在。如果您不确定列表中包含的对象类型,您可以使用 System.Collections.ArrayList。如果您确定它们是 int,您可以使用 System.Collections.Generic.List&lt;T&gt;,其中您必须像下面这样指定对象的类型:

using System;
using System.Collections.Generic;
					
public class Program
{
    public static void Main()
    {
        List&lt;int&gt; salaryList = new List&lt;int&gt;() { 100, 200, 300, 4000};

        List&lt;int&gt; DescSalaryList = new List&lt;int&gt;();

        DescSalaryList = salaryList;
    }
}

还要别忘了在文件顶部添加正确的 using

英文:

List by itself does not exist. If you are unsure about the types of objects that are in your list, you can make use of System.Collections.ArrayList. If you are sure that they are int, you can make use of System.Collections.Generic.List&lt;T&gt;, where you have to specify the type of the objects like so:

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		List&lt;int&gt; salaryList = new List&lt;int&gt;() { 100, 200, 300, 4000};

		List&lt;int&gt; DescSalaryList = new List&lt;int&gt;();

		DescSalaryList = salaryList;
	}
}

Also, don't forget to add the correct using at the top of your file.

答案2

得分: -2

如果考虑类(基本上),它由变量和函数组成,变量可由类函数或类引用访问。访问修饰符也在其中起作用。私有变量只能由自身类访问等等。通过创建引用,您正在分配内存。下面的代码对于相同的事情是有效的。

var obj = new TutorialClass();
obj.DescSalaryList = obj.salaryList

或者您可以尝试使用构造函数(如下面的代码)或创建任何操作变量的方法。在类内部,您只能通过方法访问变量。对于面向对象编程,编译器会强制执行这一点,以进行其内部机制的操作。

public class TutorialClass 
{
    List<int> salaryList = new List<int>() { 100, 200, 300, 4000 };
    List<int> DescSalaryList = new List<int>() { };

    public TutorialClass()
    {
        salaryList = DescSalaryList;
    }
}

如果您注意到,我们正在使用构造函数(另一个函数/方法)来访问变量。

英文:

If think of class (fundamentally) it consist of variables and functions and variables are accessible by class functions or class reference. Access specifiers also play role in it. private variables only accessible by own class and etc.
By creating reference you are assigning the memory. Below code is valid for same thing.

var obj = new TutorialClass();
obj.DescSalaryList = obj.salaryList

or you can try using contractor (like below code) or make any method that play with variables. Within class you can access the variable by only methods. For object oriented programming, it something compilers enforce it for it's internal mechanism

public class TutorialClass 
{
    List&lt;int&gt; salaryList = new List&lt;int&gt;() { 100, 200, 300, 4000 };
    List&lt;int&gt; DescSalaryList = new List&lt;int&gt;() { };

    public TutorialClass()
    {
        salaryList = DescSalaryList;
    }
 }

If you notice we are using contractor (another function/method) to access variable.

huangapple
  • 本文由 发表于 2023年6月6日 15:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定