New Features in C# 6 (2014)
blogs.msdn.com
blogs.msdn.com
I love the human style Microsoft has taken on in the past few years.
Lambdas, null conditionals, LINQ, compile-time generics, etc, none of these features on their own are all that amazing, but when you add them all up it just makes for a more pleasant experience.
Most of my time is spent in Java, building an Android app right now, and C#, building services, and I constantly feel like I'm doing battle with Java's syntax (it's interesting to me that the new Android Studio compresses lambda-like classes, like runnable, to look like lambdas).
In other words, LINQ should have been a library, not a language feature. (In my ideal world. It is possible that someone with better info could see such a move as a terrible mistake.)
I do agree though that LINQ seems to hint at and start evolving C# into a different semi-functional kind of language, and what we're stuck now with is a language with one foot in both worlds. Hopeful another language will come along one day and chisel out those ideas into a more pure form. (and I'm not meaning a pure functional language like for instance F#)
There is obviously a relationship between code like
.Select(blah).From(blah).Where(blah)
and SELECT blah FROM blah WHERE blah,
and I think it would be worthwhile to look into a language feature that allowed a reliable translation between these kinds of things. Maybe I want to write LET a = blah IN blah
from .Let(a, blah).In(blah)
or some such thing. It it just syntactical sugar? Is it a pattern that can be abstracted well?Most of the features that came along around the time of LINQ (var variables, extension methods, anonymous classes, lambdas etc) was done specifically to support the LINQ kinda syntax but LINQ in itself is not so much part of the language as it is of the .net libraries.
Btw, I personally think the "dot notation" syntax is superior to the "query expression". It's easy to follow from start of the line to the end and you get great autocompletion along the way.
No, that's flat-out wrong. LINQ's main feature is turning code into data: Put code in, get an expression tree out.
I suppose it is possible to build expressions with just libraries, but that would be very ugly (like control flow in XML) :)
I kinda wish they'd put more thought and energy into some tidy way of doing error checking, pre-conditions etc. locating and avoiding bugs is still one of the main concerns in any project and not enough are done to help with that. There are extensions but they are all a little clunky or require too much effort.
If they did that correctly I think it could be a game changer on par with Linq and rx
You need at least the Premium edition for the required tools.
Update: On second thought, I think Code Contracts were already available on the free Visual Studio 2013 Community edition: https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363...
Have you read the whole principles behind code contracts?
Derived classes must uphold the same contracts as the base classes, they are only allowed to strengthen them, not to drop conditions.
"...the subtype must meet a number of behavioral conditions. These are detailed in a terminology resembling that of design by contract methodology, leading to some restrictions on how contracts can interact with inheritance:
Preconditions cannot be strengthened in a subtype.
Postconditions cannot be weakened in a subtype.
Invariants of the supertype must be preserved in a subtype."
I was generalizing and though only about pre-conditions.
GRR TOO MANY OPERATORS! Also syntax, and stuff.
But I don't use c# so I've no real skin in the game. Just trying to help get things out of everyone's system.
Ideal language for me is somewhere between C# and F#. C# is moving there (but F# doesn't).
Before I knew what lambdas were JS was hard to read too, and before I learned inheritance both Java and C# were confusing messes.
And what would F# need to move to? You can type out the types now, you can do all the OO of C#. The only thing I can think you mean is as add curly braces around functions and classes, which wouldn't make it objectively easier to read, just more familiar to some C# developers. Unless you mean add higher kinded types, but that would move it closer to Haskell.
var someVar = class1.class2.SomeProp;
would return null, even if class1 was null. I have a hard time coming up with cases where you actually would want to force an exception. I realize however they reason for the new operator since they might not want to change how existing code works...
In many cases you can guarantee that the sub-property is not null and you can safely access the value. I think the proposed operator `.?` is the perfect solution and gives you the flexibility to decide what you can guarantee and how you want to access the sub-property.
But value types are a good point, either it could then set it to default(T) or not set it at all, but it kinda has to be default(T) because null makes most sense for reference types. Might give some unexpected behavior if it variables are set to 0 because of a null reference
# authenticator == null
if (authenticator.getSessionError(session) == null) {
# Assumed session is okay, actually the bug is null authenticator
}You get the feeling though many of these problems comes down to null's largely being a language defect in itself which should have been adressed differently
I'm most excited about being able to watch the result of lambda functions while debugging (not mentioned here), should make things much easier.
> int? length = customers?.Length; // null if customers is null
Surely, a primitive int cannot be null (at least in Java).
Thanks; this shows how rusty my C# skills have become.
C# has improved fairly rapidly. I haven't done it professionally in some time now and I feel like I don't have a good handle on the full language anymore.
I'm somewhat concerned the language is getting too "big", but I'm not sure it's at that point yet. They keep adding solid features.
(implemented with as a macro AFAIK)
More to the point: feature known as string interpolation is really, really old. And popular. Just go here: http://rosettacode.org/wiki/String_interpolation_%28included... and see for yourself. TCL is from 1988, for example, which predates Scala quite a bit. Oh, and also predates Swift.
Yeah, I'd like people who comment on language features to study PL design history a tiny little bit more.
We were looking for popular languages that use a syntax similar to `\{x}` for string interpolation. TCL uses a dollar sign, so pretty much as far from the C# syntax as possible (inside of the limited syntactical range out there). At least this page does not list any language that uses the same syntax: http://rosettacode.org/wiki/String_interpolation_%28included.... But maybe your vast knowledge of PL design history can provide information on one that does?
I highly recommend reading through https://roslyn.codeplex.com/discussions/570551 and the other four revisions of the nameof() spec, they do a very good job at explaining the tradeoffs of each design.
C# already has a ton of those, the most prominent so far are extension methods.
Foo.Blergh()
can be implemented by a class that offers
static void Blergh(this Foo foo) {}
and the compiler will just replace the call above, looking like a specific thing Foo can do with
SupportingClass.Blergh(thatFooInstance)
I'd say the nameof() implementation is most likely similar and will just replace the name of the local symbol with the symbol itself, during the compilation.
Yes, nameof is compile-time. Similarly, the ?. generates code (a "? :" or if statement) and so does the string interpolation (a call to string.Format).
I've even sketched an outline of a talk of the c# language features that are "just" sugar. e.g. walking through a statement like
var evens = numbers.Where(x => x % 2 == 0);
and showing what it looks like without the compile-time sugar features like the lambda, extension method use of "Where", "var" inference, etc.Of interest I suppose to just-beyond-beginner devs
That will clean up quite a bit of my code.
I'm missing a line that says
using static Math.Sqrt; using System.Math;
System.Math is a static class, not a namespace. It doesn't import a single static method, it imports the entire class.However, in the italicized note, you can see they changed their minds after releasing the preview. It's going to be "using static", but they didn't update the example. It will still operate on the entire class and not an individual method though.
It's easy to mess up a null check, but it's equally easy to find the bug and fix it.
It's easy to mess up a string format, but it's equally easy to find the bug and fix it.
It's easy to mess up a chain of Rx event streams and when you do--you're out of luck buddy. Have fun debugging that.
Async rewriting for error handling does solve a really difficult problem, but there are much deeper problems with using C# for the kind of imperative threading code where that problem comes up. I wouldn't write that in C#, that's certain.
Don't get me wrong: I like that syntax change--I'm all for shorter, cleaner code. But I've moved on from C# because it was ineffective for solving hard problems like multithreading, scaling, networking, crypto. Eliminating a few if statements doesn't fix that.