Professional Documents
Culture Documents
Introduction to C#
interpreted
C# programs runs in native machine code
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#
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 view the code from each project by
selecting the appropriate tab
Each project must be built (compiled)
before executing and any of the projects
in a solution can be selected to be
executed
Visual Studio .NET
Visual Studio .NET
It is a simple matter to reference a .dll from
a project
We can check all the references that a
project makes by expanding the References
menu item in the solution explorer
Notice for the windows project, lots of
FCL classes are referenced
Visual Studio .NET
Visual Studio .NET
An important feature of VS is its ability to enable
visual programming
Essentially we can create fairly sophisticated
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
Int32 x=10;
String s=“Hello”;
array
Arrays themselves are reference types although
their elements can be value types, or reference
types
Basic C# language concepts
Control flow statements in C# are the same
as for C++ and Java
if {} else{}
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 + -
// 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