C# Tutorial - An Objects Identity

posted Jun 10, 2011, 10:08 AM by admin@ revitviet.vn

So what do we know about this particular 'person' object? We know he is called Peter. We could say that he is called Peter Smith, in order to distinguish him from other boys called Peter. However, we could have two Peter Smiths. So how do we tell the two boys called Peter Smith apart?

We need a unique piece of information, or pieces of information, that identifies an object apart from all others. So in this case we could use his name and address, as we are pretty safe that no two Peter Smiths share the same address. However some families name their children after the parent, so we could also use ages as well to give us a unique identifier.

Each object must be unique, whatever system is used. I have a national insurance number that identifies me uniquely to anyone else without using my name.

In OOP terms, an object is a structure for incorporating information and the behaviour of working with the information. If we think about our person object, what information would we want to store? Here is a list of my first ideas:

  • Forename
  • Surname
  • Address
  • Age
  • Gender
  • Date of Birth
  • Telephone Number
  • National Insurance Number

So how do we give the object information, or how do we get the information from the object? All objects are able to interact with one another, just like people talking. Let us imagine a quick conversation between two person objects.

Peter: "Hello, my name's Peter. What's Yours?
Jane: "Mine's Jane Hill. Peter what?"
Peter: "Peter Smith. Where do you live?"
Jane: "I live in London. Where do you live?"
Peter: "I live...."

By talking, the two objects that know about themselves, can find out about others. This is exactly what objects in software do. They, just like people can also ask another object to do something and as long as that object knows how to do it, it will if it can.


So how is this achieved by objects? Objects send messages to each other. The messages that an object will respond to are its protocol, the interface that other objects see. We can think of this protocol as the language that the object talks, just like English or German. A German could say "Guten Morgan", but it does not mean that an English person would understand them, as it is not part of the English language. Similarly if the English person said "Good Morning", the German may or may not understand what has been said.

A UML Object?

date of birth
telephone number
national insurance number
set forename()
get forename()
set surname()
get surname()
set address()
get address()
calculate age(date)
set gender()
get gender()
set date of birth()
get date of birth()
set telephone number()
get telephone number()
set national insurance number()
get national insurance number() 

We can use a simple UML object model to show our person object. The name of the object is placed within the topmost area. In the middle area we place the information about the object and finally in the bottom area we place the behaviour of that object.

Most of the behaviour allows us to access the information inside the object, these are known as access methods and allow a piece of information to be either set to a value or to obtain the value of the information. In C# this can also be achieved through the use of properties, which are a simpler way of setting and getting the information.

The behaviour of the object is not limited to accessing the information, but is often involved in manipulating the information in some manner. In the example above, the behaviour calculate age is used to work out the age of the person based on a date.