Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

I wish C# wasn't so verbose

If I, like today, need one instance of the StringCollection, I can either include a

  using System.Collection.Specialized;

and then:

  StringCollection c = new StringCollection();

or skip the using statement and do it the very verbose way:

  System.Collection.Specialized.StringCollection c = new System.Collection.Specialized.StringCollection();

Why doesn't C# have a shortcut on new, so that if no class name is provided then it means: use the declared class.

Then I could simply write:

  System.Collection.Specialized.StringCollection c = new ();

And then there are those explicit typecasts. I am sick and tired of all those

  myInstance = (MyClass)myArrayList[0];

Creating a new collection class just to avoid the cast is more work and not an option. Yes I know generics will come and save the world, but generics should not be necessary in the first place.

Please don't recommend VB.NET. It depresses me to see how they destroyed the language. Ruby.Net, however, would rock!

Thomas Eyde
Thursday, October 2, 2003

Wait for Whidbey (the next release). You get templates so that you can get rid of all the typecasting needed with Object collections. Instead you create a typed Collection using a template.

Something like this:

Foo myFoo = new Foo;
Collection<Foo> foos = new Collection<Foo>;
foos.Add( myFoo );
myFoo = foos(1);

Christian Mogensen
Thursday, October 2, 2003

C# doesn't have a shortcut for typing new StringCollection() but the IDE does. As soon as you type the 'new ' the intellisense (or whatever it's called) suggests StringCollection to finish the statement.


Clutch Cargo
Thursday, October 2, 2003

If 'StringCollection' is too much for you to type, you can use the 'using' directive...

you can say...

namespace SomeNS
    using SC = System.CollectionsSpecialized.StringCollection;
    public class SomeApp
        static void Main(string[] args)
            SC sc = new SC();

Once you do that, you'll be well on your way to writing unmaintable code.

Rick Childress
Thursday, October 2, 2003

I think .NET was clearly designed with IntelliSense in mind with class names such as ArgumentOutOfRangeException.  The names that I've learned to hate the most are probably the ones like the HtmlTextWriter family:
  writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");

I think this is one clear case where using 'using' as Rick described could be useful:

  using Style = HtmlTextWriterStyle;
  using Tag = HtmlTextWriterTag;   

  writer.AddStyleAttribute(Style.FontWeight, "bold");

Annoyingly, C# doesn't seem to allow the 'using' statements to be made at function scope. 

Thursday, October 2, 2003

If you're making explicit typecasts in a language that boxes and unboxes for you as required isn't the code trying to tell you something?

Thomas David Baker
Thursday, October 2, 2003

No, it doesn't tell you anything.

If "MyClass" is really a class, as would be indicated by its name, then it's not being boxed or unboxed.

Brad Wilson (
Friday, October 3, 2003

I still think VB.NET got one thing right, and that's how new() without class name is designed. Ever tried to rename a class with constructors? It's easy to do a replace all, but it is a bump in the road. Depending on the time of day, it can hurt the flow.

Thomas Eyde
Friday, October 3, 2003

Constructors and construction in C# work exactly like the same things in C++ and Java which are the languages that C# is modelled upon. People like me who come from these languages think that this similarity is an advantage, not a problem.

There are several standard languages for the .NET Framework, and not just one, precisely because different languages have different design philosophies. Complaining that one language isn't exactly like a different language doesn't make any sense.

C# really a well-designed language, it's just not the (one?) language you're used to, and frankly that's YOUR problem. Sorry for being cranky but this stuff is getting annoying.

Chris Nahr
Saturday, October 4, 2003

Then stop reading it if it annoyes you. And I prefer C# over VB.NET. The language I *was* used to is Classic VB. These days it is C#.

Do you know what annoyes me? All the arguments about C# is built on C++/Java/you-name-it, hence it is good.

foreach is an iterator shortcut. You don't find it in C++ or Java. If you fancy. you can still call GetEnumerator() and iterate the Java way.

I'd like new() as a shortcut in addition to new WhateverClass(), not as a replacement. It happens quite often that a class changes in subtles ways, and so making the class name a bit off target. Renaming a class means I have to rename all contructors and all calls to them.

As well designed C# may be, it is not perfect. It is designed along an ideology that "if the compiler catches it" it must be a bug. Here's a common scenario:

  string aString;
  aString += "some important stuff"; // compiler stops

Because it is illegal to use an unassigned variable. But that's a lie, it is assigned, but not by me. An unassigned string is null, as you all know, so the code in it self is workable and fulfil its intentions. Still the compiler treat this as a bug.

I work in a Microsoft shop, so C# is by far our best choice. I just want it to be better, quicker (to code) and more productive.

Thomas Eyde
Saturday, October 4, 2003

"Do you know what annoyes me? All the arguments about C# is built on C++/Java/you-name-it, hence it is good."

Actually I didn't say that. All I'm saying is that it's similar to these languages in ways that I like. Now the fact that I like it doesn't make it good, but the fact that you don't like it doesn't make it bad either. Different languages, different philosophies, different preferences -- simple as that.

Chris Nahr
Saturday, October 4, 2003

"Renaming a class means I have to rename all contructors and all calls to them."

Apparently, JetBrains - - has a refactoring plugin for VS.NET. If it's anywhere as good as the refactoring support in the company's Java code editor Intellij IDEA, renaming classes should become a lot simpler.

Marc Evelyn
Monday, October 6, 2003

Don't be so freakin lazy...

God I hate Visual Basic programmers and the whole "Can't someone else do it?" mentality?

Monday, October 6, 2003

*  Recent Topics

*  Fog Creek Home