KBD

Keith Devens .com

Sunday, March 21, 2010 Flag waving
XML wasn't designed to be edited by humans on a regular basis. – Guido van Rossum

Tag: C#

Parents:

Page 1 →

Daily link icon Thursday, April 23, 2009

JSON in .NET 3.5

.NET 3.5: JSON Serialization using the DataContractJsonSerializer. Very helpful article.

However, Microsoft's built-in JSON serialization doesn't even serialize the built-in Dictionary class in a useful way.

var foo = new Dictionary<string,string>();
foo.Add("one", "uno");
foo.Add("two", "dos");
var json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(foo.GetType());
var stream = new MemoryStream();
json.WriteObject(stream, foo);

Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));

gives:

[{"Key":"one","Value":"uno"},{"Key":"two","Value":"dos"}]

Idiots.

Edit: FWIW, the reason behind it is that Microsoft wrote their JSON serialization as a half-assed subclass of their XML serialization, so it thinks everything's an "infoset". Infosets don't have associative arrays, hence their representation of one as an array of key value pairs.

JSON is kind of an important technology, and Microsoft allocated one day of a programmer's time to half-ass it into the .NET class libraries.

Daily link icon Monday, April 13, 2009

  1. Ned Batchelder: C# and OutputDebugString:

    Debugging UI code is a pain because if your debugger obscures the UI, then exposing the UI causes window messages, which change the behavior of the code, or trip more breakpoints, and so on. So I'm using printf-style debugging: printed messages in key points to understand what's going on. Here's some tips on how to do it.

    Incidentally, I never realized that using dual monitors would prevent window messages from being fired upon switching back and forth. I've never been fond of dual monitors, but that's an actual practical reason to use them.

       (0) Tags: [C#]

Daily link icon Friday, November 21, 2008

  1. Dare Obasanjo aka Carnage4Life - Functional Programming in C# 3.0: How Map/Reduce/Filter can Rock your World.

       (0) Tags: [C#, Functional Programming]

Daily link icon Thursday, November 20, 2008

  1. Today is the first I've heard about C# 4.0. They're adding some dynamic features to the language. Here are a couple interviews with the team explaining what they're working on. (To watch).

       (0) Tags: [C#, To Read]

Daily link icon Thursday, October 30, 2008

  1. Why the heck did Microsoft provide an OrderedDictionary class and not also provide a generic version. It's SO NOT HARD.

       (1) Tags: [C#]

Daily link icon Tuesday, October 7, 2008

  1. Kirk Allen Evans's Blog : Getting Path from an XmlNode

       (0) Tags: [C#, XML]

Daily link icon Wednesday, August 20, 2008

More fun with extension methods

or shoehorning in what the C# library should have included in the first place.

public static void Each<T>(this IEnumerable<T> source, Action<T> a){
  foreach (var item in source)
    a(item);
}

public static void Each(this IEnumerable source, Action<object> a){
  foreach (var item in source)
    a(item);
}

See my previous extension method, Join.

Bad C# design

I should be able to do something like:

var table = new HtmlTable {
  Rows = { // <-- error is here
    from XmlNode p in node.SelectNodes("./property")
    select new HtmlTableRow{
      Cells = {
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
      }
    }
  }
};

But I can't, because the LINQ code isn't "expanded" so the compiler complains that while it expects an HtmlTableRow element between the braces assigned to Rows, you're giving it a collection. But, you can't assign the collection to Rows directly because it's a read-only property.

Bad class design (Rows being read-only... same as Cells fwiw) or bad language design in that there's no way to "expand" the results of the LINQ query to do what seems natural in that spot?

Instead, I have to do:

var rows = 
  from XmlNode p in node.SelectNodes("./property")
  select new HtmlTableRow{
    Cells = {
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
    }
  };

var table = new HtmlTable();
foreach (var r in rows)
  table.Rows.Add(r);

which is redundant.

Daily link icon Thursday, July 24, 2008

  1. LINQ to Objects - 5 Minute Overview - Hooked on LINQ. Decent tutorial. Has examples of grouping and joins.

       (0) Tags: [C#, LINQ]
  2. C# 3.0: The Evolution Of LINQ And Its Impact On The Design Of C# (via). Very interesting explanation of how LINQ came about.

       (0) Tags: [C#, LINQ]
  3. New "Orcas" Language Feature: Extension Methods - ScottGu's Blog. Very informative post.

    Edit: He also covers C# 3's query syntax (i.e. LINQ).

       (0) Tags: [C#, LINQ]

Joining together the results of a LINQ query into a string

Say you want to search a string with a regular expression and return all the matches concatenated together, using LINQ:

var str = "some string";
var matches = Regex.Matches(str, @"REGEX");

Three ways to concatenate:

Using String.Join (simplest):

var foo = String.Join("", (from Match match in result select match.Value).ToArray());

Using an accumulator:

var foo = (from Match match in matches select match.Value)
    .Aggregate(new StringBuilder(), (sb, s) => sb.Append(s)).ToString();

Using a loop:

var sb = new StringBuilder();
foreach (var s in (from Match match in result select match.Value)){
    sb.Append(s);
}
var foo = sb.ToString();

First seems most concise, and it's easier to specify a join character with. Apparently there's no way to massage an IEnumerable(T) into a String.Join, so unfortunately you need to pass it an array, which makes the IEnumerable fill out all its values into an array (so it can't be lazy), and then String.Join makes another pass over that array and copies the values into a string. So it uses double the space and double the time.

The second is more obscure, but only makes one pass over the result, though it's harder to specify a join character if desired.

The third is presumably most efficient, but it's more verbose.

In conclusion, there should be a String.Join(IEnumerable<T>).

Edit: Though it's impossible to define a "static" extension method (like, another variation of String.Join), you can define a Join method on IEnumerable like so:

static class Extentions{
    public static string Join(this IEnumerable source, string separator){
        var sb = new StringBuilder();
        bool first = true;
        foreach(var foo in source){
            if(!first)
              sb.Append(separator);

            sb.Append(foo.ToString());
            first = false;
        }
        return sb.ToString();
    }
}

The first example above now becomes:

var foo = (from Match match in result select match.Value).Join("");

or even more concisely:

var foo = Regex.Matches(str, @"REGEX").Join("");
// (apparently the ToString on a Match object returns its Value)

Very cool.

Note: Works with custom ToString()s as expected:

class Custom{
    public string Value { get; set; }
    public override string ToString(){
        return "VALUE: "+Value;
    }
}
var list = new List<Custom> {
    new Custom { Value = "one" },
    new Custom { Value = "two" }
};

Console.WriteLine(list.Join(", "));

prints "VALUE: one, VALUE: two" as expected.

Final note: I would have used FirstOrDefault in my Join extension method instead of a first boolean, but the example of the regular expression object was chosen because it's not a generic, so I can't use IEnumerable<T>, only IEnumerable.

Daily link icon Thursday, July 17, 2008

  1. Overview of C# 3.0

       (0) Tags: [C#]

Daily link icon Wednesday, February 13, 2008

Join a NameValueCollection into a querystring in C#

C# is ugly as heck:

private static string JoinNvcToQs(System.Collections.Specialized.NameValueCollection qs){
    return string.Join("&", Array.ConvertAll<string, string>(qs.AllKeys, delegate(string key){
        return string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(qs[key]));
    }));
}

In Ruby, this would be (essentially):

e = URI.escape
qs.collect{ |k,v| e(k)+"="+e(v) }.join("&")

Daily link icon Monday, July 30, 2007

  1. Kathy Kam : .NET Format String 101.

       (0) Tags: [C#]

Daily link icon Thursday, July 12, 2007

The annoying way to .clone() a list in C#

List<Type> temp = list.ConvertAll<Type>(delegate(Type a){return a;});

You'd think a generic type like List would have a useful clone or copy method, but noo. (Type is just a placeholder... insert your type here)

Daily link icon Monday, June 4, 2007

How to "correctly" format currency in C#

For some reason C#, by default, formats negative currency as "($12,345,678.90)" instead of "-$12,345...". Here's code that lets you change that (wordwrapped for her pleasure):

// set currency format
string curCulture =
    System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat =
    new System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;

number.ToString("c", currencyFormat);
// or string.Format(currencyFormat, "{0:c}", number);

C# is retarded.

Daily link icon Thursday, March 8, 2007

C# Frequently Asked Questions : Why can't I use the same variable as an inner loop does?

C# Frequently Asked Questions : Why can't I use the same variable as an inner loop does?. Language designers, please don't try to protect me from something like this. It's annoying.

But for fun, check out the possibly contradictory compile errors in code like:

public static void Main(string[] args){
   for(int y=0; y<100; y++){}
   Console.Write(y);
   int y = 0;
}

y doesn't exist but yet can't be created.

Daily link icon Thursday, February 15, 2007

  1. SteveX Compiled » String Formatting in C#. I always look for this reference when I need to use string.Format to format a number or something, but I've never blogged it.

       (0) Tags: [C#]

Daily link icon Wednesday, August 30, 2006

Json.NET - Newtonsoft

Json.NET - Newtonsoft, a C# implementation of JSON. Looks really good from their example... about to try it out.

Update: based on testing so far, it works great. Their example output is incorrect however. It uses single quotes, when only double quotes are allowed in JSON. But the code in fact does the right thing.

Note that the library will generate invalid JSON and include language-specific types in it, such as a date. And that's actually desirable, since if you're just using it as serialization for your own code you get what you want, and can be careful and not use language-specific types if you need to share with others.

Daily link icon Thursday, July 27, 2006

Dumb substring behavior in C# (and Java)

Substring in C# (and Java) throw an exception if you take a substring and give it a length (or a starting position) that puts you after the end of the string. That's basically to ensure that you always get a string that's exactly the length you want? Rather than just being able to take a substring and not having to worry about it, you have to include code like the following around every single substring you ever take:

if (str.Length > 30){
    str = str.Substring(0, 30);
}

Typically when you take a substring you want to ensure that your string is no longer than a certain number of characters. I can't think of a situation that would it a good idea for the language to enforce that you can't get a string that's less than the maximum length you want instead of exactly the length you want.

On the other hand, most of the dynamic languages (PPPR) let you substring off the end of the string without worrying about it.

Daily link icon Friday, May 19, 2006

  1. Launching a process and displaying its standard output - The Code Project - C# Programming. So, here's how to do Perl's '$str = `shellcommand`' in C#:

    public string Run(string command, string args){
        Process p = new Process(); // System.Diagnostics.Process
        p.EnableRaisingEvents = false;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = command;
        p.StartInfo.Arguments = args;
        p.Start();
        return p.StandardOutput.ReadToEnd();
    }
       (0) Tags: [C#]
  2. VSJ | .NET Zone | Scripting with C#. Dynamically compiles C#, and so on. Didn't finish reading though.

       (0) Tags: [C#]

Daily link icon Thursday, March 16, 2006

Asinine accessors, or preposterous properties

Was just reading this post by Jeremy Miller (via Keith) and some of the code in it has forced me to write this post, completely unrelated to the topic of Jeremy's post.

So often I see people writing properties that look like this:

public DateTime InvoiceDate
{
    get { return _invoiceDate; }
    set { _invoiceDate = value; }
}

public string InvoiceNumber
{
    get { return _invoiceNumber; }
    set { _invoiceNumber = value; }
}

Damnit people, C#'s has its properties so that we don't have to go and write accessors for all public properties like people do in Java. Make it a public data member and be done with it until you actually need custom behavior. Otherwise you're just needlessly writing code around what C# already does.

Update: There are actually two cases where you would want to define "unnecessary" properties such as these. Adam explains the case of Winforms Data Binding:

Properties and member fields are treated differently by System.Reflection. You can databind to an object property, but not a plan old member.

...

The sad thing is that both MemberInfo and PropertyInfo, the classes that encapsulate field and property access, both have "GetValue" and "SetValue" members, but they're not defined in the base class nor with a common interface, so you can't use these interchangeably.

The other case is if you're writing code specifically to be distributed as a dll. Even though the syntax is the same, the generated code is of course different, and changing from a member to a property would require a recompile of the client code.

Daily link icon Wednesday, March 8, 2006

  1. CodePost | New language features in C# 3.0, via Digg, via Michael. Very good article -- concisely written. And I'm so looking forward to C# 3. He didn't go into the anonymous methods (lambdas), but he did go into "extension methods", which I had no idea existed. It's like Ruby! However, almost all of the commenters (currently at 9) are morons (with the exceptions of #3 and #9) who don't grasp what's going on with the var keyword, despite the fact that the author was very clear in explaining it.

       (0) Tags: [C#, Ruby]

Daily link icon Thursday, March 2, 2006

  1. James Manning's blog : error CS1501: No overload for method 'Base' takes '0' arguments. Why don't more languages come with 'unified constructors' like Python (and even PHP, now) does? Wouldn't that fix the problem?

       (0) Tags: [C#, OOP, PHP, Python]

Daily link icon Wednesday, March 1, 2006

  1. James Robertson: The "Excitement" of LINQ (via Adam Vandenberg). (Also see my previous post about LINQ).

       (0) Tags: [C#, Smalltalk]

Daily link icon Wednesday, February 22, 2006

  1. Here's an article explaining how to dynamically compile code and inject into a running system in C#. Very funky, I wonder how well it works. Of course, there's no hope of replacing an existing assembly in memory, so you can't use this too often without restarting your app once in a while.

       (0) Tags: [C#]
  2. Smart Software: Dynamic Typing in C#. This post was from July, 2005. I can't wait for C# 3.0. Type inference and lamdas (among many other things), woo! C# 3.0 is when C# starts transitioning from being a pretty good language to being a very good language.

       (0) Tags: [C#]

Daily link icon Friday, October 14, 2005

  1. Krzysztof Cwalina : Delegate-Based APIs, (via, via, via, but linked to earlier). Old post, but still, C# is becoming more like Ruby (which is a good thing). I liked the one guy's comment -- "couldn't you have just called ConvertAll map like everyone else?" Smiley

       (0) Tags: [C#, Ruby]
Page 1 →
March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 3 posts

Recent comments XML

new⇒Spider solitaire

I to am somewhat addicted to​spending too much time on SS.  I​have been stu...

stupid_horse: Mar 20, 10:34pm

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.164s.

(Used 10 db queries)