880 likes | 896 Views
Explore the world of .NET programming with this detailed guide covering .NET framework, C# basics, classes, IDE setup, and more. Learn about creating and using classes, differences between C++ and C#, and dive into object-oriented programming (OOP). Master the Integrated Development Environment (IDE) tools to create, debug, and run your .NET applications efficiently. Whether you are a beginner or transitioning from other languages like C++ or Java, this guide provides insights into .NET application types, system-defined and user-defined types, as well as various .NET services and tools. Enhance your programming skills and unlock the potential of .NET development.
E N D
Programming .ENT A Quick GuideforISP Yingcai Xiao
(1) Local: CLI (Command Line Interface) GUI (Graphical User Interface) NUI (Natural User Interface) (2) Web: Server Side, Web Applications, Web Services 4-Tir Enterprise Applications, (3) Cloud: IaaS (Infrastructure as a Service): HW PaaS (Platform as a Service): OS SaaS (Software as a Service): SW Types of Programming
.Net Framework Class Library Common Language Runtime Web Services Distributed Applications Browser Accessible Remote Applications Local Applications (CLI, GUI, NUI) Other Applications (Cloud, Mobile, IoT) .NET Application Types OS
.Net Framework Class Library System Windows Web Data (Database) Enterprise Services XML (Data Description) String, … Forms (GUI) UI Services Connection DataSet XmlDocument Language Integrated Query, Windows Presentation Foundation, Windows Communication Foundation, …
C# A .NET Programming Language
(0) C^(+)^(2n); n = 0, 1, 2, => C, C++, C# (1) The most advanced programming language to date (3) OOP (Object-oriented Programming) (4) EDP (Event Driven Programming) (5) Reflection (6) Strongly typed (7) Platform-independent: code runs on any platform with .NET runtime (8) Directly maps to CTS (.NET Common Type System) (8) FCL (Framework Class Library) (9) Visual Studio IDE C#
C# Basics Moving from C++/Java to C#
Data Types in (CTS) System-defined Types (SDTs): Primitives (int, float, …) User-defined Types (UDTs): Classes Properties Structs Interfaces Enumerations Events Delegates Generics Templates
Classes Class: a group of code and data to be instantiated to form objects. Four categories of class members: Fields: member variables Methods: member functions Properties: fields exposed using accessor (get and set) methods Events: notifications a class is capable of firing
Example: How to define a class (user-defined data type) class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods public Rectangle () { } public Rectangle (int cx, int cy) { width = cx; height = cy; }
Example: How to define a class (user-defined data type) // Accessor Methods public void setWidth(int w) { width = w; } public int getWidth() { return width; } public void setHeight(int h) { height = h; } public int getHeight() { return height; } public int area() { int a; a = height*width; return a; } // End of Rectangle class
Example: How to use a class (user-defined data type) Rectangle rect = new Rectangle(2,4); rect.setHeight(8); rect.setWidth(rect.getWidth() * 2); double darea = (double) rect.area();
Features Similar to Java/C++ Exception Interface Abstract Class Generics Enum
Features Beyond Java/C++ Property Struct Reflection Event Delegate
Math.dll An Example Assembly: Math.dll Simple.netmodule Complex.netmodule
.NET ProgrammingIDE and Tools Yingcai Xiao
(1) IDE: all-in-one programming tools (2) Includes: editor, compiler, linker, loader, debugger, profiler, project organizer, context-sensitive help, form designer, programming code libraries, application framework templates, … (3) IDE you may have used: Eclipse, jGRASP, NetBeans, BlueJ (4) Microsoft VS IDE: Microsoft Visual Studio .NET 20xx Start->All Programs->Microsoft Visual Studio 20xx-> Microsoft Visual Studio 20xx Documentation (5) Class specific settings on CSNET http://www.cs.uakron.edu/~xiao/lab-use.html Integrated Development Environment (IDE)
(1) Create a project Start->All Programs->Microsoft Visual Studio 20xx-> Microsoft Visual Studio 20xx Start Page -> New Projects Visual C#->Windows->Console Application Name: Hello Solution name: Hello Location: Browse Folders->Computer->C->WP (New Folder) -> Select Folder (Default on winserv1: C:\users\xiaotest\documents\visual studio 20xx\Projects) OK (2) Code System.Console.WriteLine ("Hello, world!"); System.Console.WriteLine ("Hit any key to end."); System.Console.Read(); (3) Run F5 (Debug->Start Debugging) C:\WP\Hello\bin\Debug\bin Create your first C# program using Visual Studio .NET
(1) Project setting: Set project to “Debug” (default setting) DEBUG->Start Debugging (2) Break points left-click on the left frame of the source window to set a break point. (3) Watch right-click on a variable select Add to Watch (4) Windows Watch window Output window for debugging Console window for the running program Debugging
Create the same program using Notepad (1) Edit (create the source code using Notepad) Start->All Programs->Microsoft Visual Studio 20xx->Visual Studio Tools->Microsoft Visual Studio Command Prompt (20xx) C: mkdir WP cd WP mkdir Hello2 cd Hello2 notepad Hello.cs & (background process) Copy code from the next page. (2) Compile (using the C-Sharp Compiler) (csc Helcsc /target:exe /out:Hello.exe Hello.cs lo.cs) (3)Excute Hello.exe
Create the same program using Notepad namespace Xiao { class MyApp { static void Main () { System.Console.WriteLine ("Hello, world"); System.Console.WriteLine ("Hit any key to end."); System.Console.Read(); // Don’t call “exit”. Why? } } }
.NET Programming Examples http://www.cs.uakron.edu/~xiao/isp/NET-Examples.zip http://winserv1.cs.uakron.edu/xiaotest/congo/Congo.aspx
The .NET Framework Class Library (FCL) Yingcai Xiao
FCL FCL provides the API that managed applications write to. 100 hierarchically organized namespaces and more than 7,000 types. • File and Stream I/O • Collections • Regex • Reflection • Data Access • Internet Classes
Collections (System.Collections) Classes that serve as containers for groups, or collections, of data. Collections
Regex: regular expressions, a language for parsing and manipulating text. Regex supports three basic types of operations: Splitting, Searching, Replacing. Regex regex = new Regex (@"\\"); // use “\” as a delimiter. string[] parts = regex.Split (@"c:\inetpub\wwwroot\wintellect"); foreach (string part in parts) Console.WriteLine (part); c: inetpub wwwroot wintellect (@: no need of \\ for the expression to be parsed. \ is an escape character, e.g., \n. \\ means to treat \ as a regular character.) http://en.wikipedia.org/wiki/Regular_expression Regular Expressions
Reflection: runtime understanding of the code (data types, program organizations, …) • Reflection APIs: programming tools in System.Reflection namespace to read metadata. • ILDASM: a program in .NET SDK to view the metadata using the reflection APIs • DIA SDK: Debug Interface Access SDK The Microsoft Debug Interface Access Software Development Kit (DIA SDK) providesaccess to debug information stored in program database (.pdb) files (http://msdn.microsoft.com/library/en-us/diasdk/html/vsoriDebugInterfaceAccessSDK.asp) Reflection
Contents of a managed module: • CIL instructions generated from the source code • Metadata describing code inside the module • A CLR header containing important information about the module • A Windows Portable Executable (PE) file header • Metadata: a collection of tables that describe the code. • TypeDef • Class Names • Assembly: is a collection of one or more files (modules) grouped together to form a logical unit. Manifest: metadata for an assembly. Name, version, data types exported. • AL (Assembly Linker): joins files into assemblies. Module/Metadata-Assembly/Manifest
Windows FormsGUI/EDP Yingcai Xiao
GUI Items are defined in System.Windows.Forms. System.Windows.Forms.Form class: all forms derive from it. Properties (can be treated as “public” data members): ClientRectangle (drawing area not including the borders) ClientSize BorderStyle Text (Title Bar) Methods: OnPaint (event handler for the PAINT event) .NET GUI Classes (Event Generators)
System.Windows.Forms Control Classes Controls (Windows GUI Items)
System.Windows.Forms Control Classes Controls (Windows GUI Items) Cont.
System.Windows.Forms Control Classes Controls (Windows GUI Items) Cont.
System.Windows.Forms Control Classes Controls (Windows GUI Items) Cont.
Start Visual Studio • New project • Windows Forms Application • Toolbox->All Windows Forms->Common Controls->Button drag it to Form1 • Double click on button1 in Form1 • Add to button1_Click Text = "Hello!"; Build and run Programming a GUI APP using VS
Use Notepad to write the program • Create your form (GUI) by sub-classing System.Windows.Forms.Form. • Add controls (GUI items) to the form. • Code your program logic. • Compile the program using csc. Example: Examples\DialogDemo\DialogDemo.cs Programming a GUI APP using Notepad
1. Instantiate the corresponding control class. 2. Initialize the control by setting its property values. • Add the control to the form by calling the “Add” method of the form’s Controls collection. 4. Map event handlers to the events. 5. Implement the event handlers. Programming a Control
//Create and initialize the button Button MyButton = new Button (); MyButton.Location = new Point (16, 16); MyButton.Size = new Size (96, 24); MyButton.Text = "Click Me"; // add the button to the form’s Controls collection. Controls.Add (MyButton); // Add event handlers to events MyButton.Click += new EventHandler (OnButtonClicked); // Write the event handlers void OnButtonClicked (Object sender, EventArgs e){… } NET-Examples\\DialogDemo\DialogDemo.cs Adding a Button to a Form
Common Dialog Classes Defined in System.Windows.Forms Dialog Boxes
class MyDialog : Form { Label WidthLabel; TextBox WidthBox; Button OKButton; public int UserWidth { get { return Convert.ToInt32 (WidthBox.Text); } set { WidthBox.Text = value.ToString (); } } public MyDialog () { // Initialize the dialog's visual properties ClientSize = new Size (296, 196); StartPosition = FormStartPosition.CenterParent; FormBorderStyle = FormBorderStyle.FixedDialog; Text = "Edit Ellipse"; ShowInTaskbar = false; DialogDemo.cs
// Create the dialog's controls WidthLabel = new Label (); WidthLabel.Location = new Point (16, 16); WidthLabel.Size = new Size (48, 24); WidthLabel.Text = "Width"; WidthBox = new TextBox (); WidthBox.Location = new Point (64, 12); WidthBox.Size = new Size (96, 24); WidthBox.TabIndex = 1; DialogDemo.cs
OKButton = new Button (); OKButton.Location = new Point (184, 12); OKButton.Size = new Size (96, 24); OKButton.TabIndex = 3; OKButton.Text = "OK"; OKButton.DialogResult = DialogResult.OK; AcceptButton = OKButton; // Add the controls to the dialog Controls.Add (WidthLabel); Controls.Add (WidthBox); Controls.Add (OKButton); } } DialogDemo.cs
// In the parent form who starts the dialog: MyDialog dlg = new MyDialog (); if (dlg.ShowDialog (this) == DialogResult.OK) { MyWidth = dlg.UserWidth; // get the input from the form Invalidate (); // update the display of the parent form } DialogDemo.cs
Programming the WebServer-side ProgrammingASP .NET Web Forms Yingcai Xiao
Application Deployment C:\Inetpub\wwwroot\xiaotest/calc.aspx Access http://localhost/xiaotest/calc.html http://winserv1.cs.uakron.edu/xiaotest/calc.aspx ASP.NET Application Deployment and Access
Calc.aspx <html> <body> <form RunAt="server"> <asp:TextBox ID="op1" RunAt="server" /> + <asp:TextBox ID="op2" RunAt="server" /> <asp:Button Text=" = " OnClick="OnAdd" RunAt="server" /> <asp:Label ID="Sum" RunAt="server" /> </form> </body> </html> <script language="C#" RunAt="server"> void OnAdd (Object sender, EventArgs e) { int a = Convert.ToInt32 (op1.Text); int b = Convert.ToInt32 (op2.Text); Sum.Text = (a + b).ToString (); } </script>
4-Tier Database Application