C+sharp
C+sharp
C+sharp
• Developed by Microsoft.
• Based on Java and C++, but has many
additional extensions.
• Java and C# are both being updated to keep
up with each other.
• Cross-development with Visual Basic, Visual
C++, and many other .NET languages.
Microsoft’s .NET Technologies
The Class Libraries
• The common classes that are used in many
programs
– System.Console.WriteLine
– XML, Networking, Filesystem, Crypto, containers
– Can inherit from many of these classes
• Many languages run on .NET framework
– C#, C++, J#, Visual Basic
.NET History
The Class Libraries
CLR and JIT compiling.
• C#, like Java, is executed
indirectly through an
abstract computer
architecture called the CLR.
– CLR => Common Language
Runtime.
– Abstract, but well defined.
• C# programs are compiled
to an IL.
– Also called MSIL, CIL
(Common Intermediate
Language) or bytecode.
CLR and JIT compiling.
• The CLR transforms the CIL to assembly
instructions for a particular hardware
architecture.
– This is termed jit’ing or Just-in-time compiling.
– Some initial performance cost, but the jitted code
is cached for further execution.
– The CLR can target the specific architecture in
which the code is executing, so some performance
gains are possible.
CLR and JIT compiling.
• All .NET languages compile to the same CIL.
• Each language actually uses only a subset of
the CIL.
• The least-common denominator is the
Common Language Specification (CLS).
• So, if you want to use your C# components in
Visual Basic you need to program to the CLS.
CLR versus CLI.
• CLR is actually an
implementation by
Microsoft of the CLI
(Common Language
Infrastructure) .
• CLI is an open
specification.
• CLR is really a platform
specific
implementation.
from wikipedia.org
The CLR Architecture
Class Loader
From MSDN
Common Language Infrastructure.
• Other rules
– Object life-time
– Inheritance
– Equality (through System.Object)
Common Type System (CTS)
From MSDN
Common Language System
• A specification of language features
– how methods may be called
– when constructors are called
– subset of the types in CTS which are allowed
• For example
– Code that takes UInt32 in a public method
– UInt32 is not in the CLS
• Can mark classes as CLS-compliant
– not marked is assumed to mean not compliant
First C# Program
using System;
namespace Test
{
class ExampleClass
{
static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}
}
Constructions of Note
• using
– like import in Java: bring in namespaces
• namespace
– disambiguation of names
– like Internet hierarchical names and C++ naming
• class
– like in C++ or Java
– single inheritance up to object
Constructions of Note
• static void Main()
– Defines the entry point for an assembly.
– Four different overloads – taking string arguments
and returning int’s.
• Console.Write(Line)
– Takes a formatted string: “Composite Format”
– Indexed elements: e.g., {0}
• can be used multiple times
• only evaluated once
– {index [,alignment][:formatting]}
C#
• Not all of the supported languages fit entirely neatly into the .NET
framework, but the one language that is guaranteed to fit in perfectly is
C#.
• C# (C Sharp), a successor to C++, has been released in conjunction with
the .NET framework.
C# design goals:
–Be comfortable for C++ programmer
–Fit cleanly into the .NET Common Language Runtime (CLR)
–Simplify the C++ model
–Provide the right amount of flexibility
–Support component-centric development
C# versus Java (Similarity)
• C# and Java are both languages descended from C and C++.
• Each includes advanced features, like garbage collection, which remove
some of the low level maintenance tasks from the programmer. In a lot of
areas they are syntactically similar.
• Both C# and Java compile initially to an intermediate language:
–C# to Microsoft Intermediate Language (MSIL), and Java to Java
Java bytecode.
–In each case the intermediate language can be run -by
interpretation or just-in-time compilation -on an appropriate virtual
machine. In C#, however, more support is given for the further
compilation of the intermediate language code into native code.
• Like Java, C# gives up on multiple class inheritance in favor of a single
inheritance model. C# supports the multiple inheritance of interfaces.
C# versus Java (Differences)
• C# contains more primitive data types than Java, and also allows more extension
to the value types.