Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Jamie Wyton
Jamie Wyton
3,011 Points

can two identical fields exist at the same time?

In getting so far with programming C# I realised that I'm duplicating a field of the same type and of the same name. For example in my main method I have delcared: Map map = new Map(8, 5);

Yet in other classes, for example the MapLocation constructor: public MapLocation(int x, int y, Map map) : base(x, y) {.....}

How does a computer cope with having two identical field types?

2 Answers

Jamie Wyton
Jamie Wyton
3,011 Points

yes ok they're instances of objects, what I mean is when that line is executed we have an object of Map called map, and then when another method is called in another class, another object is passed as an argument of type Map also being called map. Does that mean that we have two Map map?

Steven Parker
Steven Parker
231,172 Points

You might, in a sense. One is an actual object named "map", and the other is is a passed parameter name that is simply a substitute for the real name of the object passed in as an argument in the method call.

Steven Parker
Steven Parker
231,172 Points

There are no fields in your examples.

Your first example is the creation of a class instance, and the second one is the declaration of a class constructor. There are not any fields in either example.

Regarding the names, let's look at that first example:

    Map map = new Map(8, 5);

Here a variable named "map" (with small "m") is being created, and it's type is class "Map" (capital "M"). C# is case sensitive, so these two names are different. Then, the variable is being assigned by using the new operator and calling the class constructor which has the same name as the class itself ("Map"), but can be distinguished by the arguments being passed to it in parentheses following the name.

Does that clear things up?