Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes (Part 1) Lecture 3

Similar presentations


Presentation on theme: "Classes (Part 1) Lecture 3"— Presentation transcript:

1 Classes (Part 1) Lecture 3
M.T. Ipalakova

2 Object-Oriented Programming (OOP)
Object-oriented programming helps you think about the problem you want to solve and gives you a way to represent, or model, that problem in your code The main principles of OOP are encapsulation, inheritance and polymorphism A class is the fundamental concept of OOP, which defines both representation and behavior in a single unit Classes are the primary mechanism you use to create user-defined types

3 Classes Classes in C# are reference types that implicitly derive from object To define a class, use the class keyword The body of the class enclosed in the opening and closing braces The body contains the definition of the data and behavior for the class The main class members are fields, properties and methods class Person { }

4 Accessibility Accessibility enables you to control the visibility, or accessibility, of an entity outside of its containing scope C# provides this through access modifiers Namespaces are not allowed to have any access modifiers and are always public Classes default to internal accessibility but are allowed to have either public or internal declared accessibility Class members default to private accessibility but can have any of the four kinds of declared accessibility

5 Access Modifiers public – access is not limited
protected – access is limited to the containing class or classes derived from the containing class internal – access is limited to the containing assembly private – access is limited to the containing class only

6 Fields Fields are variables that represent data associated with a class Must be defined in the outermost scope of a class Can be either an instance field or a static field Any access modifier can be specified, but typically, fields are private, which is the default If a field is not given an initial value when it is declared, it is assigned the default value appropriate for its type

7 Fields class Person { string firstName; string lastName; int yearOfBirth; bool gender; }

8 Properties A property provides a simple way to access a private field, called the backing field, which can be publicly available while still allowing the internal details of that field to be hidden Can also be static Do not require storage in memory Declared with accessors that enable you to control whether a value can be read or written and what should occur when doing so Get accessor enables the property value to be read Set accessor enables the value to be written

9 Properties class Person { private string firstName; // field public string FirstName // property get return this.firstName; } set this.firstName = value;

10 Properties The name of a property must be the same as the name of backing field, but starting with the upper-case letter The value keyword always means “the value that was provided by the caller” and is always typed to be the same as the property type None of the accessors is obligatory, however, at least one must be present Read-only property has only get accessor Write-only property has only set accessor (not reccomendable)

11 Calculated Properties
You can create calculated properties that are read-only and do not have a backing field They are excellent ways to provide data derived from other information class Contact { private string firstName; private string lastName; public string FullName get return this.firstName + “ “ + this.lastName; }

12 Automatic Properties The simplest way to declare a property
When using this syntax, you omit the backing field declaration and must always include both the get and set accessor without a declared implementation, which the compiler provides class Person { public string FirstName get; set; } This code will be transformed by the compiler to the code on the next slide

13 Automatic Properties class Person { private string firstName; public string FirstName get return this.firstName; } set this.firstName = value;

14 Encapsulation Encapsulation, as one of the principles of OOP, enables a class to hide the internal implementation details and itself from unwanted changes that would result in an invalid or inconsistent internal state Encapsulation is also sometimes referred to as data hiding Encapsulation is realized with help of properties

15 Methods If fields and properties define and implement data, methods, which are also called functions, define and implement a behavior or action that can be performed A method declaration can specify any access modifier A method can include the static modifier Methods can accept zero or more parameters, declared by the formal parameter list, which consists of one or more comma-separated parameters Each parameter must include both its type and an identifier If a method accepts no parameters, an empty parameter list must be specified

16 Methods The method signature is made up of the method name and the formal parameters list When two or more methods have the same name in a declaration space but have different method signatures, they are overloaded public int CalcAge() { return 2012 – yearOfBirth; }

17 Constructors A constructor is a special method which creates an instance of a class A constructor has the same name of the class It cannot return a value, which is different from a method that returns void If the constructor has no parameters, it is the default constructor Constructors are usually public

18 Declaring a Constructor
public class Person { public Person() // default constructor } public Person(string firstName, string lastName) this.firstName = firstName; this.lastName = lastName;

19 Nested Classes A nested class is one that is fully enclosed, or nested, inside another class declaration Nested classes are a convenient way to allow an outer class to create and use objects without making them accessible outside of that class Are easy to overuse, making your class more difficult to work with Nested classes implicitly have at least the same access level as the containing class

20 Partial Classes Enable you to split the declaration of a class into multiple parts, typically across multiple files Are implemented in exactly the same way as normal classes but contain the keyword partial just before the class keyword When working with partial classes, all the parts must be available during compilation and have the same accessibility to form the complete class

21 Static Classes You can apply the static modifier to a class as well, which defines a static class A static class can have only a static constructor, and as a result, it is not possible to create an instance of a static class Static classes most commonly contain utility or helper methods that do not require a class instance to work Static classes can contain only static members. You must explicitly include the static modifier Any static member can be public, private, or internal

22 Object Initializers Person p1 = new Person(); p1.FirstName = “Jim”; p1.LastName = “Morrison”; Console.WriteLine(p1.ToString()); Person p2 = new Person { FirstName = “Jim”, LastName = “Morrison”, }; Console.WriteLine(p2.ToString());


Download ppt "Classes (Part 1) Lecture 3"

Similar presentations


Ads by Google