开发者

MSDN text clarification -reference types are objects

开发者 https://www.devze.com 2023-02-06 05:12 出处:网络
On MSDN, one can read following: 开发者_如何学运维 Reference types are also referred to as objects

On MSDN, one can read following:

开发者_如何学运维

Reference types are also referred to as objects

I do not understand. I thought that value of reference type points to actual object that lives on the heap, ie at least I would say "Instances of reference types are also reffered to as object". Or am I missing something?

There is also often mentioned "variable of reference type"..but I can have only value of reference type when its e.g.stored in the list. Do I get it correctly?


The jargon can be a little confusing here since it's very easy (and common) to be sloppy.

class Foo { }
Foo f = new Foo();

This defines the reference type Foo and the next line defines a reference variable, f, that points to an anonymous instance (object) of Foo. Note that working with an object always involves two 'things'. We often simplify a little by saying "f is a Foo object" and while that is practical it is not the full truth.

A few points that may help:

  • the instances (objects) don't have a name
  • you only name the reference variables
  • you cannot pass an instance (itself) as a parameter or return value
  • you cannot create arrays of instances
  • this means you can only pass/copy/store references
  • those references behave exactly like value types (!)

Note that when you look at interface types there only is the reference half, there are no 'instances' of interfaces.


The value of the reference points to the object in the heap. The object itself is a reference type, because you access it through a reference. (Although the whole redirection through the reference is transparent to you. You might think you're passing around a String object, but you're actually passing around a reference to a string object, which is dereferenced for you.)

The Reference itself (i.e. the pointer, not the object on the heap), is a value type - just to confuse things.


You could imagine that the word "Reference" just means "Pointer"; a list of objects of your own class contains the references/pointers to the heap allocated objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消