Unformatted text preview:

1C# in 75 MinutesPerry KivolowitzLaunching / Start PageRecentProjectsOpen / CreateProjectsNew console application IntellisenseIntellisense Intellisense2Hello World Hello WorldBreakpoints / single stepping• F9 toggles breakpoints• F5 start debugging• F10 step over• F11 step into• Shift F5 kills debuggingWatching / rerunningBreakpoint indicatorIndicates next line to be executedHovering over variableshows its valueTypes• object ALL types derive from object–int int i = 0;–string string s = “hello world”;– char char c = ‘A’;– float float f = 3.0f; – bool bool b = true;– enums eg: DayOfWeek.Friday– user defined classes and structs– many moreObject: base class of all types• Even value types like int and float derive from Object:– int x = 1;x.ToString() Æ “1”1.ToString() Æ “1”• Object implements other methods like:– object.Equals(other)– object.GetType()– Not used in this course3Strong typing•Pro– Catch / prevent errors at compile time•Con– VerbosityStrong typingType conversion• Implicit– Obvious relationship exists– No loss of information• Explicit– Like a cast in C or C++• Type conversion– System.Convert.To_________()– Use this extensively for ADO.NET work.Strings• string s = “some string”;• string s += “ and some other string”;• s.Length – read only attribute• s.Trim() – returns string without leading / trailing white space• Many other members to the string class– Split(), SubString(), etc.Strings• Can be indexed: char c = s[2];• Usual escape sequences– \” \n \\ etc.• Precede with @ to make a literal string– @”C:\temp\foo” is the same as– “C:\\temp\\foo”• Well defined logical operators like =, >, etc.• See also StringBuilder classEquivalent of (s)printf• string System.Format()• Uses positional notation:– System.Format(“Hi {0} {1},”, fName, lName);• Formatting for specific types available– Left for the reader4Arrays, ArrayLists, Generics• Arrays: strongly typed, fixed length– int[ ] i = { 1, 2, 4 };– int[ ] i = int[ 3 ];• ArrayList: loosely typed, variable length– Recommend use of Generics instead• Generics: strongly typed, variable length– Like C++ templatesGenerics• Strongly typed collections• Variable length• Includes List<>, LinkedList<>, Queue<>, etc.• Enable by “using System.Collections.Generic;”Generics Classes• Analogs in the real world• At the heart of OOP• An encapsulation of related functions and data• A class is a blueprint for all things of that type• Instance of a class is a thing, an objectClasses Classes• Have members:– Methods – functions in other languages–Data• Variables – member data as in other languages• Attributes – functions that behave like data• Members have protection levels– Public – visible outside class– Private – hidden outside class – Protected – visible inside “derived” classes only5Constructors• Constructors are special methods– same name as class– no return type– used for initialization of a class instance– may be “overloaded”ConstructorsUses Default Constructor• Members get their default or otherwise initialized valuesConstructors Methods• Must exist in a surrounding class or struct• “Global” methods can be fudged• Return values:– void for no return valuepublic void foo() { }– Specify type to return a specific typepublic int foo(){ }Parameters• All parameters passed by value by default• To pass by reference use ref keyword(demo)• To return more than one result, use out keyword(demo)• Keywords must be present in both method definition and invocationMember variables• Typically are not “public”• Public variables– break “data encapsulation”– cause loss of “control” of the class– easier for the lazy or hurried• Use attributes for public faces to internal variables6Member attributes• “Functions” that behave like variables• Use to provide controlled access to variables• Can be used to make read-only variables• Implemented via get and set syntaxMember attributesStatic versus instance• All individuals of a class (instances) share certain traits – but have individual copies– Rexx and Fido are Dogs but have different names– Name is a trait shared by all instances of the class Dog but each instance of Dog has its own copy– This is the “default”• To have all instances share the same member, make it “static”Static versus instanceStatic versus instance• A trait that is present in all instances of a class and physically shared by all instances is called a static trait– Can be methods or variables– Must be fully named using enclosing classOther qualifiers• const– Compile time constant• readonly– May be initialized at compile time or in a constructor• Neither can be changed after its value has been initialized7Control structures• Same as other languages such as C, C++– if, then, else–while–do– conditional operator–for–switch– continue, break• foreach not found in C or C++Operators• All the usual operators are provided• The usual order of precedence holdsEnums• Strongly typed enumerations– Not interchangeable with integers as in C, C++– Intellisense makes good use of them• Makes code more readable and maintainableEnumsBack to classes• You have seen “this”– Reference members of the current instance– Cannot be used to reference static members• Use class name instead– Typically used for disambiguating a member variable from a method parameter of the same nameClasses8Overloading methods• Used to provide alternate method signatures with the same name– Which method is called depends upon parameters– Assisted by IntellisenseOverloading methodsOperator overloading• Operators are implemented as methods• Since methods can be overloaded, it follows that operators can be overloaded• Particularly useful for user defined types such as classes• (demo)Inheritance• Some “classes” in the real world are specializations of other “classes”• All dogs are mammals• All cats are mammals too• Dogs and cats share certain traits, these could be implemented in the mammal class• Dogs and cats derive from mammals• They “inherit” common traits and “specialize” from thereInheritance• Creates specializations of a class• The “is a” relationship (versus “has a”)– Dog “is a”


View Full Document

UW-Madison CS 640 - Lecture 10

Documents in this Course
Security

Security

21 pages

Mobile IP

Mobile IP

16 pages

Lecture 7

Lecture 7

36 pages

Multicast

Multicast

38 pages

Load more
Download Lecture 10
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?