This question already has answers here:
Check if list is empty in C#
(8 answers)
Closed 3 years ago.
This is my Code:
public int PostCanal(List<int> listchannel) {
if (listchannel == null || listchannel.Contains(0))
{
listchannel.Add(1);
}
This list has values coming from a checkbox menu. So, if the user uncheck all the options I want to still using "1" as default value.
On this case, the listchannel List<int> is arriving with [0] value. However the if condition is skipped.
Any idea? Also tried .Equals Method.
You could use Any Or Count to check if the list is empty or not, like the following code :
if(listchannel == null || !listchannel.Any())
{
}
//Or
if(listchannel == null || listchannel.Count == 0)
{
}
I hope you find this helpful.
Related
This question already has answers here:
C# difference between == and Equals()
(20 answers)
Are string.Equals() and == operator really same? [duplicate]
(8 answers)
Closed 5 years ago.
I'm writing a simple contains function which accepts a value and checks if it exists in the linked list. Returns a bool. However, when temp.Data is the same as data, as checked through debugging, it still proceeds to enter the while statement and cycle through the list, as if they are not equal. I have found a solution, by using toString() on all values. So I have to compare temp.Data.ToString() and data.ToString(). Then it works...I am new to C#, I haven't used the Object keyword much. I'm wondering if it's something to do with that? So my function works with my quick fix but I'm trying to understand why...thanks!
public bool Contains(Object data)
{
Node temp = head;
while((temp.Data != data) && (temp.Next != null))
{
temp = temp.Next;
}
//Rest of code
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
first time posting on here and a bit of a noob in c#. Basically I'm creating a linkedlist and initializing it to null at the beginning of my class. When I'm ready to use it, I check to make sure that it's not equal to the string passed by the method. I immediately get a NullReferenceException, and i'm supposed to be comparing it to null. Any fixes?
private DoubleLinkedListCell<DoubleLinkedListCell<GamePiece>> _columns = null;
public void FindColumn(string columnId)
{
bool right = true;
while (_columns.Id != columnId)
{
if (_columns.Next == null)
{
right = false;
}
if (right)
{
Columns = Columns.Next;
}
else
{
Columns = Columns.Prev;
}
}
}
You will get null reference if u try to access any property or member of _columns List (ex _columns.Id,_columns.Next etc..) so initialize it in constructor or direct when u declare field
This question already has answers here:
How to ignore the case sensitivity in List<string>
(12 answers)
Closed 7 years ago.
I have something like this:
string configKeys = "othewr|RDX|MDX";
and wrote a code like this to see if value "OTHER" exists in that list
List<string> values = configKeys.Split('|').ToList();
var b = values.Find(item => item.Trim().ToUpper() == "OTHER").FirstOrDefault();
but for example because I have typed it wrong "othewr" so it crashes, but just want it to tell me if it exists or not as a boolean. How can I change the code to do that and not crash?
Use Any. If the predicate evaluates for at least one value in the collection it returns true, else false:
var b = values.Any(item => item.Trim().ToUpper() == "OTHER");
This question already has answers here:
How to check if IEnumerable is null or empty?
(23 answers)
Closed 7 years ago.
Often in C# I have to do this
if(x.Items!=null && x.Items.Any())
{ .... }
Is there a short cut on a collection ?
In C# 6, you'll be able to write:
if (x.Items?.Any() == true)
Before that, you could always write your own extensions method:
public static bool NotNullOrEmpty<T>(this IEnumerable<T> source)
{
return source != null && source.Any();
}
Then just use:
if (x.NotNullOrEmpty())
Change the name to suit your tastes, e.g. NullSafeAny might be more to your liking - but I'd definitely make it clear in the name that it's a valid call even if x is null.
I also do a check on items of the list to assure the list not just contains all null objects; so as enhancement to Jon Skeet answer:
public static bool NotNullOrEmpty<T>(this IEnumerable<T> source)
{
return source != null && !source.All(x => x == null);
}
This question already has answers here:
Easier way of writing null or empty?
(7 answers)
Closed 9 years ago.
Is there any way to check for string either it is null or blank("") in c#?
Currently I have to check two conditions first for null and other for blank value
if(val == "" || val == null)
{
return true;
}
You can use String.IsNullOrEmpty() method which checks for string references that are null or have no data:
if(String.IsNullOrEmpty(val))
{
return true;
}
There is also a method String.IsNullOrWhitespace() which indicates whether a specified string is null, empty, or consists only of white-space characters.
if(String.IsNullOrWhitespace(val))
{
return true;
}
The above is a shortcut for the following code:
if(String.IsNullOrEmpty(val) || val.Trim().Length == 0)
{
return true;
}
You can use String.IsNullOrEmpty method.
Indicates whether the specified string is null or an empty string.
if(String.IsNullOrEmpty(val))
{
return true;
}
There is easiest and simple way.
if (string.IsNullOrEmpty("Val")) //This condition comparing both NULL and EMPTY also
{
}
.Net has provided default function for this purpose you should use like this.
if (string.IsNullOrEmpty("any string"))
{
}
You can use String.IsNullOrEMpty.