Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:25:36+00:00 2026-05-23T04:25:36+00:00

public class MyClass { public string myString; public MyClass(string s) { this.myString = s;

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:25:37+00:00Added an answer on May 23, 2026 at 4:25 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say i have this public class MyClass { public string myMessage { get;
I want do something like that: public class MyClass { public String varA{ get;
Given the following example class: public class MyClass { public string S { get;
Suppose I have a class public class MyClass { private Set<String> set = new
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
Given this definition: [UniqueId(Ident)] public class MyClass : MyBase { public int Ident{ get;
In java <1.5, constants would be implemented like this public class MyClass { public
If I set a Friend-level scope on a setter, like this... Public Class MyClass
When should I use the this -keyword for properties in code? public class MyClass
public class MyClass { public string Name {get; KEYWORD set;} public MyClass(string name) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.