0

May be the questions look simple or may be the solution is also simple.But i have tried lot,But cant make this possible, I have a string[] array which has a value="".And i want to check this in an if condition

if (del.counts==null && del.mrate==null)
{
///
}

I have tried IsNullorEmpty,equal..etc but nothing gives me the solution Value iside String[] array

5
  • 1
    string.IsNullOrEmpty(del.counts[0])? Note that it is not del.counts which has the value "". It is the first element inside it: del.counts[0] Commented Nov 29, 2019 at 11:41
  • @rafalon perfect,Could u please post this below as answer?so that i can accept and close this post Commented Nov 29, 2019 at 11:48
  • MindSwipe beat me to it, feel free to mark his/her answer instead :) Commented Nov 29, 2019 at 11:50
  • Thanks @Rafalon, I honestly didn't see your comment until i already posted my answer ^^ Commented Nov 29, 2019 at 11:51
  • @MindSwipe no problem, I know that answers belong to the "Answer" section, but I keep writing comments when I feel like it's too small of an answer Commented Nov 29, 2019 at 11:53

1 Answer 1

1

You want to use array indexing if you know where in the array the element is that you want to check. So like this in your case:

if (string.IsNullOrEmpty(del.counts[0]))
{
    // Code
}

If you do not know where the element is you'll want to use Linq's Any(...) extension Mehtod.

if (del.counts.Any(value => string.IsNullOrEmpty(value))
{
    // Code
}

Any(...) will return true if any element of the array is NullOrEmpty and false if not

So you would use it like so in your application:

if (del.counts == null && del.mrate == null)
{
    // Your code to handle if 'del.counts' and 'del.mrate' are null
}
// We know 'del.counts' is not null, but one of the elements may be NullOrEmpty
else if (del.counts.Any(value => string.IsNullOrEmpty(value)))
{
    // Your code to handle if one of the counts elements IsNullOrEmpty
}

You may want to remove the else if and replace it with an if statement depending on your context

Sign up to request clarification or add additional context in comments.

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.