Talk:C Sharp Programming/Data structures

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] age example

with code like

using System;
namespace IEpr1
{
	class Class1
	{
		enum Age : int { Infant = 0, Teenager = 13, Adult = 18 };
		static void Main(string[] args)
		{
			Age age = Age.Teenager;
			System.Console.WriteLine("Hello World!");
			Console.WriteLine("Hello World!");
			Console.WriteLine("You become a teenager at an age of {0}.", age.ToString());
			Console.ReadLine();
		}
	}
}

I get Hello World! twice followed by You become a teenager at an age of Teenager. I would have expected 13 instead of Teenager. Have I made an error in my program? Thanks. Sorry about messing up the syntax of the code block, but I can't figure it out -- take a look at the edit source. —The preceding unsigned comment was added by 70.182.153.38 (talkcontribs) .

That's because Age is an enumeration, so age.ToString() returns the enumeration value's name. To show the underlying integer (13), use this instead:
Console.WriteLine("You become a teenager at an age of {0}.", (int)age);
Rodasmith 19:13, 9 May 2007 (UTC)

[edit] Errors in "person" example

There are some mistakes in definition of "person" struct and it's constructor. the preceding unsigned comment is by 194.29.160.251 (talk • contribs) 2006-03-06 14:01:47 (UTC)

I have corrected the sample. Rodasmith 17:02, 7 March 2006 (UTC)
In order for the comparison dana.birthDate < DateTime.Now to work, does the birthDate (field?) need to be declared public? —The preceding unsigned comment was added by 70.182.153.38 (talkcontribs) .
Yes, you're right. In fact, all the field should be public (or internal) for that sample to compile. Rodasmith 20:05, 10 May 2007 (UTC)

[edit] Missing word

Structs are really only used for performance reasons and/or if you intend to it by value.
Intend to what? Verb is missing here!