IntroductionValue type holds data in the variable. They are stored in “Stack”. Because of this the performance is good and causes minimal overhead. Mainly there are three value types1. In built
2. User defined(structs)
3. Enums
All the above are derived from System.Value
Nullable Types
In some scenarios we might need to store the value null in the basic types. For doing that we can make the type nullable as shown below bool? isValid = null;
//or
Nullable<bool> isSelected = null;
Structures or User Defined Types
Struct as mentioned is also a value type and is stored on the stack. Struct resembles class, but have several differences. Structures are composite types. They form a meaningful data. The most common example give for structures is the Point type available in System.Drawing namespace. Each point can be represented using x and y coordinate and hence a structure can be formed as struct Point
{
public UInt32 X;
public UInt32 Y;
}
I have enhanced the Point structure as shown below using System;
using System.Text;
namespace ConsoleApplication2
{
struct Point
{
public UInt32 X;
public UInt32 Y;
public Point(uint x, uint y)
{
this.X = x;
this.Y = y;
}
public static Point operator +(Point p1, Point p2)
{
Point newPoint = new Point(p1.X + p2.X, p1.Y + p2.Y);
return newPoint;
}
public static Point operator -(Point p1, Point p2)
{
Point newPoint = new Point(p1.X + p2.X, p1.Y + p2.Y);
return newPoint;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("The current point has X = ").Append(this.X.ToString())
.Append(" and Y = ").Append(this.Y.ToString());
return sb.ToString();
}
}
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(10, 20);
Point p2 = new Point(20, 30);
p1 += p2;
Console.WriteLine(p1);
}
}
}
As you can see the structure has a parameterized constructor and can have methods. It also has overloaded operators.
Note: We can’t use default constructors in Structure. Default constructor is used by the framework to initialize the fields in structure.
Note: Always make use of a structure only if the entire data in it constitute less than 16 bytes. Enumerations or EnumEnums are grouped constants. They are introduced mainly to make the code readable.Lets extend the above application by adding an Enum Direction. Based on the direction the point will be moved. using System;
using System.Text;
namespace ConsoleApplication2
{
struct Point
{
public UInt32 X;
public UInt32 Y;
public Point(uint x, uint y)
{
this.X = x;
this.Y = y;
}
public static Point operator +(Point p1, Point p2)
{
Point newPoint = new Point(p1.X + p2.X, p1.Y + p2.Y);
return newPoint;
}
public static Point operator -(Point p1, Point p2)
{
Point newPoint = new Point(p1.X + p2.X, p1.Y + p2.Y);
return newPoint;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("The current point has X = ").Append(this.X.ToString())
.Append(" and Y = ").Append(this.Y.ToString());
return sb.ToString();
}
public void Move(Direction d)
{
switch (d)
{
case Direction.Up:
if (this.X > 0)
this.X--;
break;
case Direction.Down:
if (this.X < 1000)
this.X++;
break;
case Direction.Left:
if (this.Y > 0)
this.Y--;
break;
case Direction.Right:
if (this.Y < 1000)
this.Y++;
break;
default:
break;
}
}
}
enum Direction
{
Up,
Down,
Left,
Right
}
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(0, 0);
Console.WriteLine(p1);
p1.Move(Direction.Left);
Console.WriteLine(p1);
p1.Move(Direction.Down);
Console.WriteLine(p1);
p1.Move(Direction.Left);
Console.WriteLine(p1);
p1.Move(Direction.Right);
Console.WriteLine(p1);
}
}
}ConclusionI hope this is help to you. |