Q:

Write a C# Sharp program to remove all the values except integer values from a given array of mixed values

0

Write a C# Sharp program to remove all the values except integer values from a given array of mixed values

Sample Output:

Original array elements:
25 Anna False 4/24/2021 11:43:11 AM -112 -34.67 

After removing all the values except integer values from the said array of mixed values:
25 -112

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] mixedArray = new object[6];
            mixedArray[0] = 25;
            mixedArray[1] = "Anna";
            mixedArray[2] = false;
            mixedArray[3] = System.DateTime.Now;
            mixedArray[4] = -112;
            mixedArray[5] = -34.67;
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < mixedArray.Length; i++)
            {
                Console.Write(mixedArray[i]+" ");
            }
            int[] new_nums = test(mixedArray);
            Console.WriteLine("\n\nAfter removing all the values except integer values from the said array of mixed values:");
            for (int i = 0; i < new_nums.Length; i++)
            {
                Console.Write(new_nums[i]+" ");
            }
        }
        public static int[] test(object[] nums)
        {
            return nums.OfType<int>().ToArray();
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now