12.07.2015 Views

Accelerated

Accelerated

Accelerated

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4 ■ CLASSES, STRUCTS, AND OBJECTS 111The meat of the changes is in the Main method. Notice that I’ve replaced the ugly try/finallyconstruct with the cleaner using statement. Under the covers, the using statement expands to thetry/finally construct I already had. Now, granted, this code is much easier to read and understand.However, it still doesn’t remove the burden from the client code of having to remember to use theusing statement in the first place.The using statement requires that all resources acquired in the acquisition process be implicitlyconvertible to IDisposable. That is, they must implement IDisposable. If they don’t, you’ll see acompiler warning.Method Parameter TypesMethod parameters follow the same general rules as those of C/C++. That is, by default, parametersdeclare a variable identifier that is valid for the duration and scope of the method itself. There areno const parameters as in C++, and method parameters may be reassigned at will. Unless theparameter is declared a certain way as a ref or an out parameter, such reassignment will remainlocal to the method.I have found that one of the biggest stumbling blocks for C++ developers in C# is dealing withthe semantics of variables passed to methods. Since the dominant type of type instance within theCLR is a reference, variables to such objects merely point to their instances on the heap—i.e., argumentsare passed to the method using reference semantics. C++ developers are used to copies ofvariables being made as they’re passed into methods by default, unless they’re passed by referenceor as pointers. In other words, arguments are passed using value semantics.In C#, arguments are actually passed by value. However, for references, the value that is copiedis the reference itself and not the object that it references. Changes in state that are made to the referenceobject within the method are visible to the caller of the method.Since there is no notion of a const parameter within C#, you should create immutable objectsto pass where you would have wanted to pass a const parameter. I have more to say about immutableobjects in Chapter 13.■Note Those C++ developers who are used to using handle/body idioms to implement copy-on-write semanticsmust take these facts into consideration. It doesn’t mean that you cannot employ those idioms in C#; rather, it justmeans that you must implement them differently.Value ArgumentsIn reality, all parameters passed to methods are value arguments, assuming they’re normal, plain,undecorated parameters that get passed to a method. By undecorated, I mean they don’t have specialkeywords such as out, ref, and params attached to them. They can, however, have attributesattached to them just as almost everything else in the CLR type system can. As with all parameters,the identifier is in scope within the method block following the parameter list (i.e., within the curlybraces), and the method receives a copy of the passed variable at invocation time. Be careful aboutwhat this means, though. If the passed variable is a struct, or value type, then the method receives acopy of the value. Any changes made locally to the value are not seen by the caller. If the passedvariable is a reference to an object on the heap, as any variable for a class instance is, then themethod receives a copy of the reference. Thus, any changes made to the object through the referenceare seen by the caller of the method.

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

Saved successfully!

Ooh no, something went wrong!