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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:02:02+00:00 2026-06-12T06:02:02+00:00

This may be somewhat of a noobish question but I cannot seem to find

  • 0

This may be somewhat of a noobish question but I cannot seem to find any information on this behavior at all.

Here is the code:

class Cars
{
    public int year = 0;
    public string make = " ";
    public string model = " ";

    public Cars()
    {
    }

    public Cars(int tYear, string tMake, string tModel)
    {
        year = tYear;
        make = tMake;
        model = tModel;
    }

    public string ConvertToString()
    {
        return year.ToString() + make + model;
    }
}

class Program
{
    Cars oneCar = new Cars();
    List<Cars> manyCars = new List<Cars>();

    public void Init()
    {
        manyCars.Add(new Cars(1993, "Nissan", "240SX"));
        manyCars.Add(new Cars(2001, "Isuzu", "Rodeo"));
        manyCars.Add(new Cars(2010, "Ford", "F150"));

        oneCar = manyCars[2];
        oneCar.model = "Pinto";

        for (int i = 0; i < manyCars.Count(); i++)
            Console.WriteLine(manyCars[i].ConvertToString());

        Console.WriteLine(oneCar.ConvertToString());
    }

    static void Main(string[] args)
    {
        Program prg1 = new Program();
        prg1.Init();
    }
}

The output is this:

1993Nissan240SX

2001IsuzuRodeo

2010FordPinto

2010FordPinto

Why is the third line reading Pinto instead of F150? It seems to be setting the oneCar variable to be a pointer or something. How can this be avoided? I thought that’s what the "new" keyword accomplished.

Thanks in advance for any help.

  • 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-12T06:02:03+00:00Added an answer on June 12, 2026 at 6:02 am

    Because Cars is an object, and objects are Reference Types in C#.

    When you call this line of code:

    oneCar = manyCars[2];
    

    You are pointing oneCar to the memory location of manyCars[2]. They are referencing the same object. Updating oneCar also updates manyCars[2].

    This link has a good writeup on Reference and Value types.

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

Sidebar

Related Questions

This may be a simple question but I can;t find the answer anywhere. Here
I understand that this question may seem somewhat ungrounded, but if someone knows anything
I am new to programming so this may seem somewhat straightforward, but I cannot
This may be a stupid question but I have a code with the following
This may sound like a very generic question but here it goes. I have
Hey this may be a somewhat simple question, but I'm new to Eclipse and
This may not sound like a coherient question, but I need to find some
This is somewhat related to this question , but I was hoping there may
I apologize if this may seem like somewhat of a novice question (which it
This may seem like a somewhat contrived example, but I'm left scratching my head.

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.