Professional Documents
Culture Documents
NET Basics
Daragh Byrne EPCC
Purpose
Microsoft .NET Framework:
Microsoft Intermediate Language (MSIL) Common Language Runtime (CLR) Class Libraries Language Compilers Distributed and Web-based computing
.NET Framework
Microsoft .NET is a set of Microsoft software technologies for connecting information, people, systems and devices
http://www.microsoft.com/net/basics/whatis.asp
Evolution
Next Generation of COM:
Component oriented software is a good thing:
Win32/C-style APIs are outdated COM was step in right direction, but painful to program with COM was restricted to VB, C++ Binary compatibility/portability an issue: x86 version of COM component needed to be compiled for e.g. PowerPC Memory management also a pain
Web Services:
Interoperability is key in the connected world:
Require open standards for interoperability and leveraging legacy code
Class libraries:
Common functionality that can be used by all languages Includes Windows Forms for GUI development
Language compilers:
C#, C++, VB
Distributed computing:
Networking using sockets, Remoting, Web Services and Applications using ASP.NET
Targeting .NET
Compiled to
MSIL
Runs on
CLR
Compiled to
Target language for all .NET compilers JIT-compiled (Just-In-Time) by the CLR to native code:
Very efficient late compilation approach
MSIL Example
Example: method body:
// Code size 21 (0x15) .maxstack 2 .locals init ([0] string CS$00000003$00000000) IL_0000: ldarg.0 IL_0001: ldfld string NDoc.Core.HtmlHelp::_projectName IL_0006: ldstr ".hhk IL_000b: call string [mscorlib]System.String::Concat(string, string) IL_0010: stloc.0 IL_0011: br.s IL_0013 IL_0013: ldloc.0 IL_0014: ret
10
.NET Executable
(Stored as Windows Portable Executable file)
Execute MSIL entry point (verifies code, starts compilation and execution)
mscoree.dll
11
Reference types are instances of classes, interfaces, arrays, delegates that the programmer deals with via references
12
Used system-wide:
Serialization of objects to network, disk, in Web Services Cross-language interoperability Intellisense in Visual Studio We use it in our Grid Services software
14
15
16
17
Language Compilers
Over 20 different languages supported to date:
C#, VB, C++ Perl, Python, Scheme, F#, Fortran, J#, write your own!
18
19
Distributed Computing
Remoting and Web Services allow remote procedure calls Remoting is used to make calls between .NET Application Domains:
Built-in to CLR
20
~110 Mb Support at http://msdn.microsoft.com Visual Studio .NET is available at a reduced rate for academic institutions
21
22
C# Features (1/2)
Programming language of choice for the .NET platform:
Microsofts preferred language
Strongly-typed:
Enforced by the compiler and the runtime As are all .NET languages
Object-oriented:
Every object is an instance of a particular Type Types are class, interface, enum, struct
23
C# Features (2/2)
Close coupling with managed code services:
Garbage collection, threading
Can access raw pointers using unsafe code blocks Properties are a first class language feature:
Unlike Java where accessor methods must be coded Syntactic sugar, but nice!
24
Semi-deterministic finalization:
Using IDisposable
25
Namespaces
Means of dividing related classes logically Avoid name clashes Analogous to Java packages, C++ namespaces:
MyCompany.MyApplication.Module
26
Sample Program
//Person.cs: using System; using SomeLib; namespace MyApplication { class Person { private string name_; public string Name { get { return name_; } set { if(value == null) throw new ArgumentNullException(name); name_ = value; } } public static void Main { Person p = new Person(); p.Name = Daragh; Console.WriteLine(p.Name); } } }
Compile as follows:
Produces Person.exe C:/> csc Person.cs /reference:SomeLib.dll
Execute:
C:/> person output: Daragh
27
Using C#
Very intuitive at first if you are a Java programmer:
Some differences will soon be noticed
28
foreach
foreach(element e in array) foreach(element e in somethingEnumerable)
29
30
Attributes
Can add custom metadata to your types:
public class SomeType { [WebMethod] public string SomeMethod() { } }
31