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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:29:40+00:00 2026-05-31T07:29:40+00:00

This should be a fundamental C# question. Suppose I have a Person class which

  • 0

This should be a fundamental C# question. Suppose I have a Person class which contains a property named Pets which is a List<Pet>.

If I want to update a pet, I can get the pet as a variable and manipulate properties on it, but I can’t seem to create a new pet object and assign it to the existing pet object. I get a “Value assigned is not used in any execution path” warning. I created some very simple code that outlines this issue.

In my real code, I want to be able to use a new child object and replace and existing object. If you can show me how I can do that with the sample updatedCat below, I would greatly appreciate it!

class Program
{
  static void Main(string[] args)
  {
    var program = new Program();
    program.RunMe();
  }

  public void RunMe()
  {
    var myPerson = new Person() { Name = "John Doe" };
    var dog = new Pet() { Type = "Dog", Name = "Woofie" };
    var cat = new Pet() { Type = "Cat", Name = "Chester" };
    myPerson.Pets.Add(dog);
    myPerson.Pets.Add(cat);
    Console.WriteLine("Initial Pet Status:");
    ListPets(myPerson);

    var currentDog = myPerson.Pets.SingleOrDefault(p => p.Type == "Dog");
    currentDog.Name = "Snoopie";
    Console.WriteLine("\r\nPet Status After Updating Dog Directly (name should be 'Snoopie'):");
    ListPets(myPerson);

    var updatedCat = new Pet() { Type = "Cat", Name = "Felix" };
    var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
    currentCat = updatedCat; 
    //Resharper shows "Value assigned is not used in any execution path" for the currentCat
    //and the current cat is never updated
    Console.WriteLine("\r\nPet Status After Trying to Update Cat by Assigning a New Cat to existing Cat (name should be 'Felix' but it's not):");
    ListPets(myPerson);

    Console.ReadLine();
  }

  public void ListPets(Person person)
  {
    foreach (var pet in person.Pets)
    {
      Console.WriteLine(string.Format("   {0} has a {1} named {2}", person.Name, pet.Type, pet.Name));
    }
  }
}

public class Person
{
  public string Name { get; set; }
  public List<Pet> Pets { get; set; }

  public Person()
  {
    Pets = new List<Pet>();
  }
}

public class Pet
{
  public string Type { get; set; }
  public string Name { get; set; }
}

Edit to add Id value to Pet and create a InsertOrUpdatePet method
(Note: I removed the Console.Writeline command for succinctness)

class Program
{
  static void Main(string[] args)
  {
    var program = new Program();
    program.RunMe();
  }

  public void RunMe()
  {
    var myPerson = new Person() { Name = "John Doe" };
    var dog = new Pet() { Id = 1, Type = "Dog", Name = "Woofie" };
    var cat = new Pet() { Id = 2, Type = "Cat", Name = "Chester" };
    myPerson.Pets.Add(dog);
    myPerson.Pets.Add(cat);

    var updatedCat = new Pet() { Id = 2, Type = "Cat", Name = "Felix" };
    InsertOrUpdatePet(myPerson, updatedCat);

    var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
  }


  public void InsertOrUpdatePet(Person person, Pet pet)
  {
    var currentPet = person.Pets.SingleOrDefault(p => p.Id == pet.Id);
    if(currentPet == null)
    {
      person.Pets.Add(pet);
    }
    else
    {
      currentPet = pet; // This doesn't work
      person.Pets.SingleOrDefault(p => p.Id == pet.Id) = pet; //This throws an error 
    }
  }

}

public class Person
{
  public string Name { get; set; }
  public List<Pet> Pets { get; set; }

  public Person()
  {
    Pets = new List<Pet>();
  }
}

public class Pet
{
  public int Id { get; set; }
  public string Type { get; set; }
  public string Name { get; set; }
}
  • 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-31T07:29:41+00:00Added an answer on May 31, 2026 at 7:29 am

    That depends on what do you want to do conceptually. If you want to say that the person got rid of the old cat and has now a new cat, you should do that: remove the old cat and then the new cat:

    var updatedCat = new Pet() { Type = "Cat", Name = "Felix" };
    var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
    myPerson.Remove(currentCat);
    myPerson.Add(updatedCat);
    

    If, on the other hand, the person still has the same cat, but the cat was renamed, you should rename the old cat, the same way you did it with the dog:

    var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
    currentCat.Name = "Felix";
    

    Your code doesn’t work, because currentCat is something that references the cat, not the spot in the list. If you want some way to represent the spot in the list, you could use indexing into the list, as Jason suggested.

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

Sidebar

Related Questions

I have a question which seems to be rather fundamental but I can't seem
OK, I'm feeling like this should be easy but am obviously missing something fundamental
This should a quick question for some easy rep. I'm doing some PHP Website
This should be straight forward for a guru. I don't have any code really
This should be a simple question, but I haven't been able to find a
This is probably a fundamental question for an experienced iOS developer, but coming from
I have a rather fundamental question about XML here. When declaring an element's xmlns
In my first attempt at an MVC web application I have a fundamental question:
I have an Order class which goes through a series of defined states. To
I've been pondering this question for a while: Can you build a faster fundamental

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.