Simple concept to C/C++/Obj-C programmers, but very lengthy explanation

C# looks like C++ or Objective-C.

However, it shares lots of its philosophy with BASIC. One of the most apparent example is “pointer”.
Although C# supports pointers, it is for “unmanaged” environment. For normal “managed” environment, C# doesn’t support pointers. However, it supports the concept of pointers.

It is called “reference type”. There are explicit and implicit reference type.
What is funny from C/C++/Objective-C programmer’s point of view is that all object, or instance of a class, is declared without any “pointer” mark.

Customer object2;

If it is C++, object2 is a statically declared object. ( I don’t mean “static” type. Here “statically” I mean the opposite of “dynamically”. )
In C#, class is reference type. So, object2 is a reference. In other words, it is a pointer in C/C++/Objective-C world.
(Again, here the reference means the concept which embraces pointers and references, &, not the “reference” in C++. )

However, because there is no concept of pointer, they explain it very lengthy way.

When an instance of a class is created, a reference to the object is passed back to the programmer. In the previous example, object1 is a reference to an object that is based on Customer. This reference refers to the new object but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:

  Customer object2;

We do not recommend creating object references such as this one that does not refer to an object because trying to access an object through such a reference will fail at run time. However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, such as this:

To C/C++/Objective-C programmers, it is very easy to explain that. “Because it is just a pointer type, it doesn’t point anything yet. So, don’t call any methods or access anything yet.”

Oh.. one thing.. in C++, it is completely normal to create an object statically. Although the C# code above looks like declaring and instantiating an object statically, but it is actually equivalent to :

Customer *object2;

in C++.

So, what C# teaches us, especially language creators, is that if they try hard to make easy language to beginners ( pointer is a difficult concept for beginners ), it can actually make things more complicated.

About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: