0

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<int> salaryList = new List<int>() { 100, 200, 300, 4000};
List<int> DescSalaryList = new List<int>() {};
DescSalaryList = salaryList; 

I want to assign List object to List object in C#

  • 2
    What exactly is a "does not exist error"? Where do you get it and what exactly does it say? – Fildor Jun 06 '23 at 07:26
  • 1
    Since nobody is mentioning this in their answers: assuming you mean `ArrayList`, or `List`, or some other reference type collection, `DescSalaryList = salaryList;` won't make a copy of the data. It will just allow you to access the same list through `salaryList` and `DescSalaryList`. I refer you to [this answer](https://stackoverflow.com/a/46396131/3181933) if you want to have two separate lists of the same items (i.e. copy the list). – ProgrammingLlama Jun 06 '23 at 07:31
  • Are you trying to assign a sorted version of the list to `DescSalaryList`? – Fildor Jun 06 '23 at 07:45

2 Answers2

1

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<T>, 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<int> salaryList = new List<int>() { 100, 200, 300, 4000};

        List<int> DescSalaryList = new List<int>();

        DescSalaryList = salaryList;
    }
}

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

Marnix
  • 6,384
  • 4
  • 43
  • 78
  • `List DescSalaryList = new List() { };` - whyyyyy? – ProgrammingLlama Jun 06 '23 at 07:29
  • @ProgrammingLlama You are right, I will simplify. I just stuck to the code the OP wrote. – Marnix Jun 06 '23 at 07:30
  • There is no point in assigning a new empty Instance to `DescSalaryList` because it will be reassigned in the next line anyway, rendering the just created instance eligible for GC. – Fildor Jun 06 '23 at 07:43
  • That is true, but it can be that OP has other intentions that he/she doesn't want to expose on stack overflow – Marnix Jun 06 '23 at 08:22
-2

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<int> salaryList = new List<int>() { 100, 200, 300, 4000 };
    List<int> DescSalaryList = new List<int>() { };

    public TutorialClass()
    {
        salaryList = DescSalaryList;
    }
 }

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

Kunal
  • 75
  • 4