简体   繁体   English

更改类的默认值

[英]Change Default Value of a Class

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 IEnumerable<MyClass>如何更改FirstOrDefault()方法的默认值
For example I want to return new MyClass("HelloWorld"); 例如,我想返回new MyClass("HelloWorld");

Edit: Is there a way to override default value of a Class? 编辑:有没有办法覆盖类的默认值?
Like Default Value of MyClass is new MyClass("Default"); MyClass默认值是new MyClass("Default");

You can't directly change it. 你不能直接改变它。 But you have a few alternatives. 但是你有一些选择。

If your sequence contains no null s you can use: 如果您的序列不包含null ,则可以使用:

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. 您也可以为FirstOrDefault的这个实现使用不同的名称,但由于它不会与任何现有的重载冲突,所以我只使用了相同的名称。

https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Util/LinqExtensions.cs https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Util/LinqExtensions.cs

As an equivalent alternative to CodeAsChaos's good answer, you could also do the somewhat shorter: 作为CodeAsChaos的良好答案的等效替代品,您也可以做得更短:

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

Why don't just use DefaultIfEmtpy() ? 为什么不直接使用DefaultIfEmtpy()

For instance: 例如:

var result = this.MyClasses
    .Where(c => c.SomeCondition)
    .Select(c => c)
    .DefaultIfEmpty(new DefaultClass())
    .First();

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;
}

I'm going to instead of using generics, just tie it to an enumerable of your class: 我将不再使用泛型,只需将其绑定到您的类的可枚举:

public static MyClass FirstOrDefault(this IEnumerable<MyClass> source)
{
    foreach (var x in source)
    {
        return x;
    }
    return new MyClass("hello world"); 
    //OR, could be made a bit nicer by making a static method in your class
    return MyClass.Default();
}

This will do what you want, but only for your class. 这将做你想要的,但只适用于你的班级。 If this is acceptable, then this is by far the easiest and shortest way of doing it. 如果这是可以接受的,那么这是迄今为止最简单,最短的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM