Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Default Value of a Class

Tags:

c#

ienumerable

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");

like image 880
Navid Rahmani Avatar asked Jun 15 '11 22:06

Navid Rahmani


People also ask

What is the default value for a class object?

In Java, class and instance variables assume a default value (null, 0, false) if they are not initialized manually. However, local variables aren't given a default value. You can declare but not use an uninitialised local variable. In Java, the default value of any object is null.

What is default value of class in C#?

The default value for classes is null . For structures, the default value is the same as you get when you instantiate the default parameterless constructor of the structure (which can't be overriden by the way).

How do I set default value in property?

Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.

How do you set a default value property in typescript?

Normal way to set a default value to a property is the following. interface MyInterface { a: number; b: string; c: unknown; } const data1: MyInterface = { a: 0, b: "default-string", c: "something", }; const data2: MyInterface = { a: 0, b: "default-string", c: { value: 1 }, };


4 Answers

You can't directly change it. But you have a few alternatives.

If your sequence contains no nulls you can use:

MyClass result = seq.FirstOrDefault() ?? new MyClass("HelloWorld");

Or you can implement your own version which takes a parameter for the default value:

    public static T FirstOrDefault<T>(this IEnumerable<T> source, T defaultValue)
    {
        using (IEnumerator<T> enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
                return enumerator.Current;
            else
                return defaultValue;
        }
    }

    public static T FirstOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate, T defaultValue)
    {
        using (IEnumerator<T> enumerator = source.Where(predicate).GetEnumerator())
        {
            if (enumerator.MoveNext())
                return enumerator.Current;
            else
                return defaultValue;
        }
    }

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

like image 124
CodesInChaos Avatar answered Oct 11 '22 16:10

CodesInChaos


As an equivalent alternative to CodeAsChaos's good answer, you could also do the somewhat shorter:

public static T MyFirstOrDefault<T>(
    this IEnumerable<T> sequence, 
    T myDefault)
{
    foreach(T item in sequence)
        return item;
    return myDefault;
}

public static T MyFirstOrDefault<T>(
    this IEnumerable<T> sequence, 
    Func<T, bool> predicate, 
    T myDefault)
{
    return sequence.Where(predicate).MyFirstOrDefault(myDefault);
}
like image 37
Eric Lippert Avatar answered Oct 11 '22 16:10

Eric Lippert


You could define an extension method like this:

public static T FirstOrDefault<T>(this IEnumerable<T> source, T defaultValue)
{
    foreach (T x in source)
    {
        return x;
    }
    return defaultValue;
}
like image 2
dtb Avatar answered Oct 11 '22 17:10

dtb


Why don't just use DefaultIfEmtpy()?

For instance:

var result = this.MyClasses
    .Where(c => c.SomeCondition)
    .Select(c => c)
    .DefaultIfEmpty(new DefaultClass())
    .First();
like image 2
JoanComasFdz Avatar answered Oct 11 '22 16:10

JoanComasFdz