What’s New in C# 7.0

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2582 Views - Last Post: 28 April 2017 - 09:52 AM

#1 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

What’s New in C# 7.0

Posted 06 September 2016 - 08:07 AM

What’s New in C# 7.0

Quote

What follows is a description of all the planned language features in C# 7.0. With the release of Visual Studio “15” Preview 4, most of these features are coming alive. Now is a great time to take them for a spin and tell us your thoughts!

This is really just a useful link for those interested. However, in terms of discussion value:

Is there a feature you are particularly excited about?
Is there anything still missing that you'd like to see?

Is This A Good Question/Topic? 2
  • +

Replies To: What’s New in C# 7.0

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: What’s New in C# 7.0

Posted 06 September 2016 - 08:35 AM

The tuple sugar is nice. With the local functions thing, I can almost write F# in C# now. A few more immutability features, like non nullable... I like how the comments degenerate into a VB.NET must die flame war almost instantly.

At this point, C# is closing in on C++ as a multi paradigm kitchen sink language.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: What’s New in C# 7.0

Posted 06 September 2016 - 10:32 AM

The Tuple sugar is all I've been asking for since I saw it in other languages. Anonymous types are great, but Tuples are easier to pass between scopes.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: What’s New in C# 7.0

Posted 06 September 2016 - 11:15 AM

Noticed this:

"Tuples are value types, and their elements are simply public, mutable fields."

Mm immutability (a la Python) requires a different way of thinking. Still, I don't suppose it matters in the great scheme of things.



Quote

Mads Torgersen - MSFT
The reasons why mutable structs are often bad, don’t apply to tuples.

If you write a mutable struct in the usual encapsulated way, with private state and public, mutator properties and methods, then you are in for some bad surprises. The reason is that whenever those structs are held in a readonly variable, the mutators will silently work on a copy of the struct!

Tuples, however, simply have public, mutable fields. By design there are no mutators, and hence no risk of the above phenomenon.

Also, again because they are structs, they are copied whenever they are passed around. They aren’t directly shared between threads, and don’t suffer the risks of “shared mutable state” either. This is in contrast to the System.Tuple family of types, which are classes and therefore need to be immutable to be thread safe.

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: What’s New in C# 7.0

Posted 06 September 2016 - 11:23 AM

This article may be of interest to some as well:

Proposal for C# Non-Nullable Reference Types
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,829
  • Joined: 05-May 12

Re: What’s New in C# 7.0

Posted 06 September 2016 - 07:34 PM

I can't find whether primary constructors will make it back into C# 7.0. Has anybody heard? I was disappointed when this was cut from C# 6.0.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: What’s New in C# 7.0

Posted 07 September 2016 - 02:42 AM

I like the improvements they're making to the switch construct. I've always been frustrated every time I get they "Switch has to use a variable of this specific type and you're a dumbass for trying to do what you want" message.

Defining an out parameter in-line is a little cleaner too.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: What’s New in C# 7.0

Posted 07 September 2016 - 03:29 AM

Assuming that primary constructors have been put on the back burner, there is a discussion here with a variety of opinions:

Changes to the language feature set

Personally, given that opinions are split on whether they are a good or bad feature (and syntax) to introduce, I think this should be enough to keep them out of C#.

Talking of a burner (awaits flames).. ;)
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: What’s New in C# 7.0

Posted 07 September 2016 - 06:13 AM

Primary constructors... nope, don't like em. :P

I like all my initialization to happen in one place, the same place, not all over the bloody place. I actually dislike variables assigned outside constructors, so I might be on the fringe with this one.

Of course, on similar principal, I wasn't a fan of the object initializer with the attributes, but now I'm quite fond of it. Of course, using that mechanism doesn't make my class messy.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,829
  • Joined: 05-May 12

Re: What’s New in C# 7.0

Posted 07 September 2016 - 06:50 AM

If you don't like your class initialization all over the place, then you aren't using the C# 6.0 expression bodied automatic getters, either?
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: What’s New in C# 7.0

Posted 07 September 2016 - 07:29 AM

Hmm... I don't see how that "expression-bodied function members" impact initial state. Maybe I'm missing something. It's pretty much just sugar for people scarred by writing too many anonymous functions or having curley hate, no?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,829
  • Joined: 05-May 12

Re: What’s New in C# 7.0

Posted 08 September 2016 - 06:41 AM

Sorry, I think I used the wrong term. I was actually talking about initializing auto-property initializers. It was the expressiveness of what somebody can put into the initializer that was throwing me off.

See Passengers below for example of expression bodied automatic getter:
class Car
{
    public string Make { get; }
    public string Model { get; }
    public List<Person> Passengers { get; } = new List<Person>();

    public Car(string make, string model)
    {
       Make = make;
       Model = model;
    }
}



as opposed to:
class Car
{
    public string Make { get; }
    public string Model { get; }
    public List<Person> Passengers { get; }

    public Car(string make, string model)
    {
       Make = make;
       Model = model;
       Passengers = new List<Person>();
    }
}


Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: What’s New in C# 7.0

Posted 08 September 2016 - 07:10 AM

The first version doesn't excite me ;).

I might not even notice the initializer on first look, dangling way over there on the right.

I shall call it a "dangling initializer" from now on :)
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: What’s New in C# 7.0

Posted 08 September 2016 - 07:27 AM

Agreed. I couldn't tel you the last time I had a property that was that "first semester simple".
Most properties tend to look more like this, where you can set the default value in the private backing field:

        #region IsDirty
        private bool _IsDirty = true;
        [XmlIgnore]
        [JsonIgnore]
        [DoNotPersist]
        public bool IsDirty
        {
            [DebuggerStepThrough]
            get
            {
                //if (_IsDirty == null) _IsDirty = new bool();
                return _IsDirty;
            }

            [DebuggerStepThrough]
            set
            {
                if (_IsDirty == value) return;
                OnPropertyChanging(() => IsDirty);
                _IsDirty = value;
                onpropertychanged(() => IsDirty);
            }
        }
        #endregion IsDirty


This post has been edited by tlhIn`toq: 08 September 2016 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,829
  • Joined: 05-May 12

Re: What’s New in C# 7.0

Posted 09 September 2016 - 06:17 AM

andrewsw: If that didn't excite you, this will probably get a reaction out of you:
class Person
{
    private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>();
    private int id = GetId();

    public Person(string name) => names.TryAdd(id, name); // constructors
    ~Person() => names.TryRemove(id, out *);              // destructors
    public string Name
    {
        get => names[id];                                 // getters
        set => names[id] = value;                         // setters
    }
}


from the "More expression bodied members" of that same link above.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2