11.07.2015 Views

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

SHOW MORE
SHOW LESS

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

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

34CHAPTER 2 ■ LEARNING ABOUT .NET NUMBER AND VALUE TYPESIn the bolded code, there is a reference <strong>to</strong> another namespace, type, and method witha pair of parentheses. This is called making a method call on a static class and static method.This says that the implementation of the method is the calling of another method.Notice how the other method is referenced using both namespace and type identifiers.This is how all types and methods are always referenced. A namespace identifier is only necessaryif the type (for example, class) is not defined in the current namespace.If you have namespaces with long names, this referencing can get tedious. As an alternative,you can add a using statement <strong>to</strong> reference the namespace, similar <strong>to</strong> the following.using MyMainTypes;namespace MyOtherMainTypes {static class AnotherType {public static void DoSomething() {AType.DoSomething();}}}The using statement says that if the code references any types that are not defined locally,look in this namespace (MyMainTypes in the example) <strong>to</strong> find the type. Note that if you use usingtwo namespaces that have identically named types, then referencing that type will result ina compiler failure, because it won’t know which type <strong>to</strong> reference.This covers the absolute basics of writing some code, and we are ready <strong>to</strong> write some code<strong>to</strong> do something.Writing the Add() MethodWe’ll write the code <strong>to</strong> add two numbers. To begin, create a new project in Visual <strong>C#</strong>:1. Open Visual <strong>C#</strong> (if Visual <strong>C#</strong> is open, choose File ➤ Close Solution <strong>to</strong> ensure you havea clean slate).2. Click File ➤ New Project or choose Create: Project.3. Choose Class Library, name it Calcula<strong>to</strong>r, and click OK.4. Rename Class1.cs <strong>to</strong> Calcula<strong>to</strong>r.cs.5. Save the solution.We can now write the Add() method. Add the bolded code <strong>to</strong> the Calcula<strong>to</strong>r.cs file.using System;using System.Collections.Generic;using System.Text;namespace Calcula<strong>to</strong>r{public class Calcula<strong>to</strong>r{}

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

Saved successfully!

Ooh no, something went wrong!