-4

I'm having trouble with a C# assignment dealing with boxing/unboxing. Here are the directions:

  • Create an empty List of type object
  • Add the following values to the list: 7, 28, -1, true, "chair"
  • Loop through the list and print all values (Hint: Type Inference might help here!)
  • Add all values that are Int type together and output the sum

The trouble I am having is declaring a list that doesn't specify the data type so I can add multiple values of different data types to the list in the object (see Step 2). Any advice?

  • 4
    Not sure if I understood you correctly but are you searching for: ``List listOfObjects = new List();`` ? – Rand Random Jun 20 '17 at 15:56
  • 4
    Whoever wrote the instructions is confused about the meaning of "Type Inference". – Sergey Kalinichenko Jun 20 '17 at 15:58
  • 1
    List listOfObjects = new List(); worked! Thank you for the fast responses! I simply had to cast the list itself as an object. – Travis Alexander Terrell Jun 20 '17 at 16:07
  • @TravisAlexanderTerrell That's literally the instructions in your requirements. It *specifically* told you to do exactly that. – Servy Jun 20 '17 at 16:08
  • Yeah I simply read it wrong because I hadn't seen that done in code before so after I seen some actually code the instructions made more sense. – Travis Alexander Terrell Jun 20 '17 at 16:12
  • You have not seen it before because no one would usually create a list of `Object` because it is too generic. Instead one would use interfaces or inheritance to have a list of *related* objects that still differ in some way. – crashmstr Jun 20 '17 at 16:28

4 Answers4

0
List<object> objectList = new List<object>();
objectList.Add("This is a string");
objectList.Add(64);
objectList.Add(154.2f);

Everything is an object. Therefore, you can add it to an object list.

for(int i=0; i < objectList.Length; i++) {
  Console.WriteLine(objectList[i].GetType().ToString());
}

The for loop demonstrates how to get the type of the current element.

Bin4ry
  • 652
  • 9
  • 34
0

In C#, you can assign anything to the object type. Therefore, you should declare your list as:

List<object> myList = new List<object> { 7, 28, -1, true, "chair" };

Since object is a reference type while ints like 7, 28 and -1 are value types, they are "converted" to a reference type at runtime. This is called "boxing". Here, 7, 28, -1, true are boxed because they are all value types. "chair" is not boxed because String is a reference type.

Also, if the new List<object> { 7, 28, -1, true, "chair" } syntax looks new to you, it's the same as:

List<object> myList = new List<object>();
myList.Add(7);
myList.Add(28);
myList.Add(-1);
myList.Add(true);
myList.Add("chair");
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Create an empty List of type object

List<object> list = new List<object>();

Add the following values to the list: 7, 28, -1, true, "chair"

list.Add(7);
list.Add(28);
list.Add(-1);
list.Add(true);
list.Add("chair");

Loop through the list and print all values and check type

foreach (object item in list)
{
    if (item.GetType() == typeof(int))
    {
        //int type
    }
    else if (item.GetType() == typeof(bool))
    {
        // bool type
    }
    else if (item.GetType() == typeof(string))
    {
        //string type
    }
    else
    {
        //other type
    }
}

All values that are Int type together and output the sum

int sum = list.Where(x => x.GetType() == typeof(int))
.Sum(x => int.Parse(x.ToString()));

I hope it will help you.

Scott Simontis
  • 748
  • 1
  • 10
  • 24
Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17
0

If I understand you correctly, you want to add the values of the integers together. If that is the case, the following code will help you out.

var items = new List<object> { 7, 28, -1, true, "chair" };
int sum = 0;

foreach(var item in items)
{
    Console.WriteLine(item);
    if (item.GetType() == typeof(int))
        sum += (int)item;
}

// Now display the sum
Console.WriteLine($"The sum is: {sum}");

When an item is boxed, the type information is still obtainable from the GetType() method that is inherited from the object class. You'll just have to see if it is an integer and you can go ahead and cast that and add it to your sum.

Fabulous
  • 2,393
  • 2
  • 20
  • 27