SlideServe
Menu
Préférences
© SlideServe · v2026

Simple Classes in C#

ranit avatar
14 diapositives · 315 Views · Dec 05, 2012

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.

Simple Classes in C# - slide 1
01 / 14Simple Classes in C#
1 / 17

Télécharger la présentation

Simple Classes in C#
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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;" ?