Tag: C#Parents:
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("&")
-
Kathy Kam : .NET Format String 101.
¶ (0)
Tags: [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)
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.
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.
-
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#]
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.
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.
-
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#]
-
VSJ | .NET Zone | Scripting with C#. Dynamically compiles C#, and so on. Didn't finish reading though.
¶ (0)
Tags: [C#]
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.
-
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]
-
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]
-
James Robertson: The "Excitement" of LINQ (via Adam Vandenberg). (Also see my previous post about LINQ).
¶ (0)
Tags: [C#, Smalltalk]
-
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#]
-
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#]
-
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?" 
¶ (0)
Tags: [C#, Ruby]
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.
|
Generated in about 0.18s. (Used 10 db queries) |
getElementsByClass function
http://pitfalls.wordpress.com/2008/07/07/querying-it-jquery-way-getelements...
maxgandalf: Jul 7, 5:50am