C+sharp

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

History of C#

• 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

Base Class Library Support

Thread Support COM Marshaler

Type Checker Exception Manager

Security Engine Debug Engine

MSIL to Native Code Garbage


Compilers (JIT) Manager Collector (GC)

Class Loader

From MSDN
Common Language Infrastructure.

• CLI allows for cross-language development.


• Four components:
– Common Type System (CTS)
– Meta-data in a language agnostic fashion.
– Common Language Specification – behaviors that
all languages need to follow.
– A Virtual Execution System (VES).
Common Type System (CTS)
• A specification for how types are defined and how
they behave.
– no syntax specified
• A type can contain zero or more members:
– Field
– Method
– Property
Common Type System (CTS)
• CTS also specifies the rules for visibility and access to
members of a type:
– Private
– Family
– Family and Assembly
– Assembly
– Family or Assembly
– Public
Common Type System (CTS)

• Other rules
– Object life-time
– Inheritance
– Equality (through System.Object)
Common Type System (CTS)

• Languages often define aliases


• For example
– CTS defines System.Int32 – 4 byte integer
– C# defines int as an alias of System.Int32
– C# aliases System.String as string.
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.

–For example, C# supports enumerations, type-safe value types which are


limited to a defined set of constant variables, and structs, which are user-
defined value types .
–Java doesn't have enumerations, but can specify a class to emulate them .

• Unlike Java, C# has the useful feature that we can


• Unlike Java, C# has the useful feature that we can overload various operators.
• However, polymorphism is handled in a more complicated fashion, with derived
class methods either overriding or hiding super class methods.
• In Java, multi-dimensional arrays are implemented solely with single-dimensional
arrays where arrays can be members of other arrays. In addition to jagged arrays,
however, C# also implements genuine rectangular arrays.
C# versus C++ (Differences)
•C# uses delegates-type-safe method pointers. These are used to
implement event-handling.
•Although it has some elements derived from Visual Basic and Java,
C++ is C#'s closest relative.
•In an important change from C++, C# code does not require header
files. All code is written inline.
•The .NET runtime in which C# runs performs memory management
takes care of tasks like garbage collection. Because of this, the use
of pointers in C# is much less important than in C++.
•Pointers can be used in C#, where the code is marked as unsafe, but
they are only really useful in situations where performance gains
are at an absolute premium.
•Generally speaking, all C# types is ultimately derived from the object
type.
C# versus C++ (Differences)
• There are also specific differences in the way that certain
common types can be used. For instance, C# arrays are
bounds checked unlike in C++, and it is therefore not
possible to write past the end of a C# array.
• C# statements are quite similar to C++ statements. To note
just one example of a difference: the 'switch' statements
has been changed so that 'fall-through' statements has
been changed so that 'fall-through' behavior is disallowed.
• As mentioned above, C# gives up on the idea of multiple
class inheritance. Other differences relating to the use of
classes are: there is support for class 'properties' of the
kind found in Visual Basic, and class methods are called
using the . operator rather than the :: operator.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy