See this sample:
var list = new List<string?>();
foreach (string item in list.Where(i => i != null))
{
if (item.Length == 2)
{
...
}
}
In this sample, I get possible null reference in two places. The foreach variable and the dereferencing of Length in if. The second one I can easily fix by adding a dammit (null-forgiving) operator like this: item!.Length
Is there any way to do the same thing to the first one? I know that I can mark it as nullable string and check again but, I have already checked.
When you apply filtering using Where, you eliminate all null values, but this doesn't change the type of values.
All you need is to cast the results after filtering to the proper type:
foreach (string item in list.Where(i => i != null).Select(x => x!))
Unfortunately the Where predicate doesn't modify the caller's view of whether items in the enumerable are null or not.
The approach given by Dmitry involves additional runtime overhead due to the use of an extra Select call.
You can avoid that overhead via this extension method:
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source) where T : class
{
foreach (T? item in source)
{
if (item != null)
yield return item;
}
}
Your code then becomes:
foreach (string item in list.WhereNotNull())
{
if (item.Length == 2)
{
...
}
}
I need compare if a Radcombobox has ItemElements that matches with my expected string. Here is what I'm trying to do:
foreach (IRadComboBoxItem item in comboBox.ItemElements)
{
var itemExists = comboBox.ItemElements.FirstOrDefault(items => item.Text.Contains(expectedString));
if (itemExists == null) continue;
itemExists.Select();
return true;
}
However comboBox.Text.Contains(expectedString) is not supported as I'm comparing IRadComboBoxItem with a string. Could you please suggest how to achieve this?
Use linq method of Any:
return comboBox.ItemElements.Any(item => item.Text.Contains(expectedString));
In your above code you mixed a bit the use of different linq methods
In the FirstOrDefault - it returns the first item in a collection that matches a predicate, otherwise default(T).
Then if it is not null you perform an Select but assign it to nowhere.
You have this code in a foreach loop - but do not use the item nowhere. you don't need the loop because you are trying to use the linq methods (which behind the scenes use the loops themselves)
Following comment what you want is:
var wantedItem = comboBox.ItemElements.FirstOrDefault(item => item.Text.Contains(expectedString));
if(wantedItem != null)
{
//What you want to do with item
}
Didn't work with RadComboBox myself but by this site maybe:
RadComboBoxItem item = comboBox.FindItemByText(expectedString);
I assume that if it doesn't find it returns null
I was wondering if there was a build in method to remove and return the first item of a list with one method/command.
I used this, which was not pretty
Item currentItem = items.First();
items.RemoveAt(0);
So I could wrote an extension-method:
public static class ListExtensions
{
public static T RemoveAndReturnFirst<T>(this List<T> list)
{
T currentFirst = list.First();
list.RemoveAt(0);
return currentFirst;
}
}
//Example code
Item currentItem = items.RemoveAndReturnFirst();
Is this the best possibility or is there any built-in method?
The list is returned from a nHibernate-Query and therefore it should remain a List<T>.
Most suitable collection for this operation is Queue:
var queue = new Queue<int>();
queue.Enqueue(10); //add first
queue.Enqueue(20); //add to the end
var first = queue.Dequeue(); //removes first and returns it (10)
Queue makes Enqueue and Dequeue operations very fast. But, if you need to search inside queue, or get item by index - it's bad choice. Compare, how many different types of operations do you have and according to this choose the most suitable collection - queue, stack, list or simple array.
Also you can create a Queue from a List:
var list = new List<int>();
var queue = new Queue<int>(list);
There is no built-in method. Your code looks fine to me.
One small thing, I would use the indexer, not the First extension method:
T currentFirst = list[0];
And check your list if there is a Count > 0.
public static T RemoveAndReturnFirst<T>(this List<T> list)
{
if (list == null || list.Count == 0)
{
// Instead of returning the default,
// an exception might be more compliant to the method signature.
return default(T);
}
T currentFirst = list[0];
list.RemoveAt(0);
return currentFirst;
}
If you have to worry about concurrency, I would advice to use another collection type, since this one isn't thread-safe.
What would be the optimal way to get the only element in the list? if it is not equal to one, log it.
Is it better to use a try..catch block against Single? or use Count?
try
{
var item = list.Single();
}
catch(System.InvalidOperationException)
{
//log
Console.WriteLine("The collection does not contain exactly one element.");
}
or
if(list.Count!=1)
{
//log
Console.WriteLine("The collection does not contain exactly one element.");
}
var item = list.FirstOrDefault();
Well, try {...} catch {...} will do stack trace and it means much overhead.
So the 2nd possibility is a better one (Count is a good choice when working with List<T>). Actually, you don't need Linq at all:
// list.Count is just a integer field, a very cheap comparison
if (list.Count != 1) {
// 0 or many items (not a single one)
...
}
else {
// list contains exactly one item
var item = list[0];
...
}
beware traps like this:
List<Object> list = new List<Object>() {
null,
123
};
// item == null even if list has TWO items
var item = list.FirstOrDefault();
if (list == null || list.Count != 1)
{
Console.WriteLine("The collection does not contain exactly one element.");
}
As Iridium commented, Single() or SingleOrDefault() might not be the best choice here because it throws an exception if there is more than one element or the list equals null.
I replaced that with a null-check and a comparison with count
This way is better
if(list.Count!=1)
{
//log
Console.WriteLine("The collection does not contain exactly one element.");
}
var item = list.FirstOrDefault();
I often come across code like the following:
if ( items != null)
{
foreach(T item in items)
{
//...
}
}
Basically, the if condition ensures that foreach block will execute only if items is not null. I'm wondering if the if condition is really needed, or foreach will handle the case if items == null.
I mean, can I simply write
foreach(T item in items)
{
//...
}
without worrying about whether items is null or not? Is the if condition superfluous? Or this depends on the type of items or maybe on T as well?
You still need to check if (items != null) otherwise you will get NullReferenceException. However you can do something like this:
List<string> items = null;
foreach (var item in items ?? new List<string>())
{
item.Dump();
}
but you might check performance of it. So I still prefer having if (items != null) first.
Based on Eric's Lippert suggestion I changed code to:
List<string> items = null;
foreach (var item in items ?? Enumerable.Empty<string>())
{
item.Dump();
}
Using C# 6 you could use the new null conditional operator together with List<T>.ForEach(Action<T>) (or your own IEnumerable<T>.ForEach extension method).
List<string> items = null;
items?.ForEach(item =>
{
// ...
});
The real takeaway here should be a sequence should almost never be null in the first place. Simply make it an invariant in all of your programs that if you have a sequence, it is never null. It is always initialized to be the empty sequence or some other genuine sequence.
If a sequence is never null then obviously you don't need to check it.
Actually there is a feature request on that here: https://github.com/dotnet/csharplang/discussions/1081#issuecomment-443209795
And the response is quite logical:
I think that most foreach loops are
written with the intent of iterating a
non-null collection. If you try
iterating through null you should get
your exception, so that you can fix
your code.
You could always test it out with a null list... but this is what I found on the msdn website
foreach-statement:
foreach ( type identifier in expression ) embedded-statement
If expression has the value null, a System.NullReferenceException is thrown.
You can encapsulate the null check in an extension method and use a lambda:
public static class EnumerableExtensions {
public static void ForEach<T>(this IEnumerable<T> self, Action<T> action) {
if (self != null) {
foreach (var element in self) {
action(element);
}
}
}
}
The code becomes:
items.ForEach(item => {
...
});
If can be even more concise if you want to just call a method that takes an item and returns void:
items.ForEach(MethodThatTakesAnItem);
It is not superflous. At runtime items will be casted to an IEnumerable and its GetEnumerator method will be called. That will cause a dereferencing of items that will fail
You do need this. You'll get an exception when foreach accesses the container to set up the iteration otherwise.
Under the covers, foreach uses an interface implemented on the collection class to perform the iteration. The generic equivalent interface is here.
The foreach statement of the C#
language (for each in Visual Basic)
hides the complexity of the
enumerators. Therefore, using foreach
is recommended instead of directly
manipulating the enumerator.
The test is necessary, because if the collection is null, foreach will throw a NullReferenceException. It's actually quite simple to try it out.
List<string> items = null;
foreach(var item in items)
{
Console.WriteLine(item);
}
the second will throw a NullReferenceException with the message Object reference not set to an instance of an object.
As mentioned here you need to check is it not null.
Do not use an expression that evaluates to null.
The accepted answer is getting old.
Nowadays, nullable types are used extensively and help the compiler understand what you're trying to achive (and avoid mistakes).
Which means that your list could be this :
List<Item>? list
...OR... this :
List<Item> list
You'll need to check for nullability only for the former case.
Same thing goes for items:
List<Item?> list
...OR... this :
List<Item> list
You'll need to check for nullability of items only for the former case.
And of course finally you have this :
List<Item?>? list
where anything (list and items) could potentially be null.
==================
EDIT: A picture is better than 1,000 words
In C# 6 you can write sth like this:
// some string from file or UI, i.e.:
// a) string s = "Hello, World!";
// b) string s = "";
// ...
var items = s?.Split(new char[] { ',', '!', ' ' }) ?? Enumerable.Empty<string>();
foreach (var item in items)
{
//..
}
It's basically Vlad Bezden's solution but using the ?? expression to always generate an array that is not null and therefore survives the foreach rather than having this check inside the foreach bracket.