public class MyClass
{
public string myString;
public MyClass(string s)
{
this.myString = s;
}
}
In IEnumerable<MyClass> how can I change the default value of FirstOrDefault() method
For example I want to return new MyClass("HelloWorld");
Edit: Is there a way to override default value of a Class?
Like Default Value of MyClass is new MyClass("Default");
You can’t directly change it. But you have a few alternatives.
If your sequence contains no
nulls you can use:Or you can implement your own version which takes a parameter for the default value:
You could also use a different name for your this implementation of
FirstOrDefault, but since it doesn’t collide with any of the existing overloads, I just used the same name.https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Util/LinqExtensions.cs