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 7709855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:58:00+00:00 2026-06-01T00:58:00+00:00

My application uses a list like this: List<MyClass> list = new List<MyClass>(); Using the

  • 0

My application uses a list like this:

List<MyClass> list = new List<MyClass>();

Using the Add method, another instance of MyClass is added to the list.

MyClass provides, among others, the following methods:

public void SetId(String Id);
public String GetId();

How can I find a specific instance of MyClass by means of using the GetId method? I know there is the Find method, but I don’t know if this would work here?!

  • 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-06-01T00:58:02+00:00Added an answer on June 1, 2026 at 12:58 am

    Use a lambda expression

    MyClass result = list.Find(x => x.GetId() == "xy");
    

    Note: C# has a built-in syntax for properties. Instead of writing getter and setter as ordinary methods (as you might be used to from Java), write

    private string _id;
    public string Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
    

    value is an implicit parameter to the set accessor (but this may be about to change according to this LDM). It contains the value assigned to the property.

    Since this pattern is often used, C# provides auto-implemented properties. They are a short version of the code above; however, the backing variable is hidden and not accessible (it is accessible from within the class in VB, however).

    public string Id { get; set; }
    

    You can simply use properties as if you were accessing a field:

    var obj = new MyClass();
    obj.Id = "xy";       // Calls the setter with "xy" assigned to the value parameter.
    string id = obj.Id;  // Calls the getter.
    

    Using properties, you would search for items in the list like this

    MyClass result = list.Find(x => x.Id == "xy"); 
    

    You can also use auto-implemented properties if you need a read-only property:

    public string Id { get; private set; }
    

    This enables you to set the Id within the class but not from outside. If you need to set it in derived classes as well you can also protect the setter

    public string Id { get; protected set; }
    

    And finally, you can declare properties as virtual and override them in deriving classes, allowing you to provide different implementations for getters and setters; just as for ordinary virtual methods. In the deriving class you can override only the getter or only the setter. The other accessor is inherited without changes.


    Since C# 6.0 (Visual Studio 2015, Roslyn) you can write getter-only auto-properties with an inline initializer

    public string Id { get; } = "A07"; // Evaluated once when object is initialized.
    

    You can also initialize getter-only properties within the constructor instead. Getter-only auto-properties are true read-only properties, unlike auto-implemented properties with a private setter.

    This works also with read-write auto-properties:

    public string Id { get; set; } = "A07";
    

    Beginning with C# 6.0 you can also write properties as expression-bodied members

    public DateTime Yesterday => DateTime.Date.AddDays(-1); // Evaluated at each call.
    // Instead of
    public DateTime Yesterday { get { return DateTime.Date.AddDays(-1); } }
    

    See: .NET Compiler Platform ("Roslyn")
             The history of C# (version history)

    Starting with C# 7.0, both, getter and setter, can be written with expression bodies:

    public string Name
    {
        get => _name;                                // getter
        set => _name = value;                        // setter
    }
    

    Note that in this case the setter must be an expression. It cannot be a statement. The example above works, because in C# an assignment can be used as an expression or as a statement. The value of an assignment expression is the assigned value where the assignment itself is a side effect. This allows you to assign a value to more than one variable at once: x = y = z = 0 is equivalent to x = (y = (z = 0)) and has the same effect as the statements z = 0; y = 0; x = 0;.

    Since C# 9.0 you can use read-only (or better initialize-once) properties that you can initialize in an object initializer. This is currently not possible with getter-only properties.

    public string Name { get; init; }
    
    var c = new C { Name = "c-sharp" };
    

    Beginning with C# 11, you can have a required property to force client code to initialize it.

    The field keyword is planned for a future version of C# (it did not make it into C# 11 and C# 12) and allows the access to the automatically created backing field in a semi-auto-implemented property.

    // Removes time part in setter
    public DateTime HiredDate { get; init => field = value.Date(); }
    
    public Data LazyData => field ??= new Data();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an Application like this: Class Application { List state1 = new
I have a domain classes like this Class Rules{ List <Department> departments = new
My application uses a large amount of Panda objects. Each Panda has a list
I have a small application that uses a SharePoint list as the data source.
I'm writing an application that uses renaming rules to rename a list of files
Our application uses SQL Server Reporting Services and allows users to add custom filters
I was making an application which uses list for displaying the details of a
I have an application that uses a cron like job to update a set
If you are using closed repository, but your application uses some dependencies from external(maven
When I return a single object from a controller like this, @ResponseBody public MyClass

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.