13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Value types and reference types49Once you “get it,” the difference between value types and reference types is simple.It can take a while to reach that stage, and you may well have been able to write a lot ofcorrect code without really understand<strong>in</strong>g it. It’s worth persever<strong>in</strong>g, though: for oneth<strong>in</strong>g, a lot of seem<strong>in</strong>gly complicated situations are much easier to understand whenyou’re aware of what’s really go<strong>in</strong>g on.This section is not a complete breakdown of how types are handled, marshal<strong>in</strong>gbetween application doma<strong>in</strong>s, <strong>in</strong>teroperability with native code, and the like. Instead,it’s a fairly brief look at the absolute basics of the topic (as applied to <strong>C#</strong> 1) that are crucialto understand <strong>in</strong> order to come to grips with <strong>C#</strong> 2 and 3.We’ll start off by see<strong>in</strong>g how the fundamental differences between value types andreference types appear naturally <strong>in</strong> the real world as well as <strong>in</strong> .NET.2.3.1 Values and references <strong>in</strong> the real worldSuppose you’re read<strong>in</strong>g someth<strong>in</strong>g really fantastic, and want a friend to read it too.Let’s further suppose that it’s a document <strong>in</strong> the public doma<strong>in</strong>, just to avoid any accusationsof support<strong>in</strong>g copyright violation. What do you need to give your friend so thathe can read it too? It entirely depends on just what you’re read<strong>in</strong>g.First we’ll deal with the case where you’ve got real paper <strong>in</strong> your hands. To giveyour friend a copy, you’d need to photocopy all the pages and then give it to him. Atthat po<strong>in</strong>t, he has his own complete copy of the document. In this situation, we aredeal<strong>in</strong>g with value type behavior. All the <strong>in</strong>formation is directly <strong>in</strong> your hands—youdon’t need to go anywhere else to get it. Your copy of the <strong>in</strong>formation is also <strong>in</strong>dependentof your friend’s after you’ve made the copy. You could add some notes to yourpages, and his pages wouldn’t be changed at all.Compare that with the situation where you’re actually read<strong>in</strong>g a web page. Thistime, all you have to give your friend is the URL of the web page. This is reference typebehavior, with the URL tak<strong>in</strong>g the place of the reference. In order to actually read thedocument, you have to navigate the reference by putt<strong>in</strong>g the URL <strong>in</strong> your browser andask<strong>in</strong>g it to load the page. On the other hand, if the web page changes for some reason(imag<strong>in</strong>e it’s a wiki page and you’ve added your notes to the page) both you andyour friend will see that change the next time each of you loads the page.The differences we’ve seen <strong>in</strong> the real world form the heart of the dist<strong>in</strong>ctionbetween value types and reference types <strong>in</strong> <strong>C#</strong> and .NET. Most types <strong>in</strong> .NET are referencetypes, and you’re likely to create far more reference than value types. Aside fromthe special cases that follow, classes (declared us<strong>in</strong>g class) are reference types, andstructures (declared us<strong>in</strong>g struct) are value types. The other cases are as follows:■ Array types are reference types, even if the element type is a value type (so<strong>in</strong>t[] is still a reference type, even though <strong>in</strong>t is a value type).■■■Enumerations (declared us<strong>in</strong>g enum) are value types.Delegate types (declared us<strong>in</strong>g delegate) are reference types.Interface types (declared us<strong>in</strong>g <strong>in</strong>terface) are reference types, but they can beimplemented by value types.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!