Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
Questionestablishing a connection error for mssql 2005 Pin
Member 808991411-Dec-11 23:53
Member 808991411-Dec-11 23:53 
AnswerRe: establishing a connection error for mssql 2005 Pin
thatraja12-Dec-11 3:03
professionalthatraja12-Dec-11 3:03 
QuestionEventType : clr20r3 ,system.windows.markup.xamlparse error Pin
sgkin11-Dec-11 22:24
sgkin11-Dec-11 22:24 
AnswerRe: EventType : clr20r3 ,system.windows.markup.xamlparse error Pin
#realJSOP12-Dec-11 4:40
professional#realJSOP12-Dec-11 4:40 
QuestionSave Word document to database (using OpenXML) Pin
dennieku11-Dec-11 21:37
dennieku11-Dec-11 21:37 
AnswerRe: Save Word document to database (using OpenXML) Pin
Luc Pattyn12-Dec-11 1:09
sitebuilderLuc Pattyn12-Dec-11 1:09 
AnswerRe: Save Word document to database (using OpenXML) Pin
Eddy Vluggen12-Dec-11 9:59
professionalEddy Vluggen12-Dec-11 9:59 
Questionself-initializing static reference types within the definition of the struct ? Pin
BillWoodruff11-Dec-11 15:51
professionalBillWoodruff11-Dec-11 15:51 
... moving on from my excursion into the mysteries of Tuple, and bearing down on a thorough review of struct ...

... edit #1 ...

The reason I originally titled this post with a qualification stating I thought use of Reference Types in a struct with self-initialization were illegal was because of this statement in the MS doc, "Using Structs: C# Programming Guide:"

"When a struct contains a reference type as a member, the default constructor of the member must be invoked explicitly, otherwise the member remains unassigned and the struct cannot be used. (This results in compiler error CS0171.)"

As mentioned below, this view was modified, as I discovered that internal members of a struct declared 'static can be self-initilizing.

... end edit #1 ...


I am aware that 'struct' in .NET is primarily intended to hold value Types, but I wanted to explore the use of reference Types inside a struct in various ways. I would be very interested in knowing, in terms of allocation, what happens "under the hood" in .NET if a reference Type is included in a struct.

This "quest" led to this particular (final) experiment, where not only did I use a reference Type, but made it static. That a static Type could be self-initializing inside a struct interested me:
C#
public struct TestStruct
{
    public static ListInnerList = new List<TestStruct>();

    public int testInt;

    public string testString;
}
And, creating instances updates the static List 'InnerList' as expected:
C#
for (int i = 0; i < 10; i++)
{
    TestStruct.InnerList.Add(new TestStruct {testInt = i, testString = "string #" + i.ToString()});
}
// set a breakpoint here: examine TestStruct.InnerList: just fine

Where it becomes more interesting to this well-trodden blade of grass is: if you want a TestStruct public field modified, using the 'InnerList: got to do something like this:
TestStruct selectedStruct = TestStruct.InnerList[5];
selectedStruct.testString += " modified !";
TestStruct.InnerList[5] = selectedStruct;
As MS says: "When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy."

I assume there's no work-around possible for this, where you could directly modify an existing instance of a struct inside a List of instances of said struct ... without creating a new variable, and then re-assigning.

The closest idea I could come up with to simplify modification was something like this to be included in the definition of TestStruct:
C#
public static void modifyString(int index, string stringToAdd)
{
   // add validation, error handling for production code use
   InnerList[index] = new TestStruct {testInt = index, testString = InnerList[index].testString + stringToAdd};
}
Which would be called like this:
TestStruct.modifyString(5, " ... modified !");
I don't regard that as "pretty" code.

My guess is that if you reach the point where you need the kind of facilities created (awkwardly) in this example, you've reached the point where you should use a Class.

Aside: interesting you cannot use the "==" operator on two instances of a struct, but, after all, why should something built to be "mean and lean," and live on the Heap, implement IEqual??? or other fancy Interfaces ?

Appreciate any comments, and if this code reflects "bad practices," I certainly am eager to know, since I hear LOPEARS, League Of Programmers Extrajudicial Authority Respecting Structs, has been authorized to shoot to kill.

thanks, Bill?
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson


modified 11-Dec-11 23:18pm.

AnswerRe: self-initializing static list of instances of a struct within the definition of the struct: I though this was illegal Pin
Luc Pattyn11-Dec-11 16:14
sitebuilderLuc Pattyn11-Dec-11 16:14 
GeneralRe: self-initializing static list of instances of a struct within the definition of the struct Pin
BillWoodruff11-Dec-11 17:06
professionalBillWoodruff11-Dec-11 17:06 
AnswerRe: self-initializing static list of instances of a struct within the definition of the struct Pin
Luc Pattyn11-Dec-11 17:21
sitebuilderLuc Pattyn11-Dec-11 17:21 
AnswerRe: self-initializing static reference types within the definition of the struct ? Pin
DaveyM6911-Dec-11 22:52
professionalDaveyM6911-Dec-11 22:52 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
BillWoodruff12-Dec-11 11:41
professionalBillWoodruff12-Dec-11 11:41 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
BobJanova13-Dec-11 23:19
BobJanova13-Dec-11 23:19 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
DaveyM6914-Dec-11 1:46
professionalDaveyM6914-Dec-11 1:46 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
BobJanova14-Dec-11 3:48
BobJanova14-Dec-11 3:48 
QuestionWorking On Communication Protocols Pin
AmbiguousName11-Dec-11 6:12
AmbiguousName11-Dec-11 6:12 
AnswerRe: Working On Communication Protocols Pin
Addy Tas11-Dec-11 9:08
Addy Tas11-Dec-11 9:08 
AnswerRe: Working On Communication Protocols Pin
AmbiguousName11-Dec-11 10:01
AmbiguousName11-Dec-11 10:01 
GeneralRe: Working On Communication Protocols Pin
Matty2211-Dec-11 14:17
Matty2211-Dec-11 14:17 
GeneralRe: Working On Communication Protocols Pin
jschell12-Dec-11 8:42
jschell12-Dec-11 8:42 
GeneralRe: Working On Communication Protocols Pin
Addy Tas12-Dec-11 10:46
Addy Tas12-Dec-11 10:46 
GeneralRe: Working On Communication Protocols Pin
BobJanova13-Dec-11 23:22
BobJanova13-Dec-11 23:22 
AnswerRe: Working On Communication Protocols Pin
Eddy Vluggen11-Dec-11 12:32
professionalEddy Vluggen11-Dec-11 12:32 
JokeRe: Working On Communication Protocols Pin
Peter_in_278011-Dec-11 13:33
professionalPeter_in_278011-Dec-11 13:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.