The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

I know what the code does, but "where T: new()" is just killing me. I didn't know that you can pull that off.

public static T MarshalObject<T>(FormView f) where T:new()
{
T dto = new T();
Type type = dto.GetType();
for (int i = 0; i < f.Row.Cells[0].Controls.Count; i += 2)
{
Control label = f.Row.Cells[0].Controls[i];
if (label is LiteralControl)
{
string propName = (label as LiteralControl).Text;
propName = Regex.Replace(propName, "([\s:-]*(<br[ ]*/>smilie*)(\w+)([\s:-])+", "$3");
if (!String.IsNullOrEmpty(propName))
{
PropertyInfo prop = GetProperty(propName, type);
if (prop != null)
{
Control valueCell = f.Row.Cells[0].Controls[i + 1];
object value = (valueCell is TextBox)
? (valueCell as TextBox).Text
: null;
prop.SetValue(dto, value, null);
}
}
}
}
return dto;
}
Thanks, Jon. Yes, it's right there. There is too much to catch up, too little time.

C# 3.0, asp.net 3.5, ajax, asp.net MVC framework, Entity Framework, the list goes on and on...
Thanks!

I should have got that by googling for "T: new()"
I remembered trying to read Quantum Chemistry and tried very hard to understand what it says without much success. I don't believe that this topic is beyond comprehension, but the learning curve is just too steep for me and trying to make the slope less steep requires years of preparation. So I just gave up on Quantum Chemistry feeling defeated. Later on, I gave up on Chemistry all together and never looked back.

Thinking back on this, my exasperation with Chemistry mostly stems from lack of good books that makes it easy to understand and interesting.

C# in depth greatly elevates my interest in moving from C# 1.0 through C# 2.0 and onto C# 3.0 and making the transition by just reading one book! I actually read "Linq in Action" and can follow along with what the book says. But it didn't really pique my interest in really getting my hands dirty on C# 3.0 features.

Another thing I found extremely useful is Snippy and Linqpad. Without those tools, learning C# 3.0 is much harder. Now, I can't go back without using Lambda expression.
Thanks Jon for the response.

I liked your book a lot because of the way it's written and a lot of effort put into making the transition from C#1 to C#3 a must instead of a choice. I am from ASP.NET background and usually don't get deep into C# specifics. I forced myself to read CLR via C#. it's a great book but C# in depth is a step further in making people want to use C# 3 features and follow the revolution of functional programming introduced by C# 3.


I think the greatest success of your book is that you motivated a lot of people to start the C# 3 journey with great enthusiasm.
The transition from introducing IEnumerator to IEnumerable is too jumpy without much explanation:

the bottom of page 165 of

public IEnumerator GetEnumerator()
{
for (int index=0; index < values.Length; index++)
{
yield return values[(index+startingPoint)%values.Length];
}
}

to page 167
static readonly string Padding = new string(' ', 30);
static IEnumerable<int> GetEnumerable()

It didn't tell me why you go from using yield return from inside IEnumerator to inside IEnumerable. What's the connection?

I googled and found the explanation from Chris Nahr (http://forums.msdn.microsoft.com/en-US/csharplanguage/thread/4dbe17cb-9da3-4f57-8a10-01e04f8a239b/)

The hidden state machine that the compiler builds when it sees a yield statement is always an implementation of IEnumerator, and it's exactly the same code whether you specify IEnumerator or IEnumerable.

When you specify IEnumerable you get the same state machine with the same IEnumerator type, but wrapped in an outer IEnumerable type with an extra GetEnumerator method. Since the IEnumerator implementation is the simpler and more fundamental case it makes sense that it would be exposed alongside the IEnumerable case.

When would you want to use IEnumerator with yield? Quite simply, whenever you want to design your own outer class that implements IEnumerable. In the generic case you'd implement "public IEnumerator<T> GetEnumerator()" within that class-- this fulfils IEnumerable<T> and makes foreach work -- and the separate explicit non-generic overload for IEnumerable without <T>:

public class MyFunnyCollectionType: IEnumerable<T> {

...lots of usual methods & properties here...

public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < 10; i++)
yield return i;
}

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}

I wish Jon hadn't been so hasty in treating this difficult topic.