“An Introduction to .NET, C# and Visual Studio 2005” An Introduction to .NET, C# and VS 2005 “An Introduction to .NET, C# and Visual Studio 2005” Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College hummel@lakeforest.edu Lake Forest College Workshop © 2006 Joe Hummel
Introductions… Joe Hummel, PhD I wear 2 hats: Chicago, USA An Introduction to .NET, C# and VS 2005 Joe Hummel, PhD Chicago, USA email: hummel@lakeforest.edu web: http://mathcs.lfc.edu/hummel blog: http://www.pluralsight.com/blogs/drjoe I wear 2 hats: Academic: Associate Prof. of CS at Lake Forest College (www.lakeforest.edu) PhD from UC-Irvine (Optimizing Compilers, 1998) Industry: consultant and trainer for Microsoft and Pluralsight (www.pluralsight.com) specializing in Microsoft Windows development and .NET training Lake Forest College Workshop © 2006 Joe Hummel
And you? Name? Institution? 1. Introduction An Introduction to .NET, C# and VS 2005 Name? Institution? Lake Forest College Workshop © 2006 Joe Hummel
Workshop Outline Topic Lecture Introduction to .NET and C# 1 An Introduction to .NET, C# and VS 2005 Topic Lecture Introduction to .NET and C# 1 Working with Visual Studio 2005 2 Working with C# 2.0 3 Intro to GUIs and the other .NET Languages 4 WinForms — Windows-based GUIs in .NET 5 Introduction to Web Applications 6 WebForms and ASP.NET 7 Web Services 8 OO and Application Design 9 Relational Databases, SQL, and Visual Studio 10 Database Programming with ADO.NET 11 Lake Forest College Workshop © 2006 Joe Hummel
Daily Schedule Sunday: lecture & lab #1 Monday: lectures & labs #2-5 6:00-8:00 Monday: lectures & labs #2-5 9am-noon 1:30-5:00 7:00-9:00 Tuesday: lectures & labs #6-9 Wednesday: lectures & labs #10-11
Resources Workshop materials are available here: 1. Introduction An Introduction to .NET, C# and VS 2005 Workshop materials are available here: http://mathcs.lfc.edu/hummel/workshops.htm PPT, demos, and lab exercises Past & future workshops available here as well Blog discussing updates, new happenings, etc: http://www.pluralsight.com/blogs/drjoe Other .NET sites: Microsoft Developer Network: http://msdn.microsoft.com/ MSDN Academic Alliance: http://msdn.microsoft.com/academic/ Microsoft’s sourceforge-like project site: http://www.codeplex.com/ Good sites for .NET code, discussions, etc: http://www.devsource.com/ http://www.gotdotnet.com/ http://www.asp.net/ MSDN proper is for professional developers MSDN Academic Alliance is specifically for academia: curriculum repository, MSDNAA software program, etc. Lake Forest College Workshop © 2006 Joe Hummel
1.1 Why use .NET Advantages How I use .NET 1. Introduction An Introduction to .NET, C# and VS 2005 Advantages How I use .NET Lake Forest College Workshop © 2006 Joe Hummel
Advantages of .NET Support for multiple languages: 1. Introduction An Introduction to .NET, C# and VS 2005 Support for multiple languages: C#, C++, VB, J# (aka Java 1.2), … same concepts, same tools — from any language Support for different types of applications: console, desktop GUI, web-based GUI, web services, mobile, … Industrial strength tools & technologies at reasonable price Visual Studio .NET is a great environment that students like Available from MSDNAA — MSDN Academic Alliance http://www.msdnaa.net allows you to legally distribute software to faculty & students Visual Studio, Windows, SQL Server, Visio, SharePoint, … Lake Forest College Workshop © 2006 Joe Hummel
.NET at Lake Forest College 1. Introduction An Introduction to .NET, C# and VS 2005 Lake Forest is a small, 4-year liberal arts college near Chicago Standard Java-based intro sequence (CS0-1-2) We use command-line and BlueJ We do *not* use .NET since Java support is too far out-of-date Upper-division courses: Operating Systems: C on Linux Programming languages: ML at command-line, SQL & VB.NET Compiler class: J# or C# in .NET Web development: LAMP (Linux, Apache, MySQL, Perl) Senior Capstone: students choose platform (most choose .NET) Lake Forest College Workshop © 2006 Joe Hummel
Demos… Here are some of the applications students develop: Fun: 1. Introduction An Introduction to .NET, C# and VS 2005 Here are some of the applications students develop: Fun: Desktop GUI SlideShow app Desktop GUI Google-based app (using Google’s web service) CS 1-2: GUI-based census data analyzer Shape inheritance hierarchy within GUI-based app AVL trees with GUI visualizer and NUnit test harness Programming Languages: Database-driven GUI apps GUI front-ends to web services (Google, Amazon, etc.) Senior capstone: GUI-based scheduling program Common theme: GUI-based applications (desktop or web) to make things more interesting… Lake Forest College Workshop © 2006 Joe Hummel
1.2 Quick Intro to .NET .NET at the command line 1. Introduction An Introduction to .NET, C# and VS 2005 .NET at the command line Lake Forest College Workshop © 2006 Joe Hummel
Command-line Development 1. Introduction An Introduction to .NET, C# and VS 2005 Available via the Microsoft .NET Framework SDK same idea as Java and the JDK SDK = Software Development Kit (100 MB download) free, complete set of command-line tools and docs http://msdn.microsoft.com/net install as a standalone product installed by default when you install Visual Studio Note: development requires a “modern” version of Windows Windows NT, 2000, 2003, XP, or Vista Lake Forest College Workshop © 2006 Joe Hummel
Example Program in C# Compute the maximum speed of a sailboat: 1. Introduction An Introduction to .NET, C# and VS 2005 Compute the maximum speed of a sailboat: /* hullspeed.cs */ public class App { public static void Main() string input; double len, speed; System.Console.WriteLine("**Hullspeed Program **"); System.Console.WriteLine(); System.Console.Write("Enter sailboat length (feet): "); input = System.Console.ReadLine(); len = System.Convert.ToDouble(input); speed = 1.34 * System.Math.Sqrt(len); System.Console.WriteLine("Hullspeed = " + speed + " knots"); } }//class Lake Forest College Workshop © 2006 Joe Hummel
Namespaces In .NET, everything is contained within a class 1. Introduction An Introduction to .NET, C# and VS 2005 In .NET, everything is contained within a class Classes nested within namespaces to help organize things a namespace is a named collection of types the System namespace denotes the outermost .NET namespace Example: System.Console.WriteLine("..."); System namespace in FxCL Namespaces are the equivalent of Java packages Console class WriteLine method Lake Forest College Workshop © 2006 Joe Hummel
FxCL .NET Framework Class Library contains 1000's of classes 1. Introduction An Introduction to .NET, C# and VS 2005 .NET Framework Class Library contains 1000's of classes Much like Java’s JCL Organized by namespaces: System System.Data System.Windows.Forms etc. Lake Forest College Workshop © 2006 Joe Hummel
Compile and Run To compile with Framework SDK, use the C# compiler 1. Introduction An Introduction to .NET, C# and VS 2005 To compile with Framework SDK, use the C# compiler open Visual Studio .NET Command Prompt so path is set csc is the command-line C# compiler use /t:exe option to build console-based EXE use /out: option to specify name of EXE c:\> csc /t:exe /out:HullspeedApp.exe hullspeed.cs Microsoft (R) Visual C# .NET Compiler version 8.00.50727.42 for Microsoft (R) .NET Framework version 2.0.50727 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:\> HullspeedApp.exe **Hullspeed Program** Enter sailboat length (feet): 36.25 Hullspeed = 8.06786836779084 knots Lake Forest College Workshop © 2006 Joe Hummel
1.3 Quick Intro to C# C# is essentially Java… 1. Introduction An Introduction to .NET, C# and VS 2005 C# is essentially Java… Lake Forest College Workshop © 2006 Joe Hummel
Example — a Student Class in C# 1. Introduction An Introduction to .NET, C# and VS 2005 /* student.cs */ public class Student { private string ID; private int Exam1, Exam2; public Student(string ID, int exam1, int exam2) // constructor this.ID = ID; this.Exam1 = exam1; this.Exam2 = exam2; } public override string ToString() double avg = (Exam1 + Exam2) / 2.0; string msg = string.Format("Student {0}: average={1}", ID, avg); return msg; }//class Lake Forest College Workshop © 2006 Joe Hummel
Example — Main Program in C# 1. Introduction An Introduction to .NET, C# and VS 2005 Input data, create Student object, and output to screen: /* main.cs */ public class App { public static void Main() Student s; string id; int exam1, exam2; System.Console.WriteLine("Please enter id and 2 exam scores:"); id = System.Console.ReadLine(); exam1 = System.Convert.ToInt32( System.Console.ReadLine() ); exam2 = System.Convert.ToInt32( System.Console.ReadLine() ); s = new Student(id, exam1, exam2); System.Console.WriteLine( s ); // calls ToString to write obj } }//class Lake Forest College Workshop © 2006 Joe Hummel
Compile and Run 1. Introduction An Introduction to .NET, C# and VS 2005 Once again, we use the C# compiler at the command line: remember to open Visual Studio .NET Command Prompt so path is set c:\> csc /t:exe /out:StudentApp.exe main.cs student.cs Microsoft (R) Visual C# .NET Compiler version 8.00.50727.42 for Microsoft (R) .NET Framework version 2.0.50727 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:\> StudentApp.exe Please enter id and 2 exam scores: 123456 87 92 Student 123456: average=89.5 Lake Forest College Workshop © 2006 Joe Hummel
1.4 Execution Model How does .NET execute? 1. Introduction An Introduction to .NET, C# and VS 2005 How does .NET execute? Lake Forest College Workshop © 2006 Joe Hummel
.NET Framework Class Library (FxCL) Common Language Runtime (CLR) Architecture 1. Introduction An Introduction to .NET, C# and VS 2005 Multi-language, virtual machine driven… C# C++ VB J# … Your Application .NET Framework Class Library (FxCL) Common Language Runtime (CLR) Operating System Hardware Lake Forest College Workshop © 2006 Joe Hummel
Execution Model .EXE is *not* a standalone program (think "Java") 1. Introduction An Introduction to .NET, C# and VS 2005 .EXE is *not* a standalone program (think "Java") When run .EXE, Windows loads CLR, which loads .EXE/FxCL CLR is a VM that JITs native code on a per-method basis… .EXE OS Process CLR JIT Compiler other FxCL components Core FxCL obj code Underlying OS and HW Lake Forest College Workshop © 2006 Joe Hummel
Viewing Compiled Code with ILDasm 1. Introduction An Introduction to .NET, C# and VS 2005 ILDasm = IL Disassembler IL = Microsoft's Intermediate Language equivalent to Java bytecodes (abstract, stack-based, verifiable) c:\> ildasm StudentApp.exe Note once again that StudentApp only contains the classes we wrote; the FxCL classes are loaded dynamically by the CLR as needed. Be sure to open Microsoft Visual Studio .NET Command Prompt to ensure path is set so ILDasm tool can be found. Want to reverse engineer .NET back into C# or VB? See Reflector: http://www.aisto.com/roeder/dotnet/ Lake Forest College Workshop © 2006 Joe Hummel
Where is .NET Installed? CLR is a normal Windows DLL 1. Introduction An Introduction to .NET, C# and VS 2005 CLR is a normal Windows DLL FxCL resides in the GAC GAC = Global Assembly Cache see the folder "C:\Windows\Assembly" Observations: some assemblies have been pre-JIT’d ("native image") assemblies are tamper proof via digital signatures GAC is version-aware — can hold different versions of component Does .NET use a CLASSPATH like Java? No! .NET searches the GAC first, then the same folder as .EXE Search can be customized with .config file Lake Forest College Workshop © 2006 Joe Hummel
Implications? Users need CLR & FxCL to run .NET applications 1. Introduction An Introduction to .NET, C# and VS 2005 Users need CLR & FxCL to run .NET applications Available via Redistributable .NET Framework (20MB download) runs on Windows 98 and above .NET applications are version-specific: app is dependent upon version of .NET it was compiled against application will *NOT* run unless that version of .NET is installed compiled with SDK 1.0 / VS .NET 2002? .NET v1.0 must be installed compiled with SDK 1.1 / VS .NET 2003? .NET v1.1 must be installed compiled with SDK 2.0 / VS .NET 2005? .NET v2.0 must be installed Much like Java, .NET execution model is about trade-offs: managed execution (more secure, memory protection, etc.) portability slower execution (10%?) How can you tell if .NET has been installed on a machine? Look for the presence of the GAC, and then the version numbers on the assemblies. Lake Forest College Workshop © 2006 Joe Hummel
.NET is Portable? Much of .NET is ECMA standardized Available on a variety of platforms: various flavors of Windows (including CE and Mobile versions) Linux / OS X: Mono project supported by Novell (very mature) http://www.mono-project.com/ Unix: dotGNU (much less mature) http://www.dotgnu.org/ FreeBSD / OS X: Rotor / SSCLI Microsoft's shared-source release of .NET for research purposes http://msdn.microsoft.com/net/sscli
1.5 What's Next? Lab exercise #1 1. Introduction An Introduction to .NET, C# and VS 2005 Lab exercise #1 Lake Forest College Workshop © 2006 Joe Hummel