You are on page 1of 30

Programming in C#

Introduction

2012 BlueSignet LLC. All rights reserved.

Welcome to C#
Wow!! Look at you! Look at what you have

accomplished

2012 BlueSignet LLC. All rights reserved.

What is C#
A Pure OOP language developed by Microsoft in

the late 1990s (release in 2001)


Microsofts response to Java

Some have accused MS of ripping off Java

The language was designed to be a catalyst for

the newly developed .Net Framework

Supposed to attract developers that were already


familiar with C & C++ to .Net development

Originally called COOL (C-like Object Oriented

Language)
2012 BlueSignet LLC. All rights reserved.

What is C#
The most common implementation of the language

Works in conjunction with a Microsoft language framework called


.Net
Compiles code into Intermediate Language (IL) AKA Byte Code

C#

Also called Common Intermediate Language (CIL)

Which is interpreted by a CLR (Common Language Runtime)


That runs overtop of a framework layer often referred to as CLI
(Common Language Infrastructure)

Compile

2012 BlueSignet LLC. All rights reserved.

CIL

Interpret

CLI

CLR
0111001011010
1010111101011
0001011011010

What is C#
Technically, the language CAN be compiled to

machine language in the same way as C or C++,


but not common
Despite its Microsoft roots, C# is legitimately
recognized as an ISO standard language

Thanks to the combined efforts of Microsoft, Intel


and HP

2012 BlueSignet LLC. All rights reserved.

Where does the name come from?


The C# name was chosen because the musical

notation # tells the reader of the sheet music that


the note is a slightly higher pitch than the root note
(One better than the previous)
In essence, C# has a similar symbolic meaning as
C++

2012 BlueSignet LLC. All rights reserved.

Language Philosophies
Top down design is for suckers!

Entities do not need to be defined top to bottom as they do in C++

Datatypes are all objects

Each type inherits from the Object class


Supports Boxing and Unboxing

Boxing Allows datatypes to be encapsulated within a generic


datatype
Unboxing Allows a generic datatype to be typecast as a specific
datatype

Namespaces are good for your health

Every class should be encapsulated within a namespace to prevent


conflicts
Assuming (using) namespaces are almost exactly like C++

Application global variables are BAD!!!

All global variables are encapsulated within a class

2012 BlueSignet LLC. All rights reserved.

Language Philosophies
We dont need no stinkin class template

Class are written as a single declaration No need for headers and


implementations!

Properties should be simple!

No need for setter / getter methods


Actions can be defined in the property declaration

Documentation should be built-in to the code

Developers can create XML formatted comments, allowing IDEs to


provide instant documentation about methods

Pointers are unsafe

If a pointer must be used it is allocated in a different section of


memory marked as unsafe
Pointer memory is unmanaged (You must clean up after yourself!)

Managed Memory cannot explicitly be deallocated

Handled by the garbage collector

2012 BlueSignet LLC. All rights reserved.

IDE
Microsoft Visual Studio

Visual C#

MonoDevelop

GUI development uses GTK# (which is GTK+ With


.Net hooks)
GTK GIMP ToolKit

DotGNU

Open Source C# Compiler

There are others!


2012 BlueSignet LLC. All rights reserved.

How its Implemented


Application

Command line or GUI program

ASP

Server side scripting

High-end Web Content

Silverlight

Web Services

Provides a SOAP Interface for interacting with remote


applications

Scripting

Used as an embedded language (Sometimes called


CSScript)

2012 BlueSignet LLC. All rights reserved.

How it Works
Develop Code

2012 BlueSignet LLC. All rights reserved.

How it Works
Develop Code

csc.exe file.cs

Compile Code to IL

IL

2012 BlueSignet LLC. All rights reserved.

How it Works
Develop Code

csc.exe file.cs

Compile Code to IL
Execute Code
0110101011001010010100101101
0101001010110101001010010100

Program.exe
Common Language Runtime

.Net Framework
2012 BlueSignet LLC. All rights reserved.

IL

The Goods and the Bads


The Goods

If you come from the C Lineage (especially Java), the


language is easy to learn
Very intuitive Namespace / Object structures
The language specifications allow compilers to run on
pretty much any system (platform independent)
Architecture indifferences are cured by the compiled
Intermediate Language
Its memory management philosophy makes the code
relatively safe to run
Language design provides a very intuitive interface for
autocompleting
Integrated security

2012 BlueSignet LLC. All rights reserved.

The Goods and the Bads


The Bads

Very Microsoft Focused


Developers are limited to the language design
Has the ability to stunt the growth of programmers

It is easier for a C/C++ trained developer to migrate to


C#, but very difficult to do the opposite

Due to performance overhead, code runs very slow


(in comparison to compiled languages such as C)

2012 BlueSignet LLC. All rights reserved.

The Goods and the Bads


The Bads

Used in Web Development, it really only supports


running on Windows Server IIS web servers
IIS is not typically the fastest web server but is easy
to configure
Generally used in code-behind for ASP.NET web
sites
Mobile development only officially used on Windows
Mobile devices (except Mono on iOS)

2012 BlueSignet LLC. All rights reserved.

Our Environment
Operating System

Microsoft Windows
Ubuntu Linux

Framework

Microsoft .Net Framework

Integrated Development Environment

MonoDevelop

Although we will be using a fully loaded IDE, we will


also expose you to the command line compiler

Visual Studio Express

2012 BlueSignet LLC. All rights reserved.

Quick Warning
We WILL sporadically introduce random classes

throughout our lessons


The .Net Framework is so interconnected that this
is pretty much inevitable

2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#

// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
Main Class
The class container for
the Main Method. This
name is arbitrary
// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
Main Method
Program entry point. Must be
declared as public and static.
The method must also be called
Main
// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
Console Output
The WriteLine method writes a
line of text to STDOUT

// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
System
Invoke the System
Namespace.

// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
Console
Invoke the Console class

// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
WriteLine
Invoke the static WriteLine
method

// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}
2012 BlueSignet LLC. All rights reserved.

Hey Buddy in C#
// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}

2012 BlueSignet LLC. All rights reserved.

>csc.exe HeyBuddy.cs

Hey Buddy in C#
// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}

2012 BlueSignet LLC. All rights reserved.

>csc.exe HeyBuddy.cs

Hey Buddy in C#
// HeyBuddy.cs
class MainClass
{
public static void Main()
{
System.Console.WriteLine("Hey Buddy!");
}
}

>csc.exe HeyBuddy.cs
>HeyBuddy.exe
Hey Buddy!
>_

2012 BlueSignet LLC. All rights reserved.

>csc.exe HeyBuddy.cs

Prerequisites
Programming in C

Introduction to Object Oriented Programming


Programming in C++
Programming in Objective-C (Preferred)
Programming in Java

2012 BlueSignet LLC. All rights reserved.

Programming in C#
The End?

2012 BlueSignet LLC. All rights reserved.