Intro C# Intro C# 1 Microsoft's.NET platform and Framework.NET Enterprise Servers Visual Studio.NET.NET Framework.NET Building Block Services Operating system on servers, desktop, and devices Web Services Web Forms Windows Forms Data and XML classes Framework Base Classes Common Language Runtime
Using Visual Studio.NET Intro C# 2 Create a project -- projects are directories where you can have a single application solutions are directories for many projects (subdirectories) File New Project select "Windows Application" for Designer use "Empty Project" for console application select a disk location specify a name for the directory directory (432?) don't check create a solutions Create or view a source file or with "empty project" Project Add New Item under Local Project Items select Code file and provide a name (p2.cs?) with "windows application" View code (F7)
example Intro C# 3
design view Intro C# 4 In design view right click form to set properties Project creates several files: Program.cs Form1.cs Form1.Designer.cs Can be renamed. Program.cs is the starting point of application. Application logic Form1.cs is a partial class for the GUI Form1.Designer is a partial class that contains InitializeComponents() the method where Toolbox places code of "drag & drop" interface building.
Intro C# 5 With "empty project" in the System Explorer (left most selection area) right click on References. In dialog select to add(if not present): System.dll System.Drawing.dll System.WindowsForms.dll You now have an empty code file in a project. Edit / enter text With "windows application" your development environment will start with Designer Toolbox -- a "draw your interface" tool. With this approach your *.dlls are set for you. Both projects: Use Build to compile (build) and Debug (to run without debugger). Hello432 project I deleted Program.cs and Form1.cs and From either the Project Add existing item menu option or by right clicking on the Hello432 item in the Solution Explorer and selecting Add to add the file Hello432.cs (from visual studio 2003 )
code view Intro C# 6 Solution Explorer right click *.cs select view code to edit Build menu to compile Debug menu to run Compile messages in output. Click errors to select in code window
Hello432.cs "extends" Intro C# 7 /*Introductory Hello CS432 C# project in Windows Forms * uses console output to show program execution. * Comparable to Smalltalk hello432.st example. * * Mike Barnes 2/24/03 */ using System; using System.Drawing; using System.Windows.Forms; class Hello432 : Form { Button b; int clickcount; string blabel; bool bstate; Random rand; construct the application framework and execute it public static void Main() { System.Console.WriteLine("Main: enter"); Application.Run(new Hello432()); System.Console.WriteLine("Main: leave"); }
Intro C# 8 public Hello432() { System.Console.WriteLine("Hello432: enter"); Width = 100; // set size of form Height = 50; blabel = "Hello CS 432!"; // initialize blabel1 value bstate = true; // initialize boolean button state clickcount = 0; // initialize clickcount // create random object and initialize from system time rand = new Random((int)DateTime.Now.Ticks); b = new Button(); // create button b.parent = this; b.text= blabel; b.dock = DockStyle.Fill; b.click += new EventHandler(ButtonOnClick); System.Console.WriteLine("Hello432: leave"); } add an event "listener"
/// <summary> /// Get and return a random bright color /// (upper third 0..255 range) /// An example of UML tagged auto documentation. /// </summary> Intro C# 9 Color RandBrightColor() { int r,g,b; r = rand.next(171, 255); g = rand.next(171, 255); b = rand.next(171, 255); return Color.FromArgb(r, g, b); } Static class and method
Event handler methods respond to events. They always have 2 arguments Intro C# 10 // process button clicks void ButtonOnClick(object obj, EventArgs e) { System.Console.WriteLine("ButtonOnClick: enter"); clickcount++; if (clickcount == 10) { // Okay enough button clicks, shut down System.Console.WriteLine("ButtonOnClick: Close()."); Close(); } // closes window terminates program if (bstate) b.text = "press me again " + clickcount.tostring(); else b.text = blabel; b.backcolor = RandBrightColor(); bstate =!bstate; System.Console.WriteLine("ButtonOnClick: leave," + "bstate = " + bstate.tostring()); }
Intro C# 11 To compile using the command line.net Foundantion SDK csc Hello432.cs // multiple *.cs files csc *.cs You may have to set the search path on your system. To run in the CS Dept. labs you may have to go to the Debug or Release directory and double click on the exe file. We may not have "debugger" system rights in these labs...
Hypertext SDK documentation Intro C# 12
Some C# differences from C++ and Java Intro C# 13 "using" for namespaces instead of "import" for packages namespaces are compiled classes stored in DLLs (base classes) "Main" is uppercase "bool" instead of "boolean" Console.WriteLine("...") not System.out.println("...") can have argument substitution and formatting like C printf Console.WriteLine("iArray[{0}] == {1}", i, iarray[i]); The arguments are substituted by index {0, 1} thus an index can be reusued in the format specification string. using System; class WriteLine { public static void Main() { int i = 20; System.Console.WriteLine("show arg 3 times:\n" + "\t first = {0} \t second = {0} \t third = {0}", i);} } show arg 3 times: first = 20 second = 20 third = 20
C# overview C# language incorporates, builds upon, and extends concepts in C++ and Java. Intro C# 14 Most types, arithmetic operators, and structured statements are the same. char, short, int, long, double + - * / % Implicitly casts from a simpler type to a more complex (no loss of info). Must explicitly cast from complex to simpler type ( possible loss). float f; int i, j = 20;... f = j; i = (int) f; Value types (built in, primitive) stored on run time stack (local, args) Reference types (objects) are allocated (new) on the heap. C# has garbage collection.
Constants, enums Intro C# 15 const type identifier = value; const int dimensions = 3; Once declared and initialized value cannot be changed. Enumerations: typed named constants enum identifier [:base-type] { enumeration list }; enum grades { f = 0, d = 1, c = 2, b = 3, a = 4 }; base-types must be of integral types { ushort, short, int, uint, long, ulong } can't be char. Useful with GUI controls settings and return values.
Safer if && switch int count = 2; if (count = 0)...; // if (count == 0)... A valid C++ conditional statement that assigns 0 to count. Intro C# 16 C# if expression accept only boolean values and there is no automatic conversion of bool to int -- compile time error. Switch does not have automatically fall through for all cases. switch ( switch expression ) { case case1 : // valid case case2 : statement; // compile error case case3 : statement; goto case4; // valid, goto case1! case case4 : statement; break; case case5 : statement; [ default : statement; ] } Switch expression: integral type, char, enum, or string type
foreach Easy iteration through a collection Intro C# 17 foreach ( type identifier in expression) statement; using System; public isn't needed for Main class SeeArgs { static void Main(string [] args) { System isn't needed int i = 0; foreach (string token in args) Console.WriteLine("Arg {0} is {1}", i++, token); } }