Introduction Value Types in .Net Framework

No.of Views1334
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  09 Apr 2010 08:04:45
Tag : .NET Frameworks , General
Introduction Value Types in .Net Framework
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Value 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 types

1. In built

2. User defined(structs)

3. Enums

All the above are derived from System.Value

 

Image Loading



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 Enum

Enums 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);            
        }

    }
}

Conclusion

I hope this is help to you.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top