Thursday, January 27, 2011

Understanding the User-defined value types in console application:

Each .NET Framework language provides keywords that enable you to define two kinds of
User-defined value types: structures and enumerations.
When you create an instance of a value type, the memory is allocated in-place. For example, if you create a value-type object as a local variable in a method, the memory is allocated on the stack and is automatically released at the end of the enclosing scope. Conversely, if you declare a value-type object in a class, the memory is allocated as part of the memory block for the class object and is deallocated when the class object is garbage collected.

Defining Structure Types:
A structure is a user-defined value type that inherits directly from the System.ValueType type. You typically define structure types to represent numeric data types that you want to allocate and deallocate frequently such as currency values, coordinates, or dimensions. To define a structure type, use the struct (C#) keyword. A structure type can contain variables, methods, properties, and parameterized constructors. Structures can implement any number of interfaces, but they cannot explicitly inherit
From a class or be used as a base class.

The following examples show:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
class Program
struct Mystr
public int x1;
public Mystr(int x)
x1 = x;
public void show()
Console.WriteLine("Hello world");
static void Main(string[] args)
Mystr s= new Mystr();
s.x1 = 123;

Output is display bellow.

1 comment:

Sumit kumar said...

Thanks for the above Article. It has all the information i needed. I would like to know if it is possible to share a this article to my email id.

Recent Posts