Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to C#
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};
}
return root;
}
}
Writing applications in C#
Writing applications in C#
We can now write a simple program
containing a Main method which uses this
library class
The only thing we need to do is to
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);
Online-help and
Debugging
in different languages
Each project contains a number of files including
source files, executables and xml files containing
information about the project and resources
Visual Studio .NET
We can add each of our previous 3 example
applications (projects) to a single solution
Learning C Sharp
Its a simple matter to flip between them and
design views
We will look more into visual programming in a
future lecture
Basic C# language concepts
C# has a rich C-based syntax much like C++ or
Java
The concepts of variables, program statements,
control flow, operators, exceptions etc are the same
in C# as in C++ and Java
Like Java, everything in C# is inside a class{}
We will only look at those C# language issues
which differ from those we are already familiar with
Basic C# language concepts
Primitive types
These are types representing the basic types we
are familiar with – integers, floats, characters
etc
In C# they are part of the FCL so they are
defaults to zero)
Basic C# language concepts
Arrays
Array declaration and initialization is
for {}
do{} while()
etc
using System;
int total = 0;
error
Int32 x = 10;
while(x--) DoSomething();
Basic C# language concepts
C# has the standard set of operators we are
familiar with
Also it has operators such as is and typeof
for testing variable type information
C# provides operator overload functions (as
does C++ but not Java) to enable standard
operators to be applied to non-primitive
types
Basic C# language concepts
Operator category Operators
Arithmetic + - * / %
Logical (boolean and bitwise) & | ^ ! ~ && || true false
String concatenation +
Increment, decrement ++ --
Shift << >>
Relational == != < > <= >=
Assignment = += -= *= /= %=
&= |= ^= <<= >>=
Member access .
Indexing []
Cast ()
Conditional (Ternary) ?:
Delegate concatenation and removal + -
exception handling
Use of the try{} catch{} mechanism as in Java
and C++
Functions should not return error conditions but
classes
Basic C# language concepts
public static void ExceptionExample()
{
// try-catch
try
{
Int32 index = 10;
while(index-- != 0)
Console.WriteLine(100/index);
}
catch(DivideByZeroException)
{
Console.WriteLine("A divide by zero exception!");
}
Console.WriteLine("Exception caught; code keeps running");
// 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