Fundamentals of Structures
代码
struct Integer
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
public int Read()
{
return int.Parse(Console.ReadLine());
}
}
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
public int Read()
{
return int.Parse(Console.ReadLine());
}
}
Structure Declaration
代码
using System;
struct Integer
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
public int Read()
{
return int.Parse(Console.ReadLine());
}
}
class Program
{
static int Main()
{
var Natural = new Integer();
Console.Write("Enter a natural number: ");
// Accessing a property of the structure
Natural.Value =
// Calling a method of the structure
Natural.Read();
Console.WriteLine("The value you entered was: {0}", Natural.Value);
return 0;
}
}
Here is an example of running the program:
Enter a natural number: 248
The value you entered was: 248
Press any key to continue . . .
struct Integer
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
public int Read()
{
return int.Parse(Console.ReadLine());
}
}
class Program
{
static int Main()
{
var Natural = new Integer();
Console.Write("Enter a natural number: ");
// Accessing a property of the structure
Natural.Value =
// Calling a method of the structure
Natural.Read();
Console.WriteLine("The value you entered was: {0}", Natural.Value);
return 0;
}
}
Here is an example of running the program:
Enter a natural number: 248
The value you entered was: 248
Press any key to continue . . .
Techniques of Using Structures
A Structure as a Property
代码
using System;
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
Console.Write("Enter the length: ");
len.Value = rat.Read();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
}
public class Program
{
static int Main()
{
var Rect = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", Rect.Length.Value);
Console.WriteLine("Height: {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
Rect.Length.Value * Rect.Height.Value);
return 0;
}
}
Here is an example of running the program:
Enter the dimensions of the rectangle
Enter the length: 44.84
Enter the height: 26.75
Rectangle Characteristics
Length: 44.84
Height: 26.75
Perimeter: 143.18
Area: 1199.47
Press any key to continue . . .
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
Console.Write("Enter the length: ");
len.Value = rat.Read();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
}
public class Program
{
static int Main()
{
var Rect = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", Rect.Length.Value);
Console.WriteLine("Height: {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
Rect.Length.Value * Rect.Height.Value);
return 0;
}
}
Here is an example of running the program:
Enter the dimensions of the rectangle
Enter the length: 44.84
Enter the height: 26.75
Rectangle Characteristics
Length: 44.84
Height: 26.75
Perimeter: 143.18
Area: 1199.47
Press any key to continue . . .
Returning a Structure From a Method
代码
using System;
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
public Real GetLength()
{
Real rat = new Real();
Console.Write("Enter the length: ");
rat.Value = rat.Read();
return rat;
}
}
public class Program
{
static int Main()
{
var Rect = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", Rect.Length.Value);
Console.WriteLine("Height: {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
Rect.Length.Value * Rect.Height.Value);
return 0;
}
}
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
public Real GetLength()
{
Real rat = new Real();
Console.Write("Enter the length: ");
rat.Value = rat.Read();
return rat;
}
}
public class Program
{
static int Main()
{
var Rect = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", Rect.Length.Value);
Console.WriteLine("Height: {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
Rect.Length.Value * Rect.Height.Value);
return 0;
}
}
Here is an example of running the application:
Enter the dimensions of the rectangle
Enter the length: 36.04
Enter the height: 22.86
Rectangle Characteristics
Length: 36.04
Height: 22.86
Perimeter: 117.8
Area: 823.8744
Press any key to continue . . .
Passing a Structure as Argument
代码
using System;
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
public Real GetLength()
{
Real rat = new Real();
Console.Write("Enter the length: ");
rat.Value = rat.Read();
return rat;
}
}
public class Program
{
public static void ShowCharacteristics(Rectangle rect)
{
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", rect.Length.Value);
Console.WriteLine("Height: {0}", rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(rect.Length.Value + rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
rect.Length.Value * rect.Height.Value);
}
static int Main()
{
var r = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
ShowCharacteristics(r);
return 0;
}
}
public struct Real
{
private double val;
public double Value
{
get { return val; }
set { val = value; }
}
public double Read()
{
return double.Parse(Console.ReadLine());
}
}
public struct Rectangle
{
Real len;
Real hgt;
public Real Length
{
get { return len; }
set { len = value; }
}
public Real Height
{
get { return hgt; }
set { hgt = value; }
}
public void CreateRectangle()
{
Real rat = new Real();
Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
hgt.Value = rat.Read();
}
public Real GetLength()
{
Real rat = new Real();
Console.Write("Enter the length: ");
rat.Value = rat.Read();
return rat;
}
}
public class Program
{
public static void ShowCharacteristics(Rectangle rect)
{
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length: {0}", rect.Length.Value);
Console.WriteLine("Height: {0}", rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(rect.Length.Value + rect.Height.Value) * 2);
Console.WriteLine("Area: {0}",
rect.Length.Value * rect.Height.Value);
}
static int Main()
{
var r = new Rectangle();
Rect.CreateRectangle();
Console.WriteLine();
ShowCharacteristics(r);
return 0;
}
}
Here is an example of running the program:
Enter the dimensions of the rectangle
Enter the length: 114.55
Enter the height: 82.72
Rectangle Characteristics
Length: 114.55
Height: 82.72
Perimeter: 394.54
Area: 9475.576
Press any key to continue . . .
Reference: http://www.csharpkey.com/csharp/builtinclasses/structures.htm