Introduction to Visual Studio and C#

Document Sample
Introduction to Visual Studio and C# Powered By Docstoc
					                                                                 Lecture overview
          Introduction to
                                                                  Visual Studio 2005
    Visual Studio and C#                                            Editing web pages
                                                                    Hello World
                                                                    “Event-driven” hello world
                        Lecture 6
                                                                    Debugging
                                                                  Introduction to C#
                                                                    How C# programs are run
                CS 638 Web Programming                              Control structures and operators
                                                                    Strings, numbers, enumerations
                                                                    Collections using generics

                                                                               CS 638 Web Programming – Estan & Kivolowitz




 Launching / Start Page                                          New console application

  Recent
  Projects

Open / Create
Projects




                   CS 638 Web Programming – Estan & Kivolowitz                 CS 638 Web Programming – Estan & Kivolowitz




 Intellisense                                                    Intellisense




                   CS 638 Web Programming – Estan & Kivolowitz                 CS 638 Web Programming – Estan & Kivolowitz
Intellisense                                                          Hello World




             CS 638 Web Programming – Estan & Kivolowitz                      CS 638 Web Programming – Estan & Kivolowitz




What types of programs will
you write?                                                            Hello World
     A naïve view                             A realistic view
 Structure of program                   Structure of program
   Read input                               Wait for events
   Perform computation                      Find appropriate method
   Produce output                           for handling the event
 The program may use                        Handle the event
 libraries written by                       Repeat
 others                                 Often you just add new
                                        events and handlers to
                                        an existing program


             CS 638 Web Programming – Estan & Kivolowitz                      CS 638 Web Programming – Estan & Kivolowitz




Breakpoints / single stepping                                         Watching / rerunning
 F9 toggles breakpoints
                                                                                      Breakpoint indicator
 F5 start debugging                                                                      Indicates next line to be executed
 F10 step over
 F11 step into
 Shift F5 kills debugging                                                                             Hovering over variable
                                                                                                      shows its value




             CS 638 Web Programming – Estan & Kivolowitz                      CS 638 Web Programming – Estan & Kivolowitz
Lecture overview                                                        How C# programs run
 Visual Studio 2005                                                      C# was developed specifically for the .NET platform
   Editing web pages                                                     Code compiled to machine-independent Microsoft
   Hello World                                                           Intermediate Language (MSIL)
   “Event-driven” hello world                                              Similar to Java bytecode
   Debugging                                                             Programs execute within an environment called
 Introduction to C#                                                      Common Language Runtime (CLR)
   How C# programs are run                                                 Similar to Java virtual machine
   Control structures and operators                                      Both MSIL and CLR designed to support code
   Strings, numbers, enumerations                                        written in various languages (e.g. Visual Basic .NET)
   Collections using generics                                              Modules written in different languages interact easily

              CS 638 Web Programming – Estan & Kivolowitz                               CS 638 Web Programming – Estan & Kivolowitz




C# has the usual control flow
statements                                                              C# has the usual operators
 if () , else, else if ()                for(;;)                         In order of precedence                        Logical AND:&
 switch(){case :break;}                  while()                           Primary: x.y f(x), a[x],                    Logical XOR: ^
                                                                           x++,x--,new,typeof                          Logical OR: |
   Break, return, or goto                do{}while()
   must come after each                                                    Unary: -,!,~,++x,--                         Conditional AND: &&
   case                                  foreach(element in                x,(T)x,&,sizeof                             Conditional OR: ||
                                         collection)                       Multiplicative: *,/,%                       Conditional: ?:
 break
                                             Repeats body of loop for      Additive:+,-                                Assignment: =,+=,-
 continue                                    each element in array or      Shift: <<,>>                                =,*=,/=,%=,&=,|=,^=,<<=,
 goto                                        collection                                                                >>=
                                                                           Relational and type
 Will discuss exception                                                    testing:<,>,<=,,>=                      Some more exotic C#
 handling later                                                            Equality:==,!=                          operators omitted
              CS 638 Web Programming – Estan & Kivolowitz                               CS 638 Web Programming – Estan & Kivolowitz




Some C# types                                                           C# uses strong typing
   object           ALL types derive from object
   int              int i = 0;
   string           string s = “hello world”;
   char             char c = ‘A’;
   double            double d = 18.5;
   float            float f = 3.0f;
   bool             bool b = true;
   enums            eg: DayOfWeek.Friday
                                                                         Pro – Catch / prevent errors at compile time
   user defined     classes and structs
                                                                         Con – Verbosity
              CS 638 Web Programming – Estan & Kivolowitz                               CS 638 Web Programming – Estan & Kivolowitz
Type conversion                                             Strings
 Implicit                                                    C# strings use 2-byte Unicode characters
   Obvious relationship exists                               string s = “some string”;
   No loss of information
                                                             string s += “ and some other string”;
 Explicit
   Like a cast in C or C++                                   s.Length – read only attribute
   May cause runtime error when types checked                s.Trim() – returns string without leading /
 Type conversion                                             trailing white space
   System.Convert.To_________()                              Many other members to the string class
   Used extensively for ADO.NET work.                          Split(), SubString(), etc.

              CS 638 Web Programming – Estan & Kivolowitz                  CS 638 Web Programming – Estan & Kivolowitz




Strings                                                     Arrays, Generics
 Can be indexed: char c = s[2];                              Arrays: strongly typed, fixed length
 Usual escape sequences                                        int[ ] i = { 1, 2, 4 };
                                                               int[ ] i = int[ 3 ];
   \” \n \\ etc.
                                                             Generics collections: strongly typed, variable
 Precede with @ to make a literal string                     length
   @”C:\temp\foo” is the same as                               Like C++ STL templates
   “C:\\temp\\foo”                                             Examples: List<T>, Stack<T>, Queue<T>,
 Well defined logical operators like =, >, etc.                Dictionary<K,T>
                                                               Enable by
 More on string manipulation later                             “using System.Collections.Generic;”

              CS 638 Web Programming – Estan & Kivolowitz                  CS 638 Web Programming – Estan & Kivolowitz




Generics                                                    Lecture overview
                                                             Visual Studio 2005
                                                               Editing web pages
                                                               Hello World
                                                               “Event-driven” hello world
                                                               Debugging
                                                             Introduction to C#
                                                               How C# programs are run
                                                               Control structures and operators
                                                               Strings, numbers, enumerations
                                                               Collections using generics

              CS 638 Web Programming – Estan & Kivolowitz                  CS 638 Web Programming – Estan & Kivolowitz

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:80
posted:12/31/2009
language:English
pages:4