Fans of the C# language will find much to like in Visual C# 2005. Visual Studio 2005 brings a wealth of exciting new features to Visual C# 2005, such as generics, iterators, partial classes, and anonymous methods. While generics is the most talked-about and anticipated feature, especially among C++ developers who are familiar with templates, the other new features are important additions to your Microsoft .NET development arsenal as well. Read more here.
heh, elegant and partial classes in the same sentence
FWIW, most of .NET is great, but the unsafe keyword and partial classes is a nightmare!
I think the reason for partial classes is mostly for code generators.
Unsafe is important for calling native functions from shared libraries and also to use raw pointers when absolutely needed. If you don’t like unsafe then just don’t use it.
It’s nice to see that microsoft has this nifty little ideas. Too bad python had pseudo lambdas and iterators for years. And ruby had real closures, iterators, and open classes since 1993. And SmallTalk since 1980. And prolly, lisp since 1956.
Almost as elegant as in Perl (but still not anywhere as compact):
foreach(“New York”, “Paris”, “Mårdaklev”){
print;
}
No one ever said that they were new features in general, but rather they are new features to the c# language
And all of those languages are just niche languages that will never have widespread use. Not too mention, that all of them are painfully slow compared to C#.
Not too mention, that all of them are painfully slow compared to C#.
Smalltalk should be comparable to C# in speed, and Lisp will usually be faster. Both are (usually) natively-compiled, unlike C# which has the overhead of a managed runtime. Compilers for these languages are a lot more mature and advanced as well.
> And all of those languages are just niche languages
C# is a niche language compared to them.
> think the reason for partial classes is mostly for code generators.
You should design a programming language for humans and not code generators.
> Unsafe is important for calling native functions from shared libraries and
> also to use raw pointers when absolutely needed. If you don’t like unsafe
> then just don’t use it
Give a developer a shotgun and you can be certain that he will shot himself in the foot eventually.
I really believe that Anders and the other folks at Microsoft had done a terrible job designing C#. They have taken a reasonable language (Java) and turned into a big mess. They have added a few good things (like structs) but in return done a lot of mistakes where anonymous methods and delegates are among them. In object oriented languages everything should be defined within the scope of a class/type or an equivalent concept. Anything else contradicts the whole idea oop.
I hope C# and MONO.NET platform gonna be de facto standard for development on Linux and others OS as well.
I really believe that Anders and the other folks at Microsoft had done a terrible job designing C#. They have taken a reasonable language (Java) and turned into a big mess. They have added a few good things (like structs) but in return done a lot of mistakes where anonymous methods and delegates are among them. In object oriented languages everything should be defined within the scope of a class/type or an equivalent concept. Anything else contradicts the whole idea oop.
Do you think that Anders the creator of C# is stupid enough to create a lot of mistakes that he can’t think of. Don’t forget that he had a lot of experience in Pascal and Delphi development.
“a terrible job designing C#. They have taken a reasonable language (Java) and turned into a big mess. They have added a few good things (like structs) but in return done a lot of mistakes where anonymous methods and delegates are among them. ”
While I don’t necessarily think that C# is that exciting as far as a language goes, having done more than a little functional programming I can tell you that often class-based OOP languages can be a major pain to express some very elegant and simple functional programming tricks. Inheritance and sub-typing are based on unifying data through merges in control flow: it is often natural to invert control flow in functional languages rather than trying to force disparate data types (or values) into the same hole.
A good example of what I am trying to illustrate is the following.
Suppose you have a function (or method, or whatever) that can return one of two types of things. For example, it can return either an integer or an object, depending on its input parameters. In a class-based language like Java, this method must be given a return type that somehow represents the merge of these two values (usually either an instance of Integer or Object, or a user-introduced class that distinguishes the two cases). The user of this method then will test the return value with an “instanceof” and do one of two things based on the result. In a functional world, there is no need to unify the two values and return a value: the client of such a function would simply supply an “integer” case method and an “object” case method as arguments to the function. The function would then call either the integer or the object function based on what the return value is. The concept is often called a continuation: instead of returning to the caller with a value, the rest of the computation is packaged up and passed forward.
Continuations are often mimicked by advanced OOP programmers (sometimes to new heights of complexity). It doesn’t always work well. Lots of new interfaces and very carefully laid out class hierarchies can sometimes yield a descent solution, but most of the time, I think the results are a mess.
Personally, I am beginning to think that languages that are “too” pure often get too good at expressing some things at the major expense of other things.
Just my two cents.
Use whatever you feel like it. To me, Java is just too dumbed down. Seriously, how can you say that delgates are bad. They are way more elegant than these interface/adapter classes in java. Sun basically says don’t interface with native libs that’s why we make it hard to use with the piece of crap JNI.
As far as shooting your foot off, I hope your not using C++.
In the end, Microsoft listened to what developers were saying is some of the brain damage with Java and improved upon it.
You should be happy that C# is around or Java still wouldn’t have enums, variable length args( a proper printf after how many years!), etc.. It still doesn’t have structs on the stack. The generics implementation in Java 1.5 is a compiler trick that won’t give you any more efficiency because it still has to box/unbox and forget about re-constructing types at runtime because it’s still all objects under the hood.
Java is fine on the server, but dead on the desktop. Sun let Java rot for too long and now developers have given up on it for the desktop.
I hope C# and MONO.NET platform gonna be de facto standard for development on Linux and others OS as well.
I do too. What needs to happen is for Novell lawyers to come out before Mono 1.0 release in a couple months and say “ok, ecma 335 and ecma336” are officially RAND + Royalty Free.
At that point, hopefully a bunch of gnome developers will get behind it and the push for inclusion into Gnome can begin. Of course, you also have people like Havoc Penningtons whose intense hatred of Microsoft causes him to suggest that Gnome might go with something like Python/XCOM/UNO/Gobject introspection and whatever other half-baked measures that will leave Gnome even further behind KDE.
Of course even if Gnome doesn’t include it in 4.0 or whatever, that still won’t stop developers from using it. If ECMA 335 and 336 are truly legally clear I’m pretty sure Mono will be bigger than Python or Perl eventually.
“You should be happy that C# is around or Java still wouldn’t have enums, variable length args( a proper printf after how many years!), etc.. It still doesn’t have structs on the stack. The generics implementation in Java 1.5 is a compiler trick that won’t give you any more efficiency because it still has to box/unbox and forget about re-constructing types at runtime because it’s still all objects under the hood. ”
GJ started long before C# was even thought of. It basically blazed all the trails. Microsoft had the advantage that they could start with a clean slate, while Java needed to have a retrofit. In that sense, it is hardly a direct result of the existence of C# that such features have made their way into Java.
That said, Java has a fully open process for proposing, prototyping, debating on, and a democratic process to add features to the language.
“Java is fine on the server, but dead on the desktop. Sun let Java rot for too long and now developers have given up on it for the desktop.”
They hardly let it rot. They’ve had a team of developers working on it since day one. That team is now more than 1,000 engineers strong. Java on the desktop is hardly dead–Sun just had a different focus (many different focuses) for the language from the start. The truth is that Sun is undergoing many changes of thought patterns internally and it is likely that Java will emerge a much more viable desktop platform than it is today.
Ask Apple. They’ve been putting a serious effort into supporting Java. Sun agreed to work with them to port Hotspot to MacOS X and Apple stepped up to the plate, porting the JIT compilers to PPC and implemented several whiz-bang behind the scenes features that make Java on MacOS a very solid platform, and steadily improving.
Apple realizes they could tap a much larger market if they offer a great Java implementation. And Apple is hardly a server company!
“If you don’t like unsafe then just don’t use it.”
Ahh, spoking like a true naive developer.
If I don’t like it, I tell the guy that did the piece of crap 2 years earlier that he shouldn’t have??
Code has to be maintained – stop thinking about 1 week ahead. My software has a longer lifecycle than yours apparently.
Reading throguh you’re other comments, it seems like you’re a .NET zealot. Get off it, and think in larger terms than 1 (one) language! – there’s more to life than C# et al. and Java!
Sorry to break it to you, but the solaris team at Sun doesn’t even want to eat the dog food. Remember the leaked memo? If you don’t I can probably dig up a link to it if you reply.
As far as Apple, sure they put a lot of effort into their java implementation, but Apple’s market share continues to dwindle to the point of irrelevance. Let’s talk linux and windows and then talk about what’s on the desktop besides IDES and a few shareware apps like limewire.
Java has always just left a bit of a bad taste in my mouth. The first time I used it I need to read in a unsigned short….no dice. no unsigneds, so I have to do needless bit-twiddling. They’ve finally got a proper printf after so many years. How annoying.
Java isn’t horrible and obviously C# shares a lot with it. If the Gnome crew decides that they’re not going to go with Mono for a dependent runtime then I hope they go with Java or some hodpodge technology that Havoc was talking about yesterday or the day before in the response to Gosling’s blog.
You seem to be pretty clueless about C#, so let me explain it to you. You have to use unsafe in order to leverage native libraries easily instead of the mess that is JNI. Most people will never drop down into unsafe mode to use raw pointers for the hell of it anyway. It has nothing to do with maintainibility.
As far as multiple languages are concerned, MSIL is much easier to target for compiler writers than java bytecode. The language java was designed before java bytecode and MSIL was designed before c#. Java doesn’t have things like a tailcall instruction which languages that use a lot of recursion need for efficiency. Yeah, you can target java bytecode coming from other languages, but it’s not near as easy or as efficient.
Why the hell are you bitching about other languages anyway. THIS thread is about c# you nut.
“You have to use unsafe in order to leverage native libraries easily instead of the mess that is JNI”
negatory…just use p/invoke or create a RCW.
“sun has 1000 devs working on java???” <– @jizzles
just goes to show you, give 1000 sun engineers 1000 typewriters….
If I don’t like it, I tell the guy that did the piece of crap 2 years earlier that he shouldn’t have??
Code has to be maintained – stop thinking about 1 week ahead. My software has a longer lifecycle than yours apparently.
You can’t blame C# for that. The way I see it C# gives you the option to use unsafe code or interop with unmanaged resources. It is an however an option, and is not forced upon developers. If you don’t like using certain parts of the technology then rather than imposing it on all C# developers why not just change the policy within your company?
C# is evolving and moving forward, just becuase someone can’t use certain powerful features correctly, it doesn’t mean that those features shouldn’t be there.
you’ll have to use the /unsafe compiler switch in order to compile pinvoke code…..i’m almost postive.
“You seem to be pretty clueless about C#”
Wrong
“blah blah, unsafe explanation”
Yes, I know that – Don’t know why you assumed I didn’t… nothing in any of my posts that should give you a reason to think so.
“Most people will never drop down into unsafe mode to use raw pointers for the hell of it anyway. It has nothing to do with maintainibility.”
I am not talking about using unsafe *for* maintainability, I am saying that if you expose unsafe this easily, you’ll invite all the C/C++ programmers who hasn’t had any *proper* education in the glory of C# to shoot them selves in the foot. Fast forwarding a couple of months/years I’ll have to maintain that crap. If they hadn’t exposed it, or made it a lot more “uneasy” like JNI, which is quite simple (have you actually done any JNI code??). You can’t do “unsafe” code from Java, you need to pair it with some C/C++ code, whereas you make it easy (at the cost of a ton of other problems) to use it directly from C#. It’s kind of like asm { } in C/C++, but at least C/C++ is a “low level” language, whereas C# isn’t.
“Why the hell are you bitching about other languages anyway. THIS thread is about c# you nut.”
Then why are you talking about MSIL??
Stop calling people “nut”, there’s no reason to go to that level, that doesn’t win any arguments.
“You can’t blame C# for that.”
I don’t blame a language – I blame those that decided that this should be a feature. At least they “locked” it using /unsafe.
“The way I see it C# gives you the option to use unsafe code or interop with unmanaged resources. It is an however an option, and is not forced upon developers.”
100% correct, however in the real world, and not some dick ass college where everything is rose colored, and project plans work, you have unrealistic deadlines and all sorts of other stuff that force you to make some decisions that you’d prefer not to do – and thats just how it is. Now some companies might have a 99% optimal development cycles, but that’d be like 1% of all IT companies…
However, If I were on a c++ team with a short deadline, with a product I didn’t have to maintain (and my ass were on the line, if the product didn’t complete) I *know* I would enable /unsafe on my new c# project to complete it in time.
I wouldn’t care at all for the poor sucker that has to maintain the POC.
“If you don’t like using certain parts of the technology then rather than imposing it on all C# developers why not just change the policy within your company?”
1 – Who says I have the authority to do this
2 – I might not have worked at the company when it was decided to use /unsafe to speed up a project
3 – You can easily “hide” an unsafe argument in the code, without making it a company wide decision.
I am not talking about using unsafe *for* maintainability, I am saying that if you expose unsafe this easily, you’ll invite all the C/C++ programmers who hasn’t had any *proper* education in the glory of C# to shoot them selves in the foot. Fast forwarding a couple of months/years I’ll have to maintain that crap. If they hadn’t exposed it, or made it a lot more “uneasy” like JNI, which is quite simple (have you actually done any JNI code??). You can’t do “unsafe” code from Java, you need to pair it with some C/C++ code, whereas you make it easy (at the cost of a ton of other problems) to use it directly from C#. It’s kind of like asm { } in C/C++, but at least C/C++ is a “low level” language, whereas C# isn’t.
What is your point? The vast majority of “sane” c/c++ programmers don’t drop down into asm { } unless (a) it’ll give them a significant speed bump (b) they have to use a feature of the processor. People want their code to be portable. It’s the same thing with c#, either (a) they want to leverage existing code in the unmanaged world (yes, leveraging existing code is a good thing no matter what kind of WORA spin Sun tries to put on it) or (b) in the very extreme rare cases they drop down into using raw pointers for efficiency or other reasons.
Most people don’t use language features just for the helluva of it. And if you have someone on your team like that, then you should either have a talk with him and if that doesn’t work get rid of him. You can write crap code in any language, I don’t know why you want to handcuff the developer like Sun tends to do. Believe it or not, when Java was being developed, there wasn’t a concensus at Sun about dumbing down c++ as much as they did. Bill Joy, for one, wanted to add some more stuff to it.
I mean what’s the deal with leaving out enums, or variable length args (for a proper printf) – 2 features which Java is finally getting after how many years. I’ll stick to my previous statement that java developers should thank god that .NET is giving Sun some competition in the managed world so that they can finally get some useful language features.
However, If I were on a c++ team with a short deadline, with a product I didn’t have to maintain (and my ass were on the line, if the product didn’t complete) I *know* I would enable /unsafe on my new c# project to complete it in time.
I wouldn’t care at all for the poor sucker that has to maintain the POC.
Well then either the the estimates at the beginning of the project were inaccurate, the risks weren’t managed properly, the developers haven’t been given specific goals in terms of code maintainability and code quality or the client requirements were not very complete.
Either way, it is a business/management problem and not a techical one. Don’t blame C# for the failures of your project manager.
As far as my comments regarding MSIL, I only brought that up because you ended your comments with “c# and for that matter java aren’t the only languages” .NET isn’t about one language, and with these improvements in .NET v2.0 it’ll only up the door for more languages to target the platform. Microsoft has made it no secret that C# and VB.NET will be taking divergent paths in the future and that VB.NET will be more of a java like language with C# doing the heavy-lifting in applications development
Does anyone know when using anonymous methods is a good idea? The traditional way (as shown in the article) seems much more elegant to me. I’m a little unsure as to the merits of this particular feature in C#.
“I mean what’s the deal with leaving out enums, or variable length args (for a proper printf) – 2 features which Java is finally getting after how many years.”
Whell, I’ve never really missed enums, so that particular feature never bothered me. varags however should definately have been there from the start!
“I’ll stick to my previous statement that java developers should thank god that .NET is giving Sun some competition”
Aye, competition is good!
I think that today computer languages are changing too rapidly.
C# has a published standard and they are already revising that standard adding features that are by no way new concepts : generators, anonymous functions, generics, why didn’t they put all that stuff from the beginning ? Maybe adding features is a way to mantain a commercial buzz around but it is really nasty, it is a way to propote incompatibilities down to the source code.
That problem is by no way exclusive to C# : Java has new genericity, Perl reaches version 5 ( or 6 ? ), and C++ has taken 10 years to become a stable language…
On the opposite, C is a quite stable language ( well, till they added booleans : STOP THEM : If it works, don’t fix it ! ) and Lisp has showed that when a language is well designed, the addition of new features can be done without adding syntaxic fixtures ( see CLOS ).
C# was created as a better Java but they have stopped the work half way, making it marginally better and stimulating a race between C# evolutions and Java evolutions, at the expense of programmers… The frightening part is that they are already planning future incompatibilities –er– evolutions, as embedded XML, special database syntax, ( maybe metaprogrammation one day ) …
“Well then either the the estimates at the beginning of the project were inaccurate, the risks weren’t managed properly, the developers haven’t been given specific goals in terms of code maintainability and code quality or the client requirements were not very complete.”
yes, yes and yes!
“Either way, it is a business/management problem and not a techical one. Don’t blame C# for the failures of your project manager.”
Ahh, though I agree with you – it doesn’t help my life that c# (and other languages) have “features” that make my life hell. Apart from C# enabling “stuff” it’s also a “high level” language (ok, not VB) that solves a lot of other issues thats meant to make my life easier, which unsafe then defeats :/
fwiw, unsafe should have been left *completely* out – pinvoke however is a nescessity for some projects (bridging between existing code).
I’m not the biggest fan of anonymous methods in C#, but I guess they have their uses for quick-n-dirty callbacks.
One cool thing that will be coming in v 2.0, is something like this
button.Clicked += HandleClicked;
instead of how it is now, button.Clicked += new EventHandler(HandleClicked);
If you were a windows developer would you want to wait another 4 years for .NET while they hammer out generics and other stuff. I mean, look at the mess that is win32 api and MFC. If I was a windows developer I would rather have something rather than nothing. I’ve heard that the generics stuff has been being worked on in a microsoft lab in the UK for a while, but wasn’t primetime for the .NET release.
Generics have been around for MANY years. IMHO, they should have implemented it from the beginning or, better, create a language allowing such extensions.
For example, when I first read the Java specs, I was astonished that they didn’t implement operator overloading. For me, it showed that Sun considered it as a draft implementation or for limited tasks. History has shown that if Java have had from the beginning features like generics, functionals, generators, and so on ( that were known techniques at that time ), C# wouldn’t have existed.
Stop calling people “nut”, there’s no reason to go to that level, that doesn’t win any arguments.
Oh I see. But calling him a zealot is ok and *does* win arguements?
I’m not the biggest fan of anonymous methods in C#, but I guess they have their uses for quick-n-dirty callbacks.
If you use generics a lot, and specifically generic algorithms such as foreach or sort, anonymous methods can be a HUGE help. Having a separate named method that is used exactly ONCE in a program is a grand waste of coding time. Heavy use of generic algorithms will require you to write a lot of quick used once type methods, hence why anonymous methods is essential.
“Java has always just left a bit of a bad taste in my mouth. The first time I used it I need to read in a unsigned short….no dice. no unsigneds, so I have to do needless bit-twiddling. They’ve finally got a proper printf after so many years. How annoying.”
It’s unfortunate that you didn’t discover that a char in Java is a 16-bit unsigned integer.
“Sorry to break it to you, but the solaris team at Sun doesn’t even want to eat the dog food. Remember the leaked memo? If you don’t I can probably dig up a link to it if you reply. ”
Sorry to break it to you, but Sun, like any big organization is made up of a bunch of little teams. There is no hive mind. There is no one purpose. There are a lot of dedicated engineers who feel that their own work is the most important. And there are a lot of turf wars.
A leaked memo from a person with one of the more radical viewpoints hardly substantiates anything you say.
I have nothing to draw these conclusions on except WORKING INSIDE OF SUN ON JAVA AND SOLARIS FOR MONTHS AND MEETING THE POWERS THAT BE. And yes, that means getting down to brass tacks and building new functionality deep inside both–not some willy nilly (oh shit, no unsigned types, I think I know all there is to know now and I’ve discovered it sucks).
Your experience with Java, with the people who invented it, and with Sun’s internal structure are all lacking. I find your generalizations inaccurate.
Anonymous methods in C# are what other languages call lambdas. Lambdas are tremendously useful, and can be used to implement a wide range of language features that are traditionally hard-coded (eg: object systems, iterators, generators, etc). Now, C# has most of these features built-in, but the key thing about lambda is that it gives the programmer the power to implement these things too. In turn, this power can be applied to situations where C# doesn’t have the built-in feature you need.
Probably the quickest use for lambdas is looping constructs. How often do you see loops nested three layers deep? Lambdas (and first-class functions) allow constructs that get rid of lots of looping to make code clearer.
Note on code examples in Dylan:
– Dylan has no ‘return’ statement. It is implicit.
– #f means ‘false’
– “method(…args…) …body… end’ is a lambda.
– member(object) is the same as object.member.
– The above means that members can be accessed as lambdas.
– Identifiers can have funky characters like ‘?’
– ~ is the negation operator.
For example: Say you’ve got a list of objects, lst, representing students in a class. Each students final exam score is stored in a member final. You want to get a list of all the students who passed the final.
let passed = choose(method(x) x.final > 60 end, lst);
choose will iterate over your collection, use your anonymous method as a predicate, and return you a new list containing the students who passed.
Lambdas become very powerful when you need to do more complex iterations. For example, say you want to calculate a class average, but exclude those who dropped the class before the end.
let lst = choose(completed?, lst)
let avg = reduce1(method(sum, x) x.final + sum, lst) / size(lst);
reduce1 will apply the given lambda to every element in the list, passing the return value to the next lambda it calls.
There are lots of other such functions, and lambda allows you to write your own — seperating out the general form of the looping from the details of what the loop does.
One very powerful aspect of most lambda implementations (including C#’s) is that they have closure semantics. When you create a lambda, they anonymous function can capture variables from its context, and will have access to those variables even after the function returns.
An example. Say you are writing a number library. You want to present an enumerator-style interface to prime numbers. Assume you’ve got a predicate prime-number? that tells if an integer is prime or not. (sorry ’bout the underscores, OSNews won’t show spaces).
define method gen-prime-generator()
__let curr-prime = 1;
__method()
____while(~prime-number?(curr-prime))
______curr-prime := curr-prime + 1;
____end;
____curr-prime;
__end;
end;
Now, you can do:
let next-prime = gen-prime-generator()
next-prime() // Returns 1
next-prime() // Returns 2
next-prime() // Returns 3
next-prime() // Returns 5
…etc…
The same technique can be just as easily be applied to, say, filtering out certain video modes in the enumerator-interface of a graphics library.
Note, in this case (but not many others), you can do the same thing with an class that implements a ‘next-prime’ interface. However, this leads to the “class glut” that Java is famous for. In object-oriented languages that have lambdas/closures (Smalltalk, Dylan, CLOS), everything is an object, but you only have to use classes where they make sense, not because the language forces you to.