A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to find string values from the list of objects using the Linq OfType() method
Q:

C# program to find string values from the list of objects using the Linq OfType() method

0

C# program to find string values from the list of objects using the Linq OfType() method

All Answers

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

Program:

The source code to find string values from the list of objects using OfType() method of Linq, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to find string values from the list
//of objects using the Linq OfType() method.

using System;
using System.Linq;
using System.Collections.Generic;

class Demo
{
    static void Main(string[] args)
    {
        List<object> objectList = new List<object>()
        {
  101, "Amit", 102,"Joseph", 10.3, "RK Verma", 104, "Sanjay Shukla", 10.5F, "Pramod Pandey"
        };

        List<string> result = objectList.OfType<string>().ToList();

        foreach (string str in result)
        {
            Console.WriteLine(str + " ");
        } 
    }
}

Output:

Amit
Joseph
RK Verma
Sanjay Shukla
Pramod Pandey
Press any key to continue . . .

Explanation:

In the above program, we created a list of objects that contains different types of values then we find string values using OfType() method using the below code.

List<string> result = objectList.OfType<string>().ToList();

 

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now