Simple C Tutorial
Simple C Tutorial
C# Tutorial
• Introducing the .NET framework
• Comparing C# to C++ and Java
• Getting Started
• Variable Types
• Arrays
• Operators
• Flow Control
• Introducing Classes, Structs and Namespaces
• Class Declaration
• Introducing Methods
• Polymorphism (Inherited Methods)
• Constants, Fields, Properties and Indexers
• Delegates and Events
• Exceptions
• Code Documentation
Introducing the Microsoft .NET Framework
• .NET (dot-net) is the name Microsoft gives to its general
vision of the future of computing, the view being of a
world in which many applications run in a distributed
manner across the Internet.
• We can identify a number of different motivations
driving this vision.
– Object-oriented programming
– Compiled once and run everywhere.
– Service-oriented application
• .NET is Microsoft JVM?
• .NET has been built upon open standard technologies like
XML and SOAP and is towards more open standards
rather than Microsoft its proprietary tendencies.
Introducing the Microsoft .NET Framework
• At the development end of the .NET vision is the .NET
Framework (Microsoft JDK?) that contains:
– The Common Language Runtime,
– The .NET Framework Classes, and
– higher-level features like ASP.NET and WinForms for
developing desktop applications.
• The Common Language Runtime (CLR) (Microsoft
JRE?) manages the execution of code compiled for the
.NET platform. The CLR has two features:
– Its specification has been opened up so that it can be ported to
non-Windows platforms.
– Any number of different languages can be used to manipulate
the .NET framework classes, and the CLR will support them.
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 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
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'
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.
Getting Started – Hello World!
• In order to use C# and the .NET framework classes, first
install the .NET framework SDK.
• Write the C# code.
using System;
public class HelloWorld {
public static void Main() {
// This is a single line comment.
/*
* This is a multiple line comment.
*/
Console.WriteLine("Hello World!");
}
}
• To compile the program on Mono, use the command:
mcs HelloWorld.cs (csc HelloWorld.cs in .NET
framework SDK)
• To run the program on Mono: mono HelloWorld.exe
Variable Types
• C# is a type-safe language. Variables are declared as
being of a particular type, and each variable is
constrained to hold only values of its declared type.
• Variables can hold either value types or reference types,
or they can be pointers.
• A variable of value types directly contains only an object
with the value.
• A variable of reference type directly contains a reference
to an object. Another variable many contain a reference
to the same object.
• It is possible in C# to define your own value types by
declaring enumerations or structs.
C# Pre-defined Value Types
C# Type .Net Framework Type Signed Bytes Possible Values
sbyte System.sbyte Yes 1 -128 to 127
short System.Int16 Yes 2 -32768 to 32767
int System.Int32 Yes 4 231 to 231 - 1
long System.Int64 Yes 8 263 to 263 - 1
byte System.Byte No 1 0 to 255
ushort System.Uint16 No 2 0 to 65535
uint System.Uint32 No 4 0 to 232 - 1
ulong System.Uint64 No 8 0 to 264 - 1
float System.Single Yes 4 ±1.5 x 10-45 to ±3.4 x 1038 with 7
significant figures
class DisplayFile
{
static void Main(string[] args)
{
StreamReader r = new StreamReader(args[0]);
string line;