Recently in C# Category

Validating XML Via Embedded XSD Schema

January 17, 2008

Problem: You have some XML, and you need to validate it against a custom schema that you want to deploy with your .Net 2.0 assemblies. Issues addressed: opening the file and handling the schema validation.

Solution: The XmlSchema class contains a Read method that takes a stream as a parameter. Nicely matching up to that, you can open a stream from an embedded resource (thank you, attilan.com).So, embed the schema. In Visual Studio's Solution Explorer, right-click the schema file and choose properties. Change the Build Action to "Embedded Resource".

Viewing Embedded Images in HTML E-mail (Base64)

January 11, 2008

Problem: you have image data from an HTML e-mail, but don't know what to do with it. When you save it to disk it comes out with a weird string instead of binary data.

Solution: pretty simple, you just have to know the terms. This string of data is actually Base64 encoded, which means that the binary data has been converted to a text string for easy transmission over text protocols. .Net Developer's Journal has a good full explanation. All you have to do is convert the string over to a byte array using Convert.FromBase64String, then save your new byte[].

Threading and Waiting with Delegates

December 7, 2007

One of the casualties of not having a full computer science education is that I was barely exposed to threading. In my two Java classes we touched upon it, and wrote some basic examples in the second semester, but that could hardly be called extensive use. Thus, a number of years on, it has been a bit of a struggle for me to use threads in a practical manner. Further complicating the matter is that I've wanted to use anonymous methods (aka delegates) and generally want to do some work after all threads have finished executing.

Passing Objects Does Not Require 'ref'

October 2, 2007

I'm willing to admit that I made a mistake, and I share this now for your reference (no pun intended): in C#, your basic data types (int, string, short, bool) are value-types. If you want to pass them to a method and have the original value updated, instead of a copy, then you must pass them with the ref or out keywords. But not so with reference-types (hence the name!), which includes classes.

One gray area: what to with structs. That last link says: "when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed." So there you have it. Definitely pays to know the differences between C# and C++!

Do not trust the generic List!

September 28, 2007

Actually, that's a slightly misleading title. You should trust List<T>, but you should also know its limitations. Here is one of the dangers of launching into using new functionality without reading all of the documentation. I've been having trouble because a client application produces thousands of reports. These reports are supposed to be sorted in a particular manner. I've been sorting and sorting in SQL, and going not paying enough attention to test well. Results in production? Not sorted.

So naturally I started properly stepping through the application, inspecting results from SQL and in code. What do I find? That SQL is returning the proper sort order, but then the ordering is getting lost. Why is that?

Performance #7: An (unsafe) Dead End

September 21, 2007

This article is part of the series An Exercise in Performance Tuning in C#.Net.

After a month-long hiatus — too much work, too fast and furious for posting — I'm back to the last posts in the series on real-world performance tuning. The point of these postings hasn't been to glorify my ability to tune a C# data processing application, but rather to share what I've learned in attempts to do so.

Where to next? Turns out my next steps were false starts, at least insofar as tuning is concerned. Still, there were some lessons (or should be) from these dead ends.

Performance #6: Reading Directly Into the Parser

July 23, 2007

This article is part of the series An Exercise in Performance Tuning in C#.Net.

As I look at the code I now have, I wonder if the fileLines variable is an unnecessary intermediate step. Can I rewrite so that stream.ReadLine() is passed directly into the parsing? If I do so, I’ll be leaving the file open longer, but since no other application should be attempting to access the file, I’m okay with that. This means moving the open file command into MyClass.ProcessFile().

Performance #5: File Buffering

July 19, 2007

This article is part of the series An Exercise in Performance Tuning in C#.Net.

It's time to stop ignoring the 800 pound gorilla in the room: System.String. Scrolling all the way over in the Allocation Graph, it is clear that Strings take up most of the memory, and it seems logical that most of that comes from the input file.

filebuffer1.jpg

Threading Links

July 18, 2007

I don't often have cause to use threading. In fact, I've found that it sometimes gums up the works (perhaps because I don't understand it well enough?) — slows things down. But when I do want to do some threading in C#, here are a few pages I find helpful in reminding myself how to perform various functions:

Visual Studio Says: "Failed to create component"

July 17, 2007

Problem: "Failed to create component" error pops up when dragging a custom Windows Forms control from the Toolbox onto a form. Offending line: foreach (Attribute att in Assembly.GetEntryAssembly().GetCustomAttributes(true)).

Background: I have started building a custom DLL containing items for re-use between different Windows Forms projects. An obvious candidate is an About window. I created this as a custom control in the DLL project; the control contains a textbox, which I want to fill with the application name, the version, copyright information, and company name. All of this is to come from the AssemblyInfo.cs file, using System.Reflection where necessary:

About this Archive

This page is an archive of recent entries in the C# category.

.Net Framework is the previous category.

Linux is the next category.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.12