C# 4.0 How-To

 

Real Solutions for C# 4.0 Programmers

 Need fast, robust, efficient code solutions for Microsoft C# 4.0? This book delivers exactly what you’re looking for. You’ll find more than 200 solutions, best-practice techniques, and tested code samples for everything from classes to exceptions, networking to XML, LINQ to Silverlight. Completely up-to-date, this book fully reflects major language enhancements introduced with the new C# 4.0 and .NET 4.0. When time is of the essence, turn here first: Get answers you can trust and code you can use, right now!

 Beginning with the language essentials and moving on to solving common problems using the .NET Framework, C# 4.0 How-To addresses a wide range of general programming problems and algorithms. Along the way is clear, concise coverage of a broad spectrum of C# techniques that will help developers of all levels become more proficient with C# and the most popular .NET tools.

 Fast, Reliable, and Easy to Use!

  • Write more elegant, efficient, and reusable code
  • Take advantage of real-world tips and best-practices advice
  • Create more effective classes, interfaces, and types
  • Master powerful data handling techniques using collections, serialization, databases, and XML
  • Implement more effective user interfaces with both WPF and WinForms
  • Construct Web-based and media-rich applications with ASP.NET and Silverlight
  • Make the most of delegates, events, and anonymous methods
  • Leverage advanced C# features ranging from reflection to asynchronous programming
  • Harness the power of regular expressions
  • Interact effectively with Windows and underlying hardware
  • Master the best reusable patterns for designing complex programs

If you find a mistake in this book, please send me a note: ben<at>benwatson<dot>org.

22 thoughts on “C# 4.0 How-To

  1. jaz

    c#4howto sample MVVMDemo may have some bugs, no command is working.. 🙁
    please offer the update in your side philosophicalgeek.com, 💡 presenter model

  2. Ben Post author

    jaz, that demo works fine for me (I downloaded the source from the informit.com site). Can you explain what’s wrong?

  3. jennya

    Hello Ben Watson I bought your book “c#4howto”, I downloaded the source code from the informit.com site. I executed all examples myself for good understanding. But I have problems with the chapter 16 example VirtualListViewSort. I ran the project downloaded from the informit.com site. There were no compilation errors but when I ran the project appeared Unhandled exception message “Unhandled exception has occured in your application. If you click botton Continue, the application will ignore this error and attempt to continue. If you click botton Quit the application willclose immediately.
    Index was out of range. Must be non-negative and less than the size of the collection. Parametr name: index.” Why does it happen? And how does correct this Unhandled exception? ❓ 🙁

  4. Ben Post author

    Hi jennya, I’m sorry about the problem you’re having. Can you be more specific about the problem? Does it happen when you first startup, after items are in the list, if you change the quantity? Can you post a stack trace? I cannot replicate this error.

  5. jennya

    I am apologizing for my grammatic mistakes in English language, because English is not my native language. I tried to describe detailed a problem. It’s a pity but I can not put the screenshots from my computer for the detailed describtion of the problem, because it is impossible in this forum.
    Yes Unhandled exception occurs when I am first startuping a project. If I am clicking a button Continue, the application is ignoring this Unhandled exception and working O.K. even when I am changing the quantity of number with a step: 100.
    When I Start Debugging with a current project, the string:
    ” e.Item = _listViewItemCache[0];”
    is marked by yellow color and is occuring Unhandled exception:
    ArgumentOutOfRangeException was unhandled:
    “Index was out of range. Must be non-negative and less than the size of the collection.
    Troubleshooting tips:
    “Make sure the arguments to this method have valid values. if you are working with a collection, make sure the index is less than the size of the collection. When using the overloaded two-argument FindString or FindExactString methods with a ComboBox or ListBox, check the startIndex paramet. ❓ 🙁

  6. jennya

    Hello when I was begining to study a chapter 17 I detected that the source code which I downloaded from the informit.com site did not include the directory a project PrintDemo. When I was running chapter 17 in Visual Studio in Solution Explorer in front of a project PrintDemo apperaed the message: “A project PrintDemo is unavailable. The project file can not be found”.
    Please can you check a chapter 17 in the informit.com site and upload a chapter 17 with a project PrintDemo to informit.com site again.

  7. Ben Post author

    Hi jennya, I’ve verified there was a problem with chapter 17. Apparently, I had some issues with my version control system and some folders never got checked in. The same problem also existed in chapter 25 with the LocWPFXAML project.

    I have provided a new zip file to the publisher and I’ll post a blog entry once it’s updated. Thanks for the catch!

    Unfortunately, I cannot replicate the problem in chapter 16 with the virtual ListView, but I think you may be able to solve it by surrounding the code with:

    if (_listViewItemCache != null && _listViewItemCache.Count >0)
    {
    e.Item = _listviewItemCache[0];
    }

  8. jennya

    I changed the fragment of code how you are advising:

    else

    e.Item = _listViewItemCache[0];
    e.Item.Text = “Oops”;
    }

    for:

    if (_listViewItemCache != null && _listViewItemCache.Count > 0)
    {

    e.Item = _listViewItemCache[0];
    e.Item.Text = “Oops”;
    }

    but when I am running this project again it is occuring Unhandled exception:
    “If you click a button Continue the application will ignore this error and attempt to continue. If you click a button Quit the application will close immediately. Object reference not set to an instance of an object. When I am clicking a button Continue I get the result: the listView which include messages “Oops” instead the listView of digits increasing with step 100.
    Please can you explain detailed what kind I need to change in this fragment of code?

  9. jennya

    Hello, I have a problem with the project Drawing Demo of a chapter 17. What kind of items is DrawingPanel? Is it the item of Class, Component Class, Installer Class or User Control? My suggestion that it is a User Control because when I am opening this project downloaded from the informit.com site. I see that DrawingPanel is situated in Toolbox. DrawingPanel will appear in Toolbox if to choose item User Control, to add on it design tool Panel and to build a project. But when I am expanding the project DrawingDemo in Solution Explorer I can not to see DrawingPanel Designer. I do not know why?
    Please can you explain detailed what kind of items is DrawingPanel? And how did you add it in Drawing Demo Form?

  10. [email protected]

    Hello Ben I downloaded the source code from the informit.com site.When I was running a chapter 19 Visual Studio gave an error: “MVCBooksApp is not avaliable” Can you help me to solve this problem please. ❓ ❓ ❓ ❓

  11. Ben Post author

    jennya, DrawingPanel is a UserControl, but there is no UI in it so there is nothing to edit in the designer. If you view its code, you can see its implementation.

  12. [email protected]

    Hello Ben, thank you for advice I downloaded the silverlight 4 tools for Visual Studio and set up it but when I am running and building a project SketchPad it has the error of compilation: “Error 1 No overload for method ‘Print’ takes 0 arguments C:\Users\Administrator\Documents\Visual Studio 2010\Projects\C#\SketchPad\SketchPad\MainPage.xaml.cs 70 13 SketchPad”
    Pelase can check this project??????? 🙁 🙁 😥 😥

  13. [email protected]

    Hello Ben I have some a problem again. My version of Visual Studio 2010 has not have ASP.NET MVC 2 Web Application. Please tell me where can I download ASP.NET MVC 2 Web Application? Is it paid? Thank you previously. ??????? 🙁 🙁 🙁 🙁 🙁 😥 😥 😥 😥 😥 😥 😥

  14. Ben Post author

    The problem with Print() is that the method was changed in .Net 4 after I released the book. I’ve submitted an updated zip file to informit.com, but it hasn’t been updated yet. All you need to do is supply a string to the Print method as a title.

  15. neobat

    Hi Ben,

    I have found your book at Barnes & Noble just by looking an looking for a good C# book.
    I program a lot but want to learn C# now, I am interested on graphics, things like capturing the desktop for an idea/project I have in mind.
    I have chosen your book because it looks really clean written and has great examples, also because I like things at your site.
    Can you give me a recommendation for a C# books to learn more about graphics?

    Thanks a lot,
    neobat

  16. Ben Post author

    @neobat, unfortunately it’s been a long time since I’ve done real graphics programming, so I don’t know of any good books anymore. What kind of graphics are you interested in?

  17. neobat

    Actually, I don’t know if this is called graphic but I would like to be able to capture the current window screen no matter what program I am running, once capture I would like to put the screen as blur a possible so nobody can see except for an area (rectagle area) where the mouse pointer is located
    strange, uh?
    but this is my project now

  18. neobat

    Actually, I don’t know if this is called graphic but I would like to be able to capture the current window screen no matter what program I am running, once capture I would like to put the screen as blur a possible so nobody can see except for an area (rectagle area) where the mouse pointer is located
    strange, uh? but this is my project now 😛

Leave a Reply

Your email address will not be published. Required fields are marked *