nau
Uploaded by
5 SLIDES
185 VUES
50LIKES

Understanding Properties in C#: Simplifying Data Encapsulation

DESCRIPTION

This course covers the concept of properties in C# programming, focusing on how to manage access to fields using getters and setters. Discover typical patterns of defining properties, including read-write, read-only, and write-only types. Learn how properties can be used in interfaces and the benefits of automatic properties introduced in C# 3.0, which streamline the process of backing store declaration. Gain insight into its significance in WPF and declarative programming, enhancing your coding efficiency and style.

1 / 5

Télécharger la présentation

Understanding Properties in C#: Simplifying Data Encapsulation

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. Programming in C#Properties CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

  2. Properties • Typical pattern for accessing fields. private int x; public int GetX(); public void SetX(int newVal); • Elevated into the language: privateint count; publicint Count {get { return count; }set { count = value; }} • Typically there is a backing-store, but not always.

  3. Properties • Using a property is more like using a public field than calling a function: FooClass foo; int count = foo.Count; // calls get int count = foo.count; // compile error • The compiler automatically generates the routine or in-lines the code.

  4. Properties • Properties can be used in interfaces • Can have three types of a property • read-write, read-only, write-only • More important with WPF and declarative programming. // read-only property declaration // in an interface. int ID { get; };

  5. Automatic Properties • C# 3.0 added a shortcut version for the common case (or rapid prototyping) where my get and set just read and wrote to a backing store data element. • Avoids having to declare the backing store. The compiler generates it for you implicitly. publicdecimal CurrentPrice { get; set; }

More Related