0

First of all i'm novice to C#. How to determine array size in C# ? with the if condition check.

Normally php do like this,

if((sizeof($NewArray)==3) && (strtolower($NewArray[1])=='dddd'))

I just tried it out like this,

 If(NewArray.Length)==3) && (

After that i'm stucking ....

2
  • 2
    As a side note to the main answer, it's considered bad practice to use ToLower() when what you want to do is make a case insensitive string comparison. It's not too bad when done once or twice, but in a method that's called many times, it could cause performance problems since ToLower create and allocates a new string every time. You can use myString.Equals("secondString", StringComparison.CurrentCultureIgnoreCase), which is a bit bulkier, but smarter. Commented Jun 7, 2012 at 10:54
  • Your question Title is incorrect? Commented Jun 7, 2012 at 10:56

4 Answers 4

3

You're looking for ToLower() method?

if (newArray.Length == 3 && newArray[1].ToLower() == "dddd") ...
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure what part you are stuck with so I shall explain all the parts I think I can see.

It looks like you are looking for the indexer syntax on arrays.

The code you may want is:

if (NewArray.Length == 3 && NewArray[1].ToLower() == "dddd")

Note the square brackets [] indexing into the array. Regular C# arrays exposes an int indexer. Once indexed, the dot-notation will give you access to the type inside the array, here I assume that the array is a string[], hence we can do NewArray[1].<string members here>.

Note also that array indexing in C# is zero-based, so 0 is the first element of the array and NewArray.Length - 1 is the last element. Your [1] may not be correct unless of course you intend on accessing the second array item.

As a side note, using ToLower is not the only way to get case-insensitive comparisons, you can also do the following:

string.Compare(NewArray[1], "dddd", true) == 0

The string.Compare documentation shows the ignoreCase argument. I'm not in any way trying to say my suggestion is best practice.

Comments

1
 //in php
 if((sizeof($NewArray)==3) && (strtolower($NewArray[1])=='dddd'))

 //in C#
 if ((NewArray.Length == 3) && (NewArray[1].ToLower() == "dddd"))

1 Comment

This won't compile because (NewArray[1] == "ddd") returns a boolean and there is no ToLower() on bool.
0

Try this

if( NewArray.Length== 3 && NewArray[1].ToLower() =="dddd")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.