Introduction to C#
What is. NET? Web Services Sharepoint Building Blocks (e. g. for Services) . . . . NET Applications Enterprise Servers Languages: SQL Server C#, Visual Basic, etc Biz. Talk . . . Runtime Common Type System Common Language Runtime Services: . NET and COM+ . NET Framework Operating System
What is the CLR (Common language runtime)? Base Class Library Support Thread Support COM Marshaler Type Checker Exception Manager Security Engine Debug Engine MSIL to Native Compilers (JIT) Code Manager Class Loader Garbage Collector (GC)
Meaning of CLR l Ouutput age of object using System; namespace Console. Application_Test_Csharp { public class Some. Class { int age; public int Get. Age() { age = 22; return age; } } public sealed class Program { public static void Main() { System. Console. Write("My age is "); Some. Class me = new Some. Class(); int my. Age; my. Age = me. Get. Age(); System. Console. Write. Line(my. Age); Console. Read. Line(); } } }
IL (Intermediate Language) l IL – special lowlevel language (like asm)
Metadata l Data for define objects (in *. exe or *. dll)
How work JIT (Just-intime compilation)? l l l Analyze of head (32 - or 64 -bit) Use MSCor. EE. dll (C: WindowsSystem 32MSCor. EE. dll for 32 bit) from MSCor. EE. dll initialize CLR, assembly entry point of Main() function of our program static void Main() { System. Console. Write. Line("Hello "); System. Console. Write. Line("Goodbye"); }
How work JIT?
What is the CTS (Common Type System)? l A set of common types l l l any language that runs in CLR should implement no syntax specified Languages often define aliases supports only single inheritance (unlike C ++) all types are inherited from System. Object (Object - type name, root of all other types, System - namespace) For example l l CTS defines System. Int 32 – 4 byte integer C# defines int as an alias of System. Int 32
What is the CTS? From MSDN
What is the CLS? l A specification of language features l l For example l l l how methods may be called when constructors are called subset of the types in CTS are allowed Code that takes UInt 32 in a public method UInt 32 is not in the CLS Can mark classes as CLS-compliant l not marked is assumed to mean not compliant
What is the CLS?
The Class Libraries l The common classes used in many programs l l like Java Class Library eg. l l System. Console. Write. Line XML, Networking, Filesystem, Crypto, containers Can inherit from many of these classes Many languages run on. NET framework l l C#, C++, J#, Visual Basic even have Python (see Iron. Python)
Assemblies l Code contained in files called “assemblies” l code and metadata. dll as before to run: public static l types l l l private: local directory, not accessible by others § l l void Main(string[] args) eg. Wolfram. NETLink shared: well-known location, can be GAC strong names: use crypto for signatures § then can add some versioning and trust
COM vs. NET l Historically, COM provided this integration l l l support for interfaces and interaction given a GUID, lookup the type library dynamically instantiate class do RPC to make calls in many cases Difficult to get right l l software engineering problems not type safe at all
ASP. NET and ADO. NET l Use. NET languages in web pages l l l thus can write typesafe code server-side or client-side Sharepoint interactions can log in Use the CLR to access databases l l in the manner of ODBC provides classes for access
Windows Power. Shell l New shell originally for MS Vista l l available for Win. XP/2 k 3 native. NET l l instantiates arbitary. NET classes and accesses them Also can access COM objects Allows better interaction with programs Can it surpass bash and others?
First C# Program using System; namespace Test { int a = 137; class Hello { public static void Main(string[] args) { Console. Write. Line(“Hello {0}”, a); } } }
Constructions of Note l using l l namespace l like import in Java: bring in namespaces disambiguation of names like Internet hierarchical names and Java naming class l l like in Java single inheritance up to object
Constructions of Note l Console. Write(Line) l l Takes a formatted string: “Composite Format” Indexed elements: e. g. , {0} l l can be used multiple times only evaluated once {index [, alignment][: formatting]} also can use as in Java l “Test “ + a
More C# : basic inheritance class A { protected int a; public virtual void print() { Console. Write. Line(“a = “ + a); } } class B : A { public override void print() { Console. Write. Line(“a really = “ + (a + 137)); } }