0% found this document useful (0 votes)
18 views18 pages

Lecture #8 Properties, Indexers

Uploaded by

baheerfidaie9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

Lecture #8 Properties, Indexers

Uploaded by

baheerfidaie9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Visual Programming

… Instructor: Mr. Muhammad Adil Khan


Contents

• Properties
• Indexers

Introduction
• In the C# programming language, properties and indexers are two
key concepts.

• Properties allow developers to create variables that can be


accessed and modified like any other variable, but with the added
benefit of being able to control how they are accessed and
modified.

• Indexers provide a way to access elements of a collection by index,


just like arrays.
C# Access Modifiers
You might work with public keyword which is an access modifier, which is
used to set the access level/visibility for classes, fields, methods and
properties.

C# has the following access modifiers:

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class
that is inherited from that class.
internal The code is only accessible within its own assembly, but
not from another assembly.
Internal -Access Modifiers
Overview
Introduction

public class test static void Main(string[] args)


{ {
public double pi = 3.14; test t = new test();
t.pi = 4.66; ;
} Console.WriteLine(t.pi);
}
}

when pi is public, it can be acceded, and modified


Introduction
public class test
{ static void Main(string[] args)
private double pi = 3.14;
{
public double getPi(){
test t = new test();
t.setPi(10.6);
return pi;
} double data = t.getPi();

public void setPi(double Value) Console.WriteLine(data);


{
pi = Value;
}
}
}

Now we can access value of pi by using methods, where pi(data member) have restricted access
Properties : get and set accessor
public class test static void Main(string[] args)
{ {
private double pi = 3.14; test t = new test();

Console.WriteLine(t.piProperties);
public double piProperties
{ t.piProperties = 65.6;
get { return pi; }
set { pi = value; } Console.WriteLine(t.piProperties);
}
} }

get { return pi; }


set { pi = value; }

Note: value is a predefined word, used to access and assign value


The data type of value will be implicitly as the data type of property
Example : condition

public class test static void Main(string[] args)


{ {
private double pi = 3.14; test t = new test();
public double piProperties
{ Console.WriteLine(t.piProperties);
get { return pi; }
set { t.piProperties = 1.4;
if (value > pi) { //t.piProperties = 8.4;
pi = value;
}
double data = t.piProperties;
}
} Console.WriteLine(data);
}

}
Example
public class test
{
private double pi = 3.14;
static void Main(string[] args)
public double piProperties
{
{
test t = new test();
get {
if (pi <10)
Console.WriteLine(t.piProperties);
{return 0;
}
t.piProperties = 8.4;
return pi;
}
double data = t.piProperties;
set {
if (value > pi) {
Console.WriteLine(data);
pi = value;
}
}
}
}
Other data types
The data type of value will be implicitly changed as the data type of
property
Difference between methods and properties
Indexers
• An indexer allows us to access instances of a class using an index just like an array.
• In C#, we define an indexer just like properties using this keyword followed by [] index
notation.
For example,
public int this[int index] Here,
{
get
{ public - access modifier
return val[index]; int - return type of indexer
}
this - indicates we are defining indexer in current class
set int index - access values using integer index position
{
val[index] = value; get - ,method that returns values
} set - method that assigns values
}
Example – string

class MyClass
{
private string []data = new string[5]; MyClass mc = new MyClass();
mc[0] = "Ali";
public string this [int index] mc[1] = "Hamza";
{ mc[2] = "Javed";
get mc[3] = "Yasir";
{ mc[4] = "Zubair";
return data[index];
} for (int i = 0; i < 5; i++)
set {
{ Console.WriteLine("{0}", mc[i]);
data[index] = value; }
}
}
Example –int
class MyClass
{
MyClass mc = new MyClass();
private int[] data = new int[5];
mc[0] = 10;
mc[1] = 20;
public int this[int index]
mc[2] = 30;
{
mc[3] = 40;
get
mc[4] = 50;
{
return data[index];
for (int i = 0; i < 5; i++)
}
{
set
Console.WriteLine("{0}", mc[i]);
{
}
data[index] = value;
}
}

}
Difference

Properties Indexers

1. Properties are declared by giving a unique name. Indexers are declared without giving a name.

2. Properties are identified by the names While indexers are identified by the signatures.

3. Properties are invoked through a described name. Indexers are invoked using an index of the created object.

Properties does not needs this keyword in their


4. Indexers needs this keyword in their keyword.
creation.

A get accessor of a property does not have any A get accessor of a property contains the list of same
5.
parameters. proper parameters as indexers.

You might also like