CSC439: Visual Programming: 9 Lecture (Constructor, Properties and Indexers)
This document summarizes a lecture on constructors, properties, and indexers in C# and .NET. It includes:
- Properties look like fields but contain logic through get and set accessors. A property example accesses a private backing field.
- Indexers provide natural syntax for accessing elements in a class like a list or dictionary. An indexer example accesses elements in a string.
- Constructors initialize an object on creation and have the same name as the class. A constructor example initializes a field.
- An exercise asks students to write a Student class demonstrating properties, indexers, and a constructor.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
40 views9 pages
CSC439: Visual Programming: 9 Lecture (Constructor, Properties and Indexers)
This document summarizes a lecture on constructors, properties, and indexers in C# and .NET. It includes:
- Properties look like fields but contain logic through get and set accessors. A property example accesses a private backing field.
- Indexers provide natural syntax for accessing elements in a class like a list or dictionary. An indexer example accesses elements in a string.
- Constructors initialize an object on creation and have the same name as the class. A constructor example initializes a field.
- An exercise asks students to write a Student class demonstrating properties, indexers, and a constructor.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
CSC439: Visual Programming
9th Lecture (Constructor, Properties and
Indexers)
Fall 2018
1 Lecture 9 - Constructor, Properties, Indexer
Properties Properties look like fields from the outside, but internally they contain logic, like methods do A property is declared like a field, but with a get/set block added. get and set denote property accessors.
Here’s how to implement CurrentPrice as a property
public class Stock { decimal currentPrice; // The private "backing" field public decimal CurrentPrice // The public property { get { return currentPrice; } set { currentPrice = value; } } }
Properties The get accessor runs when the property is read. It must return a value of the property’s type. The set accessor runs when the property is assigned. It has an implicit parameter named value of the property’s type that is typically assigned to a private field
3 Lecture 9 - Constructor, Properties, Indexer
Indexer Indexers provide a natural syntax for accessing elements in a class that encapsulate a list or dictionary of values. Indexers are similar to properties, but are accessed via an index argument rather than a property name. The string class has an indexer that lets you access each of its char values via an int index: string s = "hello"; Console.WriteLine (s[0]); // 'h' Console.WriteLine (s[3]); // 'l'
4 Lecture 9 - Constructor, Properties, Indexer
Indexer General form element-type this[int index] { // The get accessor get { // return the value specified by index } // The set accessor set { // set the value specified by index } }
5 Lecture 9 - Constructor, Properties, Indexer
Indexer Example class Customer } { private string[] address =new class Program string[] { "Uttara", “Mirpur“, { “Nikunjo” }; static void Main(string[] args) { public string this[int index] Customer a = new { Customer(); set { address[index] = a[1] = "HB"; value; } Console.WriteLine(a[1]); get { return } address[index]; } } }
6 Lecture 9 - Constructor, Properties, Indexer
Constructor A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. The general form of a constructor is shown here:
Constructor Example public class Panda { string name; // Define field public Panda (string n) // Define constructor { name = n; // Initialization code (set up field) } } ... Panda p = new Panda ("Petey"); // Call constructor
8 Lecture 9 - Constructor, Properties, Indexer
Exercise Write a program having a class Student that demonstrates the uses of properties, indexers and constructor.