RajasekharwhY / Build-your-muscle

Learning concepts and sample practice code - C#, MVC, SQL etc..
2 stars 0 forks source link

Updates from C#5 to C#6 and C# 7 #15

Open RajasekharwhY opened 5 years ago

RajasekharwhY commented 5 years ago

What language features are we missing if we use C# 5 ??

RajasekharwhY commented 5 years ago

Top level highlights by version wise 👍 C# 6.0 and C# 7.0 mainly added features which are helpful for developers for clean code. That means they did not added a big language feature (unlike c# 5.0 - One big language feature the async and await model for asynchronous programming) they added bunch of small C# features in 6.0 and 7.0.

C# version 4.0 C# version 4.0 would have had a difficult time living up to the groundbreaking status of version 3.0. With version 3.0, C# had moved the language firmly out from the shadow of Java and into prominence. The language was quickly becoming elegant.

The next version did introduce some interesting new features:

Dynamic binding Named/optional arguments Generic covariant and contravariant Embedded interop types Embedded interop types alleviated a deployment pain. Generic covariance and contravariance give you more power to use generics, but they're a bit academic and probably most appreciated by framework and library authors. Named and optional parameters let you eliminate many method overloads and provide convenience. But none of those features are exactly paradigm altering.

The major feature was the introduction of the dynamic keyword. The dynamic keyword introduced into C# version 4.0 the ability to override the compiler on compile-time typing. By using the dynamic keyword, you can create constructs similar to dynamically typed languages like JavaScript. You can create a dynamic x = "a string" and then add six to it, leaving it up to the runtime to sort out what should happen next.

Dynamic binding gives you the potential for errors but also great power within the language.

C# version 5.0 C# version 5.0 was a focused version of the language. Nearly all of the effort for that version went into another groundbreaking language concept: the async and await model for asynchronous programming. Here is the major features list:

Asynchronous members Caller info attributes See Also Code Project: Caller Info Attributes in C# 5.0 The caller info attribute lets you easily retrieve information about the context in which you're running without resorting to a ton of boilerplate reflection code. It has many uses in diagnostics and logging tasks.

But async and await are the real stars of this release. When these features came out in 2012, C# changed the game again by baking asynchrony into the language as a first-class participant. If you've ever dealt with long running operations and the implementation of webs of callbacks, you probably loved this language feature.

C# version 6.0 (https://msdn.microsoft.com/en-us/magazine/dn802602.aspx)

With versions 3.0 and 5.0, C# had added major new features in an object-oriented language. With version 6.0, it would go away from doing a dominant killer feature and instead release many smaller features that made C# programming more productive. Here are some of them:

  1. Static imports : Introduced capability of importing Static classes, so you can access methods of static class with out referring the static class. Example :

    using static FileHelper; // in a member ExtractSimpleFileName(file)

  2. Exception filters 3.Auto-property initializers 4.Expression bodied members 5.Null propagator 6.String interpolation 7.nameof operator 8.Index initializers Other new features include:

10.Await in catch/finally blocks 11.Default values for getter-only properties Each of these features is interesting in its own right. But if you look at them altogether, you see an interesting pattern. In this version, C# eliminated language boilerplate to make code more terse and readable. So for fans of clean, simple code, this language version was a huge win.

They did one other thing along with this version, though it's not a traditional language feature in itself. They released Roslyn the compiler as a service. The C# compiler is now written in C#, and you can use the compiler as part of your programming efforts.

C# version 7.0

The most recent major version is C# version 7.0. This version has some evolutionary and cool stuff in the vein of C# 6.0, but without the compiler as a service. Here are some of the new features:

  1. Out variables 1.Tuples and deconstruction 2.Pattern matching 3.Local functions 4.Expanded expression bodied members 5.Ref locals and returns 6.Other features included:

7.Discards 8.Binary Literals and Digit Separators 9.Ref returns and locals 10.Throw expressions All of these features offer cool new capabilities for developers and the opportunity to write even cleaner code than ever. A highlight is condensing the declaration of variables to use with the out keyword and by allowing multiple return values via tuple.

But C# is being put to ever broader use. .NET Core now targets any operating system and has its eyes firmly on the cloud and on portability. These new capabilities certainly occupy the language designers' thoughts and time, in addition to coming up with new features.

RajasekharwhY commented 5 years ago

When looking at new C# 6.0 features ... like Auto property Initializes and Index initializes, it is also time to remind the object initializes ( which are introduced in C# 3.0) which bought more Convenient way to initialize the objects.

public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public int Age { get; set; } public string Address { get; set; } }

class Program { static void Main(string[] args) // Object Initializes example. { Student std = new Student() { StudentID = 1, StudentName = "Bill", Age = 20, Address = "New York"
}; } }