Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Linq FirstOrDefault() null check in c#
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { try{ //Your code goes here PlayerSettingsResponsible objPlayer = new PlayerSettingsResponsible(); objPlayer.Designation="DesigOne"; objPlayer.IdPlayerDeviceSettingsType=1;//Change equal to 2 gives null objPlayer.Value="ValueOne"; List<PlayerSettingsResponsible> objPlayerList = new List<PlayerSettingsResponsible>(); objPlayerList.Add(objPlayer); PlayerSettingsResponsible objPlayerSearch = objPlayerList.FirstOrDefault(r => r.IdPlayerDeviceSettingsType != null && r.IdPlayerDeviceSettingsType == 1); if(objPlayerSearch == null) { Console.WriteLine("Null Object"); return; } string sValue = objPlayerSearch.Value; Console.WriteLine(sValue); } catch(Exception e){Console.WriteLine("Exception: "+e.Message);} } } public class PlayerSettingsResponsible { public string Designation { get; set; } public int? IdPlayerDeviceSettingsType { get; set; } public string Value { get; set; } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
list copy test
SortedDictionary<TKey, TValue> クラス
2
Math 5.81
Великий и могучий РУСЯЗ
Towers of Hanoi
Linked Lists
1
test
Timespan Calculator
Please log in to post a comment.