question

Saga-3468 avatar image
0 Votes"
Saga-3468 asked Saga-3468 commented

Best way to detect forat, last and middle array elements

Hi all,

I have a string array: MyStringArray. I need to distinguish between the first, last and middle elements.

Currently, this is what I am doing:

            string[] MyStringArray = { "A", "B", "C", "D", "Z" };
    
             for (int i = 0; i < MyStringArray.Length; i++)
             {
    
                 if (i == 0)
                 {
                     //Do stuff for first element.
                     MessageBox.Show("First element: " + MyStringArray[i]);
                 }
                 else
                 if (i < MyStringArray.Length - 1)
                 {
                     //Do stuff for middle elements.
                     MessageBox.Show("Middle element: " + MyStringArray[i]);
                 }
                 else
                 {
                     //Do stuff for last element.
                     MessageBox.Show("Last element: " + MyStringArray[i]);
                 }
    
             }

This works, but I want to ask whether there is a better way to accomplish the same functionality. Thank your for your time. Saga

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
2 Votes"
Viorel-1 answered Saga-3468 commented

Try this code:

 //Do stuff for first element.
 MessageBox.Show( "First element: " + MyStringArray.First( ) );
    
 for( int i = 1; i < MyStringArray.Length - 1; i++ )
 {
     //Do stuff for middle elements.
     MessageBox.Show( "Middle element: " + MyStringArray[i] );
 }
    
 //Do stuff for last element.
 MessageBox.Show( "Last element: " + MyStringArray.Last( ) );

It assumes that the array is not empty.

An alternative for middle elements:

 foreach( var s in MyStringArray.Skip( 1 ).Take( MyStringArray.Length - 2 ) )
 {
     //Do stuff for middle elements.
     MessageBox.Show( "Middle element: " + s );
 }

In case of modern C# in .NET 6:

 foreach( var s in MyStringArray[1..^1] )
 {
     //Do stuff for middle elements.
     MessageBox.Show( "Middle element: " + s );
 }


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the follow up. Nice!

Which is the first Visual Studio that supports .NET 6? Is it 2022? Thanks! Saga

0 Votes 0 ·

It seems that it is not supported in previous versions: https://devblogs.microsoft.com/dotnet/announcing-net-6/#support.


1 Vote 1 ·

Found it, from the link you provided:

".NET 6 is supported with Visual Studio 2022 and Visual Studio 2022 for Mac. It is not supported with Visual Studio 2019, Visual Studio for Mac 8, or MSBuild 16. If you want to use .NET 6, you will need to upgrade to Visual Studio 2022 (which is also now 64-bit). .NET 6 is supported with the Visual Studio Code C# extension."

Oh! I did not know that VS2022 was only 64 bit. Bummer, I still have some machines running Win 32 bit.

Again, thanks! Saga

0 Votes 0 ·