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.

42 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1Delegates are one very specific feature of <strong>C#</strong> and .NET—a detail, <strong>in</strong> the grand schemeof th<strong>in</strong>gs. Both of the other “rem<strong>in</strong>der” sections <strong>in</strong> this chapter deal with muchbroader topics. First, we will consider what it means to talk about <strong>C#</strong> be<strong>in</strong>g a staticallytyped language and the implications that has.2.2 Type system characteristicsAlmost every programm<strong>in</strong>g language has a type system of some k<strong>in</strong>d. Over time, thesehave been classified as strong/weak, safe/unsafe, static/dynamic, and no doubt somemore esoteric variations. It’s obviously important to understand the type system withwhich one is work<strong>in</strong>g, and it’s reasonable to expect that know<strong>in</strong>g the categories <strong>in</strong>towhich a language falls would give a lot of <strong>in</strong>formation to help on that front. However,because the terms are used to mean somewhat different th<strong>in</strong>gs by different people,miscommunication is almost <strong>in</strong>evitable. I’ll try to say exactly what I mean by each termto avoid confusion as much as possible.One important th<strong>in</strong>g to note is that this section is only applicable to “safe” code—which means all <strong>C#</strong> code that isn’t explicitly with<strong>in</strong> an unsafe context. As you mightjudge from the name, code with<strong>in</strong> an unsafe context can do various th<strong>in</strong>gs that safecode can’t, and that may violate some aspects of normal type safety. Most developersare unlikely ever to need to write unsafe code, and the characteristics of the type systemare far simpler to describe and understand when only safe code is considered.This section shows what restrictions are and aren’t enforced <strong>in</strong> <strong>C#</strong> 1 while def<strong>in</strong><strong>in</strong>gsome terms to describe that behavior. We’ll then see a few th<strong>in</strong>gs we can’t dowith <strong>C#</strong> 1—first from the po<strong>in</strong>t of view of what we can’t tell the compiler, and thenfrom the po<strong>in</strong>t of view of what we wish we didn’t have to tell the compiler.Let’s start off with what <strong>C#</strong> 1 does, and what term<strong>in</strong>ology is usually used to describethat k<strong>in</strong>d of behavior.2.2.1 <strong>C#</strong>’s place <strong>in</strong> the world of type systemsIt’s easiest to beg<strong>in</strong> by mak<strong>in</strong>g a statement, and then clarify what it actually means andwhat the alternatives might be:<strong>C#</strong> 1’s type system is static, explicit, and safe.You might well have expected the word strong to appear <strong>in</strong> the list, and I had half am<strong>in</strong>d to <strong>in</strong>clude it. However, while most people can reasonably agree on whether alanguage has the listed characteristics, decid<strong>in</strong>g whether or not a language is stronglytyped can cause heated debate because the def<strong>in</strong>itions vary so wildly. Some mean<strong>in</strong>gs(those prevent<strong>in</strong>g any conversions, explicit or implicit) would clearly rule <strong>C#</strong> out—whereas others are quite close to (or even the same as) statically typed, which would<strong>in</strong>clude <strong>C#</strong>. Most of the articles and books I’ve read that describe <strong>C#</strong> as a stronglytyped language are effectively us<strong>in</strong>g it to mean statically typed. I’ve used the word statichere to try to m<strong>in</strong>imize the potential for confusion—although it should be noted thatit has little <strong>in</strong> common with the common understand<strong>in</strong>g of the keyword static usedwith<strong>in</strong> code itself as related to the type rather than a particular <strong>in</strong>stance.Let’s take the terms <strong>in</strong> the def<strong>in</strong>ition one at a time and shed some light on them.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!