One of my colleagues mentioned that one can not add a default constructor to a structure definition. I tried that out and found that to be true. I believe it was a language design decision to have left out a default constructor from structure ADT (Abstract Data Type). But it is not a consistent design i believe, classes have default constructors so should the case be with structures. Following is the code snippet that i tried
using System;
using System.Collections.Generic;
using System.Text;
namespace DefaultConstructor
{
struct MyStructure
{
int i;
public void PrintMyValue()
{
Console.WriteLine(" My value is " + i.ToString());
}
public MyStructure(int iValue)
{
i = iValue;
}
public MyStructure()
{
Console.WriteLine(" I am default Ctor ");
}
}
class Program
{
static void Main(string[] args)
{
MyStructure myStruct;
//myStruct = new MyStructure(12);
myStruct = new MyStructure();
myStruct.PrintMyValue();
Console.ReadLine();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment