CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    11

    short instruction

    hello,

    I'm new to c sharp and I have a small problem.
    I have an array A of 4 elements.
    I want to check if an element e is part of the array, and if so return the index of this element (which is always unique) and assign it to a variable v.
    What's the easiest and shortest way of doing this, in one line if possible ?
    Something like var v = A.findNext(a=> a == e);
    The problem is that A.findNext doesn't exist, and A.contains only returns a boolean.

    Thanks.
    Last edited by elishac; October 26th, 2010 at 02:44 PM.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: short instruction

    Code:
    int myIndex = Array.IndexOf(myArray, myValue);
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: short instruction

    There are multiple ways.

    Code:
    // List of Integers
    var A = List<int>;
    
    //Find position of the number 42
    int myIndex = A.IndexOf(42);
    
    //or
    
    int myIndex = A.FindIndex( (i) => i == 42 );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured