140 likes | 289 Vues
This guide covers foundational concepts in C# object-oriented programming, focusing on classes, variables, scope, and methods. Key topics include visibility modifiers (public, private, protected), constructors, destructors, and the distinctions between instance and static variables. The significance of properties resembling methods to users is explored, alongside practical examples illustrating object creation and parameter passing. Additionally, quizzes reinforce learning by assessing comprehension of variable visibility, constructor necessity, and class variables.
E N D
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
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
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
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
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 }
Creating an Object • Don't forget to call new() class MainProgram { static void Main() { list mylist1 = new list(); list mylist2 = new list(50); ...
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);
"this" = this object public class list { private int size; ... public set_size (int size) { this.size = size; }
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.
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; }
Destructors • Not necessary to call free(), because garbage collection is automatic. • Example Destructor ~WarningBox() { boxcount--; }
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;
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;" ?