Introduction:-
In
this article I have explained how to select only string values from ArrayList
using Linq.
Code:-
Don't
forget to add namespace:-
using
System.Linq;
Write
the following code in page load:-
protected void Page_Load(object
sender, EventArgs e)
{
Label1.Text = string.Empty;
ArrayList
al = new ArrayList
{ "Hello", 200, "World", false,
100 };
var
onlyStr = al.OfType<string>();
Console.WriteLine("Printing Only Strings");
foreach
(var str in
onlyStr)
Label1.Text += str.ToString()+"\n";
}
For Console write the
following Code:-
ArrayList al = new ArrayList { "Hello", 200, "World",
false, 100 };
var
onlyStr = al.OfType<string>();
Console.WriteLine("Printing Only Strings");
foreach
(var str in
onlyStr)
Console.WriteLine(str);
Console.ReadLine();
}
Run the code and check the
result.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.