Beruflich Dokumente
Kultur Dokumente
Introduction to C#
Contents
Introducing C#
Writing applications in C#
Visual Studio .NET
Basic C# language concepts
Introducing C#
C# (C-sharp) is a language that targets one
and only one platform, namely .NET
That doesnt mean its restricted to Windows
There are now .NET implementations on
other operating systems including Linux
As long as we get to grips with object oriented
programming, C# is a simple language to
master
Introducing C#
Writing applications in C#
An application in C# can be one of three types
Console application (.exe)
Windows application (.exe)
Library of Types (.dll)
The .dll is not executable
These 3 types exclude the more advanced
web-based applications
Writing applications in C#
Before we look at the more detailed structure
and syntax of C# programs, we will show a
simple example of each type
In each case we will use the command line
compiler (csc) to create the binary (assembly)
Later in this lecture we will look at using
Visual Studio to create our applications
Writing applications in C#
Writing applications in C#
using System;
class Square_Root
{
static void Main(string[] args)
{
double a,root;
do
{
Console.Write("Enter a number: ");
a=Convert.ToDouble(Console.ReadLine());
if (a<0)
Console.WriteLine(Enter a positive number!");
} while (a<0);
root=a/2;
double root_old;
do
{
root_old=root;
root=(root_old+a/root_old)/2;
} while (Math.Abs(root_old-root)>1.0E-6);
Console.WriteLine("The square root of " + a + " is " + root);
}
}
Writing applications in C#
Writing applications in C#
Writing applications in C#
Writing applications in C#
using System;
using System.Drawing;
using System.Windows.Forms;
class App{
public static void Main(){
Application.Run(new MenuForm());
}
}
class MenuForm:Form{
public MenuForm(){
this.ContextMenu = new ContextMenu(SetupMenu());
this.Menu = new MainMenu(SetupMenu());
}
MenuItem[] SetupMenu(){
MenuItem file = new MenuItem("&File");
file.MenuItems.Add("Exit", new EventHandler(OnExit));
MenuItem messages = new MenuItem("&Message Boxes");
EventHandler handler = new EventHandler(OnMessageBox);
messages.MenuItems.Add("Message Box 1", handler);
messages.MenuItems.Add("Message Box 2", handler);
return new MenuItem[]{file, messages};
}
void OnExit(Object sender, EventArgs args){
this.Close();
}
void OnMessageBox(Object sender, EventArgs args){
MenuItem item = sender as MenuItem;
MessageBox.Show(this, "You selected menu item - "+item.Text);
}
}
Writing applications in C#
Writing applications in C#
Writing applications in C#
Writing applications in C#
Example 3 A library
We can take some of the code from
example 1 for computing the square root
and make it a library
It will not have a Main method
We indicate that we are compiling to a
.dll using the /Target:library option
Writing applications in C#
using System;
public class Square_Root_Class
{
public static double calcRoot(double number)
{
double root;
root=number/2;
double root_old;
do
{
root_old=root;
root=(root_old+number/root_old)/2;
} while (Math.Abs(root_old-root)>1.0E-6);
return root;
}
}
Writing applications in C#
Writing applications in C#
Writing applications in C#
using System;
class Square_Root
{
static void Main(string[] args)
{
double a,root;
do
{
Console.Write("Enter a number: ");
a=Convert.ToDouble(Console.ReadLine());
if (a<0)
Console.WriteLine("Please enter a positive
number!");
} while (a<0);
root=Square_Root_Class.calcRoot(a);
Console.WriteLine("The square root of " + a + " is " +
root);
}
}
Writing applications in C#
Primitive types
Basic C# language
concepts
C# Primitive
C# Alias
Description
Boolean
bool
Indicates a true or false value. The if, while, and dowhile constructs require expressions of type Boolean.
Byte
byte
Char
char
Decimal
decimal
Double
double
Single
float
Int32
int
Int64
long
SByte
sbyte
Int16
short
UInt32
uint
UInt64
ulong
UInt16
ushort
String
string
Object
object
String variable
s is ax=10;
reference (memory address) of some
Int32
String
memory which
storess=Hello;
the string (which defaults to null)
Int32 variable x is the actual value of the integer (which
defaults to zero)
Arrays
Array declaration and initialization is
similar to other languages
// A one dimensional array of 10 Bytes
Byte[] bytes = new Byte[10];
foreach
(type
identifier in arrayName)
There
is no loop
counter
If the loop counter variable is required in the loop,
then a for construct must be used
The type must match the type of elements in the array
The array cannot be updated inside the loop
Operators
Arithmetic
&
String concatenation
Increment, decrement
++--
Shift
<<
>>
Relational
==
!=
<
+=
-=
*=
&=
|=
^=
<<=
Assignment
Member access
Indexing
[]
Cast
()
Conditional (Ternary)
?:
Object creation
New
Type information
is
checked
>
&&
<=
||
>=
/=
>>=
sizeof
->
typeof
unchecked
[]
&
%=
true false
Error handling
This is always done in C# using structured
exception handling
Use of the try{} catch{} mechanism as in Java
and C++
Functions should not return error conditions but
should throw exceptions
This is done universally by methods in FCL
classes
// try-finally
try
{
return;
}
finally
{
Console.WriteLine("Code in finally blocks always runs");
}
}
Summary
We have looked at different types of simple C#
applications
Console applications
Windows applications
Libraries (reusable types)
We have looked at the basics of using Visual
Studio.NET
We have looked at some C# language issues from the
point of view of differences from C++ and Java