KBD

Keith Devens .com

Friday, September 5, 2008 Flag waving
Nonsense on stilts! – Derived from Jeremy Bentham

Tag: C#

Parents:

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]

Daily link icon Sunday, September 18, 2005

Anders Hejlsberg on C# 3.0

Slashdot | Anders Hejlsberg on C# 3.0:

The new language enhancements include implicitly typed locals, extension methods, strongly-typed lambda expressions, anonymous types, and LINQ - a builtin SQL-like syntax for data access.

Rock on. I recently saw an article on LINQ at LtU, but the technology didn't pass my importance filter so I didn't read it. Now I find it's going to be in C#. But given my cursory look at some of the samples /. linked to, I don't immediately see how it's much different than Python's list comprehensions.

For instance, in Dan Fernandez' blog post about LINQ linked from the page with the interview with Anders Hejlsberg, he uses these examples:

 var result =
   from s in aBunchOfWords // query the string array
   where s.Length == 5     // for all words with length = 5
   select s;               // and return the string

 var result =
   from c in allCustomers
   where c.ContactTitle.Length == 5
   select c.ContactName;

I don't see how that's any different than Python's:

result = [s for s in aBunchOfWords if len(s) == 5]
result = [c.ContactName for c in allCustomers if len(c.ContactTitle) == 5]

And I think Python's syntax is far more elegant. Of course, allCustomers in the second example was actually a database table. But you can already do that easily with Python's (or any other language's) generators. Consider (from an interactive console session):

>>> def allCustomers():
...    custs = ["larry", "curly", "moe"]
...    for cust in custs:
...       yield cust
...
>>> [c for c in allCustomers() if len(c) == 5]
['larry', 'curly']

Of course, the syntax is a little different between the two examples, so a little bit of the implementation details leak into the syntax. Nothing's perfect.

Update: Slava on C# 3.0:

Take a look at the features planned for C# 3.0; they're basically adding a bunch of features from Common Lisp and ML... For years we have heard Java advocates say that higher order functions and expression trees lead to complex code that is hard to understand, despite plenty of evidence to the contrary. But now that Microsoft is adding these features, watch Sun scramble to catch up with Microsoft, yet again!

Update: Now I know what they mean by "implicitly typed locals". About 5:30 into the video Hejlsberg explains it (calling it "local variable type inference"). You can say var c = customers[i] instead of Customer c = customers[i] or Customer c = new Customer(). I've always complained that the latter two have redundancy, so I'm definitely a fan of that new feature. Plus, does this mark the first entrance of type inference (basic though it is) into a mainstream language?

Update: After watching more of the video, that query language is pretty cool. I love how it works with anything that's IEnumerable, and that the expressions given as filters are actually type-safe (with type inference) lambas, creating anonymous types as they go.

September 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930 



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

Recent comments XML

new⇒What's nice about comment spam...

http://buyhomefree.com/...

teena: Sep 5, 12:51pm

new⇒I hate ASP.NET

Agree.

You can add VS to the​list....

graffic: Sep 5, 5:15am

new⇒Girls, please don't get breast implants

Well alright I just read my above​comment and I wanted to add​this...I shou...

76.66.140.8: Sep 4, 7:31pm

Spider solitaire

I don't think the question was​necessarily if there are unbeatable​games.  ...

Jared: Sep 4, 12:44pm

Convert Pantone Colors to RGB and Hex - Color Conversion Chart

The colors on those website don't​seem to relate to the pantone data​we hav...

blah: Sep 3, 10:12am

Generated in about 0.354s.

(Used 10 db queries)

mobile phone