|
This tutorial shows how properties are declared, used and implemented in C#.
TutorialThis tutorial includes two examples. The first example shows how to declare and use read/write properties. The second example demonstrates abstract properties and shows how to override these properties in subclasses. Example 1This sample shows a // person.cs
using System;
class Person
{
private string myName ="N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
// Declare an Age property of type int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public override string ToString()
{
return "Name = " + Name + ", Age = " + Age;
}
public static void Main()
{
Console.WriteLine("Simple Properties");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);
// Increment the Age property:
person.Age += 1;
Console.WriteLine("Person details - {0}", person);
}
}
OutputSimple Properties Person details - Name = N/A, Age = 0 Person details - Name = Joe, Age = 99 Person details - Name = Joe, Age = 100 Code Discussion
Example 2The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors. The example demonstrates how to override these properties in subclasses. This sample consists of three files. In the Properties Sample, these files are compiled into a single compilation but in this tutorial, each file is compiled individually and its resulting assembly referenced by the next compilation:
To compile the example, use the command line: csc abstractshape.cs shapes.cs shapetest.cs This will create the executable file shapetest.exe. File 1 - abstractshape.cs This file declares the // abstractshape.cs
// compile with: /target:library
// csc /target:library abstractshape.cs
using System;
public abstract class Shape
{
private string myId;
public Shape(string s)
{
Id = s; // calling the set accessor of the Id property
}
public string Id
{
get
{
return myId;
}
set
{
myId = value;
}
}
// Area is a read-only property - only a get accessor is needed:
public abstract double Area
{
get;
}
public override string ToString()
{
return Id + " Area = " + string.Format("{0:F2}",Area);
}
}
Code Discussion
File 2 - shapes.cs The following code shows three subclasses of // shapes.cs
// compile with: /target:library /reference:abstractshape.dll
public class Square : Shape
{
private int mySide;
public Square(int side, string id) : base(id)
{
mySide = side;
}
public override double Area
{
get
{
// Given the side, return the area of a square:
return mySide * mySide;
}
}
}
public class Circle : Shape
{
private int myRadius;
public Circle(int radius, string id) : base(id)
{
myRadius = radius;
}
public override double Area
{
get
{
// Given the radius, return the area of a circle:
return myRadius * myRadius * System.Math.PI;
}
}
}
public class Rectangle : Shape
{
private int myWidth;
private int myHeight;
public Rectangle(int width, int height, string id) : base(id)
{
myWidth = width;
myHeight = height;
}
public override double Area
{
get
{
// Given the width and height, return the area of a rectangle:
return myWidth * myHeight;
}
}
}
File 3 - shapetest.cs The following code shows a test program that creates a number of // shapetest.cs
// compile with: /reference:abstractshape.dll;shapes.dll
public class TestClass
{
public static void Main()
{
Shape[] shapes =
{
new Square(5, "Square #1"),
new Circle(3, "Circle #1"),
new Rectangle( 4, 5, "Rectangle #1")
};
System.Console.WriteLine("Shapes Collection");
foreach(Shape s in shapes)
{
System.Console.WriteLine(s);
}
}
}
OutputShapes Collection Square #1 Area = 25.00 Circle #1 Area = 28.27 Rectangle #1 Area = 20.00 See Also |
