Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes In C#.

Similar presentations


Presentation on theme: "Classes In C#."— Presentation transcript:

1 Classes In C#

2 Namespaces and Classes
Classes can be contained in a Namespace, but everything else must be contained in a class. namespace MyProgram { class Mileage }

3 Definition A Class is an abstract description of an object
For example, A customer class would describe the typical attributes and actions of a customer. A customer would have a name, address, phone etc. An object is a particular instance of the class in your program, for instance the customer Robert Jones who lives at 121 Dexter bld. With the phone number

4 Elements of a class A class definition can contain Fields
Properties (or accessor, mutator methods) Methods Constructor(s)

5 Fields Fields are class level variables that represent some attribute of the class, like customerName. In good design these fields are private, which means they are only accessible within the class This is to enforce the Object Oriented principle of “Encapsulation.”

6 Encapsulation Encapsulation means that:
A class should be as self contained and independent as possible. It should protect its inner workings and variables from uncontrolled change by other classes or programs Ideally, a class should be a component that can just be plugged in where ever it is appropriate

7 Properties Properties (or accessor/mutator methods) are a way of controlling how other classes can use the classes private fields. In C# properties do not take parameters and so do not have ( ) following the property name Properties are special public methods that allow a user of the class to “get” a variables value, or to “set” a value You can use a get without a set and visa versa You can use properties to validate the user’s input or to change the output

8 Methods Methods are things the class can do
For instance, in a class that converts Miles to Kilometers the class would contain a method to do that conversion Methods can be public, which means they can be accessed from other classes or they can be private which means they can only be called from within the class that contains them

9 Method signatures Methods usually have an accessor like public or private (if none is stated in c# the default is private) They also have a return type like int. If the method will not return anything the type is void If the type is not void the method must contain a return statement The return type is followed by an method name and parenthesis () The parenthesis can contain parameters, values or objects passed to the method from the calling method

10 Method Signatures Examples
public void Display() { } private double CalculateGPA(int totalCredits, double weightedAverage)

11 Constructors Constructors are special methods that “Construct” the class when it is initialized That is, the set all the variables to their initial values and call any methods that need to run to prepare the class for use If no constructor is created, C# will create a default constructor that initializes all numeric values to 0 and all objects to null

12 Constructor Signatures
Constructors have no return type, and are almost always public Constructors always have the same name as the class itself Constructors can take parameters You can have as many constructors as you want in a class as long as each has a different signature Only one constructor will run for any particular instance of a class


Download ppt "Classes In C#."

Similar presentations


Ads by Google