Array Of User Defined Type In C Sharp
using System;
class Rectangle
{
int width,height;
static void Main()
{
//-------------First Way To Assign Value To Data Member-----------
Rectangle rect1 = new Rectangle();
rect1.width =11;
rect1.height = 22;
Rectangle rect2 = new Rectangle();
rect2.width = 33;
rect2.height = 44;
Console.WriteLine(rect1.width + " " + rect1.height);
Console.WriteLine(rect2.width + " " + rect2.height);
Console.WriteLine("-------------");
//-------------Second Way To Assign Value To Data Member-----------
Rectangle[] rect3 = new Rectangle[2];
rect3[0] = new Rectangle{width=11, height=22};
rect3[1] = new Rectangle{width=33, height=44};
foreach(Rectangle rec in rect3)
Console.WriteLine(rec.width + " " + rec.height);
Console.WriteLine("-------------");
//-------------Third Way To Assign Value To Data Member-----------
Rectangle[] rect4 = {
new Rectangle{width=11, height=22},
new Rectangle{width=33, height=44}
};
foreach(Rectangle rec in rect4)
Console.WriteLine(rec.width + " " + rec.height);
}
}
-----------------------------------------------------------------------------------------------------------
Array Of User Defined Type In C Sharp
Reviewed by Baljeet Singh
on
23:13
Rating:
No comments: