1 / 14

Simple Classes in C#

Simple Classes in C#. CSCI 293 September 12, 2005. Today's Objectives. Terminology Scope of Variables and Methods public, private, and more Constructors, Initializers, and Destructors Instance Variables and Class (static) Variables.

ranit
Download Presentation

Simple Classes in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Classes in C# CSCI 293 September 12, 2005

  2. Today's Objectives • Terminology • Scope of Variables and Methods public, private, and more • Constructors, Initializers, and Destructors • Instance Variables and Class (static) Variables

  3. Terminology of C# Classes • an object is a particular instance of a class • class = elephant, object = Dumbo • Classes contain • methods - functions • fields - member variables • properties - methods that looks like fields to users of the method

  4. Limiting Scope of Fields Given a class named "list" with a variable named "size" • public int size; • everyone can see size • private int size; • only list objects can see size, default is private • protected int size; • list objects and objects derived from list can see size • internal and protected internal

  5. Constructors • Just like C++ public class list { private int size; // variable public list () // constructor { size = 0; } • Constructors are optional in C# • this constructor sets size=0, which is the default anyway

  6. Initializers • Since constructors are optional… public class list { private int maxsize = 10; // set default public list () { maxsize = 10; // redundant } public list (int newmax) { maxsize = newmax; // override default }

  7. Creating an Object • Don't forget to call new() class MainProgram { static void Main() { list mylist1 = new list(); list mylist2 = new list(50); ...

  8. Passing Parameters void find_max (int size, ref int max); { search the array and set max } ... // pass 10 by value and new_max by reference find_max (10, ref new_max);

  9. "this" = this object public class list { private int size; ... public set_size (int size) { this.size = size; }

  10. Instance Variables vs Class Variables • Sometimes, you need a variable that belongs to the entire class, not one variable for each object. public class WarningBox { private string message; private static int boxcount; Each WarningBox instance has its own message, but there is only one boxcount for all WarningBox objects.

  11. Static Constructors • Operate on the class, not an individual object. • Only called once, even with multiple instances of a class. • Not allowed to access non-static variables. public class WarningBox { private string message; private static int boxcount; static WarningBox() { boxcount = 0; }

  12. Destructors • Not necessary to call free(), because garbage collection is automatic. • Example Destructor ~WarningBox() { boxcount--; }

  13. Properties Properties look like variables to the class user, but they look like methods to class programmer. private int securitylevel; // object variable ... public int SecurityLevel // property { get { //search database for level return level; } set { securitylevel = value; } } --- inside main() --- mymessage.SecurityLevel = 5;

  14. Q u i z • Are variables and methods public or private by default? • Why do I need a constructor if I can initialize fields when I declare them? • How is a "class" variable declared? • Given "private static int BoxCount" is this legal: "this.BoxCount = 0;" ?

More Related