net, java, and mobile training and consulting from intertech - … · 2014-03-08 · intrinsic...

30
Copyright © Intertech, Inc 2014 Rev: 27 1-1 Chapter 1 The Philosophy of .NET Objectives: Understand the scope of the .NET platform Understand the role of the Common Type System (CTS) Understand the role of the Common Language Specification (CLS) Understand the role of the Common Language Runtime (CLR) Know the distinction between an assembly, namespace and type Understand the role of the Common Intermediate Language (CIL) Understand the role of the Common Language Infrastructure (CLI) Work with the C# command line compiler (csc.exe) Survey several popular .NET languages and IDEs

Upload: others

Post on 12-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

Copyright © Intertech, Inc 2014 Rev: 27 1-1

Chapter 1

The Philosophy of .NET

Objectives:

Understand the scope of the .NET platform

Understand the role of the Common Type System (CTS)

Understand the role of the Common Language Specification (CLS)

Understand the role of the Common Language Runtime (CLR)

Know the distinction between an assembly, namespace and type

Understand the role of the Common Intermediate Language (CIL)

Understand the role of the Common Language Infrastructure (CLI)

Work with the C# command line compiler (csc.exe)

Survey several popular .NET languages and IDEs

Page 2: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-2 Copyright © Intertech, Inc 2014 Rev: 27

Overview

The point of this first chapter is to provide a foundation for the remainder of the class.

Here we examine some core topics of the .NET world, including the role of assemblies,

namespaces and types. We will also spend some time examining various important .NET

specifications and services, including the CTS, CLS and CLR.

The final aspect of this chapter is to survey a number of languages (and tools) which can be

used to build managed code, including C#, VB and C++/ CLI.

With this foundation in place, you will have the proper foundation to build .NET solutions

using C# and your IDE of choice.

Page 3: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-3

A First Look at the .NET Platform

• The C# programming language, and the Microsoft .NET platform, were first released circa 2003.

• Prior to the release of .NET, the Component Object Model (COM) was the preferred way to build applications for Microsoft Windows using Microsoft technologies.

• You may have heard COM described using other related terms such as ActiveX or OLE automation.

• While COM applications did provide a number of benefits…

• COM was a fairly complex programming discipline.

• Developers needed to manually manage memory.

• COM also demanded programmers implement a good deal of interfaces, author complex descriptions of their code (via IDL) and register components.

• While COM is still used today, most modern day desktop applications and web sites constructed with Microsoft technologies make use of the .NET platform.

• Do be aware that the .NET platform does support interoperability with COM applications.

• It is possible to build a .NET application which makes use of COM libraries, and vice versa! COM applications can also make use of .NET libraries.

• .NET and COM interoperability is not covered in this course; however ask your instructor if you have interest in the topic.

• So what exactly is the Microsoft .NET platform? From a high level, .NET is:

• A robust runtime environment for software components.

• A platform for building all types of software (web and desktop applications).

• A language agnostic programming platform.

• A set of specifications submitted to ECMA International to allow ports to non-Windows operating systems.

• A collection of code libraries one can use as a starting point for custom application development.

Page 4: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-4 Copyright © Intertech, Inc 2014 Rev: 27

• The .NET platform runs on top of the underlying operating system.

• Windows Vista and higher ship with the .NET platform preinstalled.

• Windows XP can be configured using the freely downloadable .NET platform installer named dotnetfx.exe (the exact name of the executable will be based on which version of .NET you are installing).

• Other OSs (Mac, Linux, Solaris, etc.) require installation of an ECMA-compliant .NET distribution (more information at the end of this chapter).

• Consider the following high-level first look at the .NET platform, with further details on the next page.

Page 5: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-5

• Here are a few key terms .NET developers should be aware of (each of which will be detailed later in this chapter):

• Common Type System (CTS): A standard for type definitions and functionality that .NET languages *can* support.

• Common Language Specification (CLS): A subset of the CTS that all .NET languages *must* support.

• Common Language Runtime (CLR): A runtime for executing .NET code with garbage collection, security checks, type verification, thread management, assembly resolution and other runtime services.

• Common Intermediate Language (CIL): All .NET languages compile to this platform-independent language. This is the one true language of .NET (languages such as C# or VB are higher level abstractions).

• Assemblies: A new name and format for EXE and DLL files. More information on “assemblies” a bit later in this chapter.

Page 6: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-6 Copyright © Intertech, Inc 2014 Rev: 27

The Role of the Common Type System (CTS)

• The Common Type System (CTS) is a formal specification describing how "types" must be defined to work in the .NET world.

• "Type" is a general term describing the fundamental programming constructs used when building a .NET program (classes, interfaces, enumerations, structures and delegates).

• The CTS solves many language-interoperability issues, as all .NET languages share the same intrinsic data types.

• You will learn about each of these types over the next several chapters.

• Any .NET language is free to define its own corresponding keywords for each intrinsic basic data type (e.g., int, long, short, string, bool).

• Although each language will have unique keywords for fundamental data types, they are shorthand notations for the corresponding .NET data type.

• The .NET data types are defined in a key namespace named System.

• Some exceptions do exist. For example, languages may vary on their support for unsigned types, and may not have specific keywords.

Page 7: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-7

• Here is a summary of C#, VB and C++/CLI keywords for fundamental data types.

• Again notice, that each C# keyword is a shorthand for a corresponding data type in the System namespace.

.NET Data Type C# Keyword VB Keyword

C++/CLI Keyword

System.Byte byte Byte char

System.SByte sbyte SByte signed char

System.Int16 short Short short

System.Int32 int Integer int or long

System.Int64 long Long __int64

System.Uint16 ushort UShort unsigned short

System.Uint32 uint UInteger unsigned int or unsigned long

System.Uint64 ulong ULong unsigned __int64

System.Single float Single float

System.Double double Double double

System.Object object Object Object

System.Char char Char __wchar_t

System.String string String String

System.Decimal decimal Decimal Decimal

System.Boolean bool Boolean bool

Page 8: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-8 Copyright © Intertech, Inc 2014 Rev: 27

• Because C# keywords for fundamental data types are shorthand notations for the corresponding .NET data type, the following code is functionally equivalent:

• Notice that the C# int keyword is really just System.Int32.

private static void MakeSomeInts()

{

// Create a 32-bit integer with the C# keyword.

int i = 22;

Console.WriteLine("The value of i is {0}", i);

// Create another 32-bit integer using the .NET

// system type.

Int32 i2 = new Int32();

i2 = 22;

Console.WriteLine("The value of i2 is {0}", i2);

}

• Most programmers will prefer to use the C# keywords (int, double, short, bool, etc.) to define simple data types.

• However, it is very important to remember that the C# keywords are functionally equivalent to the corresponding .NET data type.

• For example, because the int keyword is a shorthand for System.Int32, you will find that the “simple data types” have built in functionality!

• We will explore this topic in more detail in Chapter 4; however consider the following screen shot. Notice how the int data type supports “real” functionality, including the following public methods.

Page 9: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-9

The Role of the Common Language Specification (CLS)

• The Common Language Specification (CLS) is a subset of the CTS.

• The CLS defines the minimal features a language must support to be considered a .NET language.

• If a complier abides by the CLS rules, all other .NET languages can use the resulting binaries in a uniform manner.

• By and large, only developers creating a new .NET language compiler need to engross themselves in the full details of the CLS (or CTS).

• Nevertheless, it is enlightening to examine some of these rules firsthand.

• CLS rule: All classes ultimately derive from System.Object:

• We will see the details of System.Object later in this class.

• For the time being, simply understand that everything in the .NET universe (integers, database connections, web pages, etc.) have the following shared functionality.

// The top-most class in the .NET world: System.Object

public class Object

{

// Reference equality by default. Override for value semantics.

public virtual bool Equals(object obj);

// Get hash value for object.

public virtual int GetHashCode();

// RTTI for this object.

public Type GetType();

// Override to produce state data in string format.

public virtual string ToString();

// Override to cleanup resources (non-deterministic destruction)

protected virtual void Finalize();

// System.Object also defines two static functions.

public static bool Equals(object objA, object objB);

public static bool ReferenceEquals(object objA, object objB);

}

Page 10: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-10 Copyright © Intertech, Inc 2014 Rev: 27

• CLS rule: Every class has exactly one direct base class but may implement multiple interfaces.

• Like Java, the CLS does not support multiple inheritance for class types. C++ developers take note!

• Like Java, the CLS allows a class to implement multiple interfaces.

• It is possible for an interface to have multiple base interfaces. In other words, multiple interface inheritance is supported.

• You will learn about interfaces in Chapter 3.

• CLS rule: Every type in an assembly must specify its visibility. Although a given language may have unique keywords, the basic visibility settings are:

• Public: visible to anyone, anywhere.

• Private: only visible to members of the defining type.

• Protected: only visible to members of the defining type or members of derived types.

• Internal: visible to any type defined within the assembly.

• In C#, the default access modifier for a (non-nested) type is internal.

• The default access modifier for a member of a type (a method, property, data member, constructor, nested type, etc.) is private.

• We will examine these defaults in more details beginning in Chapter 2, however here is a screen shot that illustrates the C# access modifier defaults.

Page 11: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-11

The Role of the Common Language Runtime (CLR)

• The Common Language Runtime (CLR) is the engine which manages your .NET applications. The CLR provides several useful services, including:

• Memory management via a garbage collector. When creating objects in C#, the CLR will automatically remove your memory when it is no longer used.

• Locating external libraries for the current application. The CLR will locate and load any external libraries a given application requires.

• Performing security checks when required, coordinating threads, handling file IO, etc.

• In many cases, you will not need to directly interact with the CLR, however it is possible.

• For example, the System.GC class allows you to interact with the garbage collector (however, you will seldom, if ever need to do so).

• At the system level, the crux of the CLR is contained within a library named mscoree.dll (the “Microsoft core execution engine”).

• An application which runs under the watchful eye of mscoree.dll is called managed code.

• In contrast, VB6/COM/native C++ applications are all unmanaged code….the CLR has no idea what to do with them.

• The only way managed and unmanaged code can communicate is through the .NET interoperability layer.

Page 12: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-12 Copyright © Intertech, Inc 2014 Rev: 27

The Role of .NET Base Class Libraries

• The .NET platform ships with a large body of existing code termed the Base Class Libraries (BCL).

• The BLC is also known as the Framework Class Library (FCL).

• Programmers use the BCL as a starting point for new application development.

• The BCL provides types which can be used to build web sites, desktop applications, communicate with relational databases and many, many other services.

• The BCL organizes its huge set of services using a concept called namespaces.

• A namespace is a logical, hierarchical organization of types similar to Java packages.

• All .NET types “live” in a namespace. To use a type, you must refer to it using its fully qualified name (i.e., including the namespace).

• Namespaces, in turn, are packed up into various assemblies. Recall that an “assembly” is just a new term for a DLL or EXE built with a .NET compiler.

• While there are many assemblies, there is one particular assembly which all .NET applications have automatic access to: mscorlib.dll.

• Remember: You always have access to mscorlib.dll when you build any new .NET project- you can’t get rid of it (nor would you want to)!

• Mscorlib.dll contains dozens of commonly used namespaces, with their related types.

• As you will see, mscorlib.dll has functionality for file IO operations, basic security and threading, XML manipulation, and other tasks.

• This library also defines the System data types (Int32, String, Boolean, etc.).

• In short, mscorlib.dll defines a number of very important primitives any .NET program is likely to require.

• Given this, it is certainly worth your time investigating what is in this all-important .NET library.

Page 13: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-13

• Here is mscorlib.dl as seen in the Visual Studio object browser tool.

• This single *.dll assembly has been organized into numerous namespaces.

• Each namespace has some number of types.

• Each type as some number of members (constructors, functions, properties, events, etc.)

• Here, we are looking at the BinaryReader class type, in the System.IO namespace, of the mscorlib.dll assembly.

• Always remember! Assemblies have namespaces and namespaces have types.

Page 14: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-14 Copyright © Intertech, Inc 2014 Rev: 27

• Assuming you have referenced the correct assembly, you can access a type defined within a namespace in a couple of different ways.

• You can use the fully qualified type name, e.g., namespace.typename.

• For example, the Console class lives in the System namespace:

class TesterMain

{

public static void Main()

{

// Namespace.TypeName.Method

System.Console.WriteLine("Hello world");

}

}

• Since typing namespaces is tedious, most .NET languages provide a shortcut mechanism.

• C# has a 'using' keyword (note the case and semicolon):

using System;

using System.Drawing;

using System.Collections;

• In contrast, VB has an 'Imports' keyword (note the lack of a semicolon):

Imports System

Imports System.Drawing

Imports System.Collections

Page 15: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-15

• Here is an example which makes use of two namespaces in mscorlib.dll.

• Because of the “using statements”, we can directly use Console and ArrayList.

• If we did not have these “using statements”, we would have to qualify Console and ArrayList.

• Also, because these two namespaces are within mscorlib.dll, we did not need to manually reference any external libraries (recall, we always have access to mscorlib.dll!)

using System;

using System.Collections;

namespace Test

{

class TesterMain

{

public static void Main()

{

// Use the System.Console class

Console.WriteLine("Hello world");

// Use the System.Collections.ArrayList class.

ArrayList myList;

myList = new ArrayList();

}

}

}

• The .NET base class libraries provide many, many more assemblies beyond mscorlib.dll.

• Each assembly contains more namespaces, which contain more types.

• For example, the System.Data.dll assembly contains core types for communicating with relational databases.

• The System.Core.dll assembly contains (among other things) the core types for using LINQ APIs.

• When you make a new project with Visual Studio, you will automatically be given a set of referenced libraries to use.

• If you need to use something in an assembly not initially referenced, you will need to do so manually.

• You will see how to work with this aspect of Visual Studio later in the class.

Page 16: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-16 Copyright © Intertech, Inc 2014 Rev: 27

• Clearly, your real challenge as a .NET developer is to learn about the wealth of namespaces and types found within the .NET base class libraries.

• MSDN (your local help system, which is also available online) has a specific book that describes each and every assembly, namespace and all the contained types.

• Here you will find ample sample code, details of a type’s functionality and numerous other details.

• Look up the topic ".NET Framework Class Library" for full details.

Page 17: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-17

The Role of Common Intermediate Language (CIL)

• .NET language compilers do not emit native platform code.

• Rather, they emit platform-independent instructions called the Common Intermediate Language (CIL).

• CIL is not interpreted. It is just-in-time compiled (JITed) at runtime.

• You may see CIL referred to by a few other names: IL or MSIL.

• CIL is just-in-time compiled (JITed) to the target platform at runtime.

• A block of code (method) is JITed when it is first called by a client. The JITed version of the code is cached for subsequent use.

• Alternately, you can use the ngen.exe utility to pre-JIT an entire assembly at the time of installation.

• Surprisingly, pre-JITing does not always mean better performance. UI intensive applications seem to benefit the most from pre-JITing.

Page 18: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-18 Copyright © Intertech, Inc 2014 Rev: 27

Viewing an Assembly’s Internals with ildasm.exe

• From a physical level, most assemblies are a single DLL or EXE file.

• This binary file (the assembly) is a collection of types (classes, interfaces, structures, enumerations, delegates) within some number of namespaces.

• Be aware that the name of the assembly does not need to match the name of the contained namespace(s).

• As well, it is perfectly possible for the same namespace to be defined across multiple assemblies.

• You can investigate the contents of an assembly using tools such as ILDasm.exe:

• This tool displays the manifest of an assembly and the internal CIL code instructions.

• This tool ships with the .NET SDK.

Page 19: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-19

• Every assembly contains a MANIFEST that documents key pieces of assembly information, including the following:

• The assembly version, culture, optional security settings, optional resources, and optional ‘strong name’ information (more details later).

• Any assembly level attributes.

• A list of all external assemblies required by the current assembly.

• You will learn more about the MANIFEST later in this class when we talk about how the CLR locates and loads assemblies (Appendix A).

Page 20: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-20 Copyright © Intertech, Inc 2014 Rev: 27

• Assemblies contain type metadata that describe each type in the assembly.

• You can view the metadata of an assembly by pressing the “ctrl+m” keystroke in ILDasm.exe.

• Metadata is a very important aspect of more advanced .NET programming techniques, including “reflection”.

• Although this course does not cover reflection in detail, you will be introduced to the topic where appropriate.

• FYI: Metadata is actually very useful. For example, the IntelliSense feature of Visual Studio reads metadata to display your choices in the code editor.

Page 21: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-21

A Survey of .NET Languages

• One of the core strengths of the .NET platform is the deep level of language interoperability.

• Assemblies can be developed and used by any .NET language.

• All .NET languages use the same namespaces, same types, and same runtime facilities. In fact, all .NET languages make use of the same base class libraries.

• Out of the box, the .NET SDK includes the VB, C#, C++/ CLI and F# programming languages.

• VB (also known as VB .NET):

• VB as we know it today has little in common with older versions of BASIC.

• Unlike VB 6 (or earlier) VB modern day VB is just as object oriented as Java, C# or C++.

• C# (pronounced “see sharp”):

• C# is a new member of the C family of languages, and therefore has a similar look and feel to that of C++ or Java.

• However, C# has a number of unique features, programming constructs and keywords which are not found in Java.

• Like other members of the C family, C# prefers terse code statements and keywords, uses curly brackets to define a level of scope, and terminates statements with a semi-colon.

• C++ / CLI:

• C++ developers using the Microsoft compiler can use some new keywords to build .NET assemblies rather than native binaries.

• This language is commonly used to wrap legacy C++ types and expose them to .NET.

• F#:

• F# is a language which began shipping with .NET 4.0.

• This is a "functional" language, which favors mathematical expressions (functions) as the unit of work.

• However, unlike pure functional languages (such as Scheme or LISP proper), F# has object orientated aspects and access to the .NET framework class libraries.

Page 22: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-22 Copyright © Intertech, Inc 2014 Rev: 27

• Be aware that numerous other .NET-aware languages exist, all of which have been developed by vendors other than Microsoft.

• .NET compilers exist for Ruby, COBOL, Python, Eiffel, Pascal, and more.

• Check out http://www.dotnetlanguages.net for more details (click on the Resources link on the top of the page to see the following list).

Page 23: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-23

Platform Independent .NET Development

• As mentioned earlier in this chapter, the .NET platform is not limited to the Windows family of operating systems.

• You can create and execute .NET assemblies on a variety of operating systems including Mac OS X, Solaris and numerous Linux distributions.

• You can also use C# and .NET to build iOS and Android applications.

• To understand how this is possible, there is yet another acronym to learn: CLI.

• Microsoft has submitted specifications on the C# programming language, the CLR, and a subset of the .NET base class libraries to ECMA International.

• Collectively, these documents are termed the "Common Language Infrastructure" or CLI.

• This provides the means for external (non-MS) companies and individuals to create custom builds of the .NET platform.

• This class will focus on Microsoft’s implementation of the C# language and the .NET platform.

• However, it is worth pointing out a popular alternative...

Page 24: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-24 Copyright © Intertech, Inc 2014 Rev: 27

• The Mono project (http://www.mono-project.com) is an open source distribution of the C# language and .NET platform to the Unix-based operating system (including Mac OS X).

• Mono includes, among other things, a C# compiler, a VB compiler, and implementations of Windows Forms, ASP.NET, ADO.NET, and the base class libraries.

• Assemblies built using Mono are ECMA compliant and, therefore, can be executed on any OS supporting the .NET platform.

Page 25: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-25

Building C# Applications Using the Command Line Compiler

• Although most developers will build .NET applications using a visual IDE, it is important to know how to work with the command line tools.

• As useful as a visual IDE might be, several advanced features (such as native generation of CIL, construction of “multi-file” assemblies, and so on) can only be done at the command line.

• As well, many important .NET APIs (such as WCF) require use of command line tools.

• Both VB and C# provide a command line compiler named vbc.exe and csc.exe, respectively.

• These command line compilers are included with the .NET SDK and are freely downloadable from Microsoft.

• Assume we have the following C# source code, created in a simple text editor.

• Remember that C# is a case sensitive programming language.

• All C# keywords are in lower case.

• Thus, “using” is syntactically correct. However, “Using” or “USING” are compiler errors.

• Likewise, Writeline() is *not* the same as WriteLine().

• All .NET namespaces, types and type members follow Pascal naming conventions (upper case the first letter of every word or acronym [System.Console, System.Collections.ArrayList, SqlConnection]).

Page 26: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-26 Copyright © Intertech, Inc 2014 Rev: 27

• Now let’s get to know the core options of the C# compiler.

• The first point of interest is to understand how to specify the sort of output file you are interested in building (e.g., console application, DLL, Windows application, etc.).

• Each choice is assigned a specific flag, which is sent into csc.exe.

Compiler Option Meaning in Life

/out Used to specify the name of the output file (e.g., MyAssembly.dll, WordProcessingApp.exe, etc.).

/target:exe This option will build an EXE console application.

This is the default file output type and thus may be omitted.

/target:library This option will build a DLL assembly.

/target:winexe This option will build an executable Windows GUI application.

• To compile the TestApp.cs file as a console application, you would use the following command set:

• You will see additional options of the command line compiler in your upcoming lab.

csc /target:exe TestApp.cs

Page 27: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-27

Building C# Applications Using Visual IDEs

• While it is important to know how to work with the command line tools of the .NET SDK, few programmers would want to build a complex application using the low level compiler.

• Thankfully, many .NET Integrated Development Environments (IDEs) exist.

• Some are free and open source, others come at a cost.

• In our class, we will use Microsoft Visual Studio.

• This IDE is the preferred professional tool for .NET developers.

• VS comes in many different versions, each of which supports various levels of tools and features.

• Consult http://www.microsoft.com/visualstudio/eng/products/compare for details of what is supported in each edition of the tool (and the related cost!)

Page 28: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-28 Copyright © Intertech, Inc 2014 Rev: 27

• If you do not have a copy of Visual Studio , and do not wish to spend the money to obtain a copy, you should know about the Microsoft Express family of products.

• Microsoft has released a completely free set of IDEs which fall under the “Express” family of tools.

• You can download an Express edition of C#, VB, C++, Web Development IDE and SQL Server.

• These Express IDEs are perfect for those working on smaller scale .NET application, students or for personal use at home.

• By and large, Express products are “lightweight” versions of the Visual Studio IDE.

• Consult http://www.microsoft.com/visualstudio/eng/products/compare for details.

Page 29: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

Copyright © Intertech, Inc 2008 Rev: 2 1-29

• In addition to these Microsoft IDEs, many open source IDEs exist as well.

• One very popular open source IDE is SharpDevelop (also known as #Develop).

• This IDE has an integrated debugger, visual designers, and support for multiple .NET programming languages.

• Consult http://www.icsharpcode.net/OpenSource/SD/Default.aspx for details.

Lab Exercise: Up and Running with C#

Page 30: NET, Java, and Mobile Training and Consulting from Intertech - … · 2014-03-08 · intrinsic basic data type (e.g., int, long, short, string, bool). • Although each language

The Philosophy of .NET

1-30 Copyright © Intertech, Inc 2014 Rev: 27

Chapter Summary

• The key .NET components are:

• Common Language Infrastructure (CLI): A set of ECMA standards that enable platform-independent .NET.

• Common Type System (CTS): A standard for type definitions and functionality that .NET languages CAN support.

• Common Language Specification (CLS): A subset of the CTS that all .NET languages MUST support.

• Common Language Runtime (CLR): A library of utility classes for building desktop, web, or any type of application and a runtime for executing .NET code with garbage collection and type verification.

• Common Intermediate Language (CIL): All .NET languages compile to this platform-independent language.

• Assemblies: A new name and structure for .exe and .dll files.

• .NET is language agnostic, but most development will be in either C# or VB.

• C# is a language specifically designed to build managed code.

• Modern day VB has little to do with VB6. Nowadays, VB is just as object oriented as C++, C# or Java.