Blog Stats
  • Posts - 104
  • Articles - 0
  • Comments - 160
  • Trackbacks - 383

 

C # Language Design :: Default Constrcutor for Structure

`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();

}

}

}


Hammad Rajjoub,
MVP (Windows Server System - XML Web Services),
User Group Leader - Dot Net Wizards (http://dotnetwizards.blogspot.com),
Chariman UG Relations Committee (
http://inetapakistan.org),
Member Speakers Bureau (
http://mea.ineta.org)

Feedback

No comments posted yet.


Post a comment





 

 

 

Copyright © INETA Pakistan