Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

class returned from inside when and why

$
0
0

Please see the following coding

public class Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString()
{
return String.Format("Width: {0}, Height: {1}", Width, Height);
}
}
public class Rectangle: Shape
{
}
public interface IIndex < out T >
{
T this[int index] { get; }
int Count { get; }
}
public class RectangleCollection: IIndex<Rectangle>
{
private Rectangle[] data = new Rectangle[3]
{
new Rectangle { Height=2, Width=5}, //  please see below
new Rectangle { Height=3, Width=7},
new Rectangle { Height=4.5, Width=2.9}
};
public static RectangleCollection GetRectangles()
{
return new RectangleCollection();
}
public Rectangle this[int index]
{
get
{
if (index < 0 || index > data.Length)
throw new ArgumentOutOfRangeException("index");
return data[index];
}
}
public int Count
{
get
{
return data.Length;
}
}
}

please locate this area

public class RectangleCollection: IIndex<Rectangle>
{
//etc

public static RectangleCollection GetRectangles()
{
return new RectangleCollection();
}
// etc
}

"RectangleCollection" is a class name and we are returning this class from inside the class

RectangleCollection itself.

Could you please explain the physical scenarios (and when and why) when we need to return an instance of the class from inside the class itself.

which chapter should I read here?


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>