all groups > c# > april 2008 >
You're in the

c#

group:

getting first element from array


getting first element from array CSharper
4/23/2008 12:00:53 PM
c#: I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}

if (array.Contains<string>("Csharp"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.
}

Could someone help?
Re: getting first element from array Ignacio Machin ( .NET/ C# MVP )
4/23/2008 12:18:45 PM
[quoted text, click to view]

Hi,

myStringList.Find(delegate(string item) { return
item.Contains("MyStr"); });

Love generics :)

Re: getting first element from array CSharper
4/23/2008 1:43:17 PM
On Apr 23, 2:18 pm, "Ignacio Machin ( .NET/ C# MVP )"
[quoted text, click to view]

Re: getting first element from array CSharper
4/23/2008 1:59:19 PM
[quoted text, click to view]
quick question, what does (x=>x.Contains("Csharp")) means?
I know that, x is each element from the collection and it returns the
string of x. So does it mean, foreach element in the array first the
first element which contains the requested match?
x=>x.Contains() what does it do? It is evaluating to a bool and where
is the return here?
Re: getting first element from array Jon Skeet [C# MVP]
4/23/2008 8:15:17 PM
[quoted text, click to view]

You can avoid the "contains" bit to start with by using FirstOrDefault:

string first = array.FirstOrDefault(x => x.Contains("Csharp"));

if (first != null)
{
...
}

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: getting first element from array Jon Skeet [C# MVP]
4/23/2008 10:32:23 PM
[quoted text, click to view]

Sort of, yes.

[quoted text, click to view]

It's a lambda expression. I don't have time to go into lambda
expressions in detail right now, but hopefully you can find out a fair
amount about them just by searching with the right name.

My bluffer's guide gives *slightly* more detail:
http://csharpindepth.com/Articles/General/BluffersGuide3.aspx

<shameless plug>
If you want *lots* of detail, look at chapter 9 in my book - click on
the image on the left hand side of the page linked above.
</shameless plug>

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: getting first element from array Chris Dunaway
4/24/2008 7:01:45 AM
[quoted text, click to view]

Has the book been released yet? Amazon still shows April 15, 2008 and
haven't updated their site in awhile with any new information.
Manning's site seems to indicate that it is available (not pre-order)
but Amazon doesn't seem to know about it and hasn't updated the
shipping information on my order.

Regards,

Re: getting first element from array CSharper
4/24/2008 7:29:57 AM
[quoted text, click to view]

Thanks Jon and I appriciate your answer and the link and yes, I will
buy the book (based on the content on your page, it is worth
pursuing...)
I am starting to write a blog for myself and bunch of my friends at
work to keep up with C# and here it the link, if you have time have a
look at it and let me know if the content is right?
Re: getting first element from array Jon Skeet [C# MVP]
4/24/2008 7:33:10 PM
[quoted text, click to view]

It's been shipped from the printers - I've got my copies. I'd expect it
to become in stock at Amazon Real Soon Now (tm). I keep checking as
well - as soon as it's available from Amazon I'll blog about it one
last time :)

I suspect you'll get it faster if you order it from Manning though - I
assume they've got copies ready to ship. I haven't checked pricing,
mind.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: getting first element from array Jon Skeet [C# MVP]
4/24/2008 7:34:22 PM
[quoted text, click to view]

Looks good to me, but I'd try to format the code a bit more clearly.
You're welcome to use the formatter I use for my blog:

http://csharpindepth.com/CodeFormatterTool.aspx

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button