DOC PREVIEW
UCSC CMPS 20 - Introduction to C# Properties, Arrays, Loops, Lists

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to C# Properties, Arrays, Loops, ListsAnnouncementsSwitch statementInformation Hiding in OO LanguagesC# PropertiesC# Property SyntaxC# Property ExampleAutomatic PropertiesC# Automatic Property ExampleArraysDeclaring an ArrayArrays of Reference TypesArrays of Reference Types (2)Initializing ArraysTwo Dimensional ArraysSorting ArraysLooping in C#Foreach StatementListCreating a ListQueue, Stack, DictionaryReadingIntroduction to C#Properties, Arrays, Loops, ListsGame Design ExperienceProfessor Jim WhiteheadJanuary 12, 2008Creative Commons Attribution 3.0creativecommons.org/licenses/by/3.0Announcements•Have created a web-based forum►forums.soe.ucsc.edu►Also link from course web page►www.soe.ucsc.edu/classes/cmps020/Winter09/•Team selection due today►Hand in at front of classroom►Reminder: you are expected to know contact information listed on this page•Quick poll – who still needs a partner?•Homework #1 (Hunt the Wumpus): due Wednesday, January 21►Start work on this now, so you can get help•Section today►3:30-4:40pm, Physical Sciences 140►Attend if you want help with installing XNA, or homework #1•Still no word on the second section…Switch statement•Alternative to if•Typically use break•Can use goto to continue to another caseswitch (expression){ case constant-expression: statement(s); jump-statement [default: statement(s);]Example:const int raining = 1;const int snowing = 0;int weather = snowing;switch (weather) { case snowing:System.Console.Writeln(“It is snowing!”); goto case raining; case raining; System.Console.Writeln(“I am wet!”); break; default: System.Console.Writeln(“Weather OK”); break;}Information Hiding in OO Languages•Classes support information hiding►Data members of a class can be declared as private•Hides implementation details of the class►Create accessor methods to access data•Typically get__() and set__(), which are public•“Getters and setters”►Other classes access data via get() and set() method•So long as the interface to get and set stay the same, the class can change how it represents its data•Information hiding permits implementations to change without affecting using classes►But, is tedious to always access data via accessors•x = foo.getX() is more tedious than x = foo.x;•Wouldn’t it be great to preserve benefits of accessors, while also retaining the syntax of direct access?C# Properties•Provide procedural access to data►Like accessors•But have syntax similar to direct variable access►foo.X instead of foo.getX();•Minor feature, but provides substantial improvement in readability and fluidity of programmingTravis thumbs his nose at private propertywww.flickr.com/photos/sillygwailo/492070136/C# Property Syntax•Get accessor returns value of same type as “return type”•Set accessors have implicit parameter named “value”►Use to set internal data representation•Properties can be public, private, protected►Public: any class can access, private: only that class, protected: that class and children •By convention, property names have initial capital (“X” to access “x”)[access-modifiers] return-type property-name{ get { … sequence of statements ending in a return (or throw) } set { … sequence of statements }}get accessorset accessorC# Property Examplepublic class GameInfo{private string name; public string Name { get { return name; } set { name = value; } }}// Test codeGameInfo g = new GameInfo();// Call set accessorg.Name = “Radiant Silvergun”;// Call get accessorSystem.Console.Write(g.Name);Automatic Properties•Very often, properties are straightforward getters and setters►Get accessor just reads value of one variable►Set accessor just writes the value of one variable•Creating properties in this situation is very mechanical•With automatic properties, the compiler creates these straightforward get and set accessors►New to C# 3.0C# Automatic Property Examplepublic class GameInfo{ public string Name {get; set;} }// Test codeGameInfo g = new GameInfo();// Call set accessorg.Name = “Radiant Silvergun”;// Call get accessorSystem.Console.Write(g.Name);This property behaves the same as the first property example, two slides ago.A private class variable, private String name, is automatically created.Arrays•Array is an indexed collection of objects►Index means you use array[i] syntax to access members►Recall that types like int, string, float, etc. are objects►Can even have arrays of arrays•Unlike C++, in C# array is an object•Arrays have many useful properties and methods►Properties:•Length: length of array•Rank: number of dimensions of array►Methods:•Sort(): sorts values in one dimensional array•BinarySearch(): searches one dimensional array•Clear(): sets range of elements to 0 or null reference•Reverse(): reverses elements in one dimensional array•… and many othersDeclaring an Array•Array numbering follows C conventions►First element is numbers[0]►Upper bound is 2, so 3rd element is numbers[2]type[] array-name;Example:int[] numbers;numbers = new int[3]; Technically, just creates a variable (numbers) that will hold a reference to an array-of-integers object.Creates an array-of-integers object, of length 3, which are initialized to 0.numbers000Arrays of Reference Types•Creating a “new” array of a reference type►Just creates a series of null references►Need to assign object instances to array elementspublic class GameInfo{public string gameName; }GameInfo[] myGArray = new GameInfo[2];myGArraynullnullArrays of Reference Types (2)public class GameInfo{public string gameName; }GameInfo[] myGArray = new GameInfo[2];GameInfo A = new GameInfo();GameInfo B = new GameInfo();myGArray[0] = A;myGArray[1] = B;myGArray[0][1]ABGameInfo’1gameName: nullGameInfo’2gameName: nullThere are only two instances of class GameInfo.There are four reference variables that point to GameInfo.A, BmyGArray[0]myGArray[1]Initializing Arrays•Both syntaxes have identical behavior•Can also initialize reference types:int[] anIntArray = new int[3] { 2, 4, 6 }ORint[] anIntArray = { 2, 4, 6 }string[] aStringArray = { “The”, “Quick”, “Brown”, “Fox” }AClass[] AClassArray = { new AClass(), new AClass(), new AClass() }Two Dimensional Arrays•Can have two (and more) dimensional


View Full Document

UCSC CMPS 20 - Introduction to C# Properties, Arrays, Loops, Lists

Documents in this Course
Load more
Download Introduction to C# Properties, Arrays, Loops, Lists
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 Introduction to C# Properties, Arrays, Loops, Lists 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 Introduction to C# Properties, Arrays, Loops, Lists 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?