Author: Sheik Mohamed
Features of C# 7.0
C# 7.0 Features
Last article we have looked the features of C# 6.0, so in this article we will see the features of C# 7.0
C# 7.0 | March 2017 | .NET Framework 4.6.2 | Visual Studio 2017 |
out Keyword
The out parameter can be used to return the value in the same variable and as a parameter of the method, if any changes made to that parameter it will reflect the changes to that out variable.
In earlier version
In C# 7.0
Here no need to declare it separately, specify directly in the argument itself.
Tuples
C# tuples as richer anonymous types, it’s a set of elements.
You can access name and age by referencing Item1 and Item2.
In C# 7.0
Here we can directly get the name and age instead of Item1 and Item2
Discards
Discards are equivalent to unassigned variables, that variable may not even be allocated storage
They don’t have names, instead, they are represented as a _ (underscore.)
Pattern Matching
It’s not new to C#, in the earlier version itself introduced but in the coming version its added some more features in the implementation.
- Pattern matching has the ability to extract the data from the expression.
- Pattern matching can be used with any data type including custom whereas if/else can only be used with primitive types.
In C# 7.0, introduced 2 more features like “Is expression” and “Switch case statement”
With “Is” pattern
Pattern with Switch…case statement
Now Switch…case statement also works with expression as well.
Ref local and returns
Ref Locals
Before C# 7.0 it was not possible to declare the return type with the ref modifier. While this feature was available using IL code, the feature was not reflected with C#. This changes with C# 7.0. However, before looking into this, let’s get into another new C# 7.0 feature: ref locals. Local variables can be declared with the ref modifier. Here, the variable x1 references variable x, and thus changing x1 changes x as well
output: local variable x after the change: 2
Ref Returns
The ref keyword can also be used with the return type. This allows code as shown in the following code snippet. Here, an array of type int is declared and initialized. In the next line, a local ref variable is declared that references the first element of the array. This variable is then returned in the last statement of the method
Local Functions
Local functions enable you to define a function within the scope of another method to help in promoting encapsulation and bring local variables into scope.
More expression-bodied members
Expression-bodied methods was introduced with C# 6.0, that simplify the syntactic expression for methods in C#. We have seen this for Methods and Properties in the previous version of C#. C# 7.0 extend this features for several new members including constructor, destructor, property assessors etc.
In earlier version
In C# 7.0
Throw expression
C# 7.0 introduces throw expressions. We can add exception throwing to expression-bodied members, null-coalescing expressions and conditional expressions. This blog post introduces throw expressions, demonstrates how to use them and also provides a peek behind a compiled throw expression.
Generalized async return type
Running a Task from async methods can introduce performance bottlenecks in certain paths.
Ever since C# 5.0 when the async/await pattern was introduced, the only supported return types were Task<TResult>, Task, and void
If the directory is empty, the known space is 0 and there is no need for an asynchronous thread to calculate the size. However, since Task<long> is the return, it still needs to be instantiated.
By using ValueTask<….> we don’t want to instantiate the Task<long>, it can return Task or Task<long> and so on.
Numeric literal syntax improvements
The digit separator “_” can be used inside number literals now. The purpose of a digit separator is to improve readability, nothing more
public const int One = 0b0001;
public const int Two = 0b0010;
public const int Four = 0b0100;
public const int Eight = 0b1000;
public const int Sixteen = 0b0001_0000;
Note: 0b -> indicates as binary
Conclusion
I hope this article will help much to understand the features of C# 7.0, in the next article we will see the features of C# 7.1 and 7.2
Features of C# 7.1 and 7.2
C# 7.1 Features
In the previous article we have looked in to the features of C# 7.0, in this article we will see the some of the features introduced in C# 7.1 and 7.2
C# 7.1 | August 2017 | .NET Framework 4.6.2 | Visual Studio 2017 version 15.3 |
Default literal expressions
When we declare the local function or variable or methods until C#7.0 we need to instantiate that in the code, but the newer version of C# not necessary to create a default type of that value.
In C# 7.0
In C# 7.1
Here we don’t want to specify the type of the function or variable, by default its taking.
Tuple Name inferred
The names of tuple elements can be inferred from tuple initialization in many cases.
In C# 7.0
In C# 7.1
Here we don’t want to give the name, its taking from the variable name itself.
Async and Await in Main method
The purpose of it is for situations where your main method calls one or more async methods directly. Prior to C# 7.1, you had to introduce a degree of ceremony to that main method.
In C# 7.1
Pattern Matching with Generics
We have already got enough information about Pattern matching in C# 6.0 itself, now in C# 7.0 added some additional feature like “Pattern Matching with Generics”, in the previous version there is a design flow in the pattern matching with switch case statement.
Here the compiler indicates an error, because we cannot match the pattern with generics, so in order to avoid the problem in the next version introduced the feature.
C# 7.2 Features
Private Protected
This is the new access modifier in C#, previously we had private, public, protected and so on, this feature is enable the possibility to access the private member from the parent class and that too within the same assembly.
Non-trailing named arguments
In C# 4.0 a new type of argument is introduced known as a named parameter. Using this feature, we can specify the value of a parameter by parameter name regardless of its ordering in the method.
Here we can give the parameter in any order with the specified named parameter.
Leading underscores in numeric literals
The implementation of support for digit separators in C# 7.0 didn’t allow them _ to be the first character of the literal value. Hex and binary numeric literals may now begin with an _.
Reference semantics with value type
out: option to set the value
ref: Should set the value before passing it.
in: This method does not modify the value of the argument used as this parameter.
The “in” keyword specifies that you are passing the parameter by reference and the called method does not modify the value passed to it.
Conclusion
I hope this article will help much to understand the features of C# 7.1 and 7.2.
C# 6.0 Features Overview
C# 6.0 Features.
C# is a general-purpose programming language, it’s an elegant and type-safe object-oriented language that enables developers to build a variety of robust, secure applications in .NET framework.
C# 6.0 | July 2015 | .NET Framework 4.6 | Visual Studio 2015 |
Read-Only Properties
Properties are an extension of data field in C#, data fields are not directly accessible from outside class, we must use the GET, SET methods to access it.
In general, read only properties allows the properties to set only inside the scope, outside classes can only read.
In C# 6.0, it is not necessary to specify the private set, by default it’s a read-only property when we don’t have a SETTER
Auto-Property Initializer
An auto-property initializer allows you to set the value of the property at the same time you declare it in a class, in the earlier version we need to set the property value in the constructor.
In earlier version of C#
In C# 6.0
Expression bodied function member
Expression-bodied function members allow properties, methods, operators and other function members to have bodies as lambda like expressions instead of statement blocks. Thus, reducing lines of codes and clear view of the expressions.
In earlier version of C#, the function will be like.
In C# 6.0
So, we can directly write the body as an expression here.
Using static
The using static enhancement; enables you to import the static methods of single class. Previously, the using statement imported all types in a namespace.
In earlier version
C# 6.0
Null-conditional operators
It’s a new feature in C# 6.0 which will bring more productivity for the developers by reducing the number of lines.
In earlier version
In C# 6.0
String interpolation
This feature inserts values into a string with simple syntax. It is similar to string.Format, but variables may be accessed directly (not through index arguments).
In earlier version
In C# 6.0
Exception filters
If you want to use Exception Filter, you have to declare it in the same line where you declared the catch block, just like this: catch (Exception ex) if (FilterIsTrue). Here, if the parenthesized expression after the “if” returns true, the associated catch block will execute. Otherwise, it will move forward.
nameof Expressions
Evaluates to the name of a symbol
In earlier version
In C# 6.0
Await in catch and finally blocks
In earlier version of C# itself we can use try, catch and finally blocks together but was not able to use the Async and await in catch and finally blocks, so the next version of C# 6.0 introduces the features to use the await in catch {} and finally {} blocks without any complicated structure.
The await operator is applied to a task in an asynchronous method suspend the execution of the method until awaited task completes, the asynchronous method in which await is used much be modified by the Async keyword.
Index initializers
Initializing dictionaries and other objects with indexers is less elegant. We are adding a new syntax to object initializers allowing you to set values to keys through any indexer that the new object has
In earlier version
In C# 6.0
Conclusion
I hope this article would help to understand the features of C# 6.0, in the next article we will see the features of C# 7.0
GIDS 2017
Mr. S. Sheik Mohamed
Ramarson Technology Developers LLP, Trichy, Tamilnadu, India.
Great Indian Developer Summit (GIDS) 2017 at Bangalore.
GIDS is one of the best developer summit in India, it’s an excellent opportunity to upgrade our knowledge to the current Market requirements, speakers from all over the world shared their knowledge and current trends in the IT industry. There are lots of delegates participated in that event, they talks about 10 important technologies to change the developers landscape, which is a highlight in the GIDS 2017. The speaker Mr. Arun Gupta – vice president of developer advocacy at couchbase explains the technologies and concepts very interestingly to the audience. He gave the summery about Containers, Micro services, CI & CD, DevOps, IoT, Server less, Virtual & Augmented reality, AI & Machine learning, Block chain, ChatOps, he explained the overview of these technologies and how they lead in the technology world in few years to the developers concerns and how they change the developers activities and so on.
The interesting thing was about the containerization concepts, because the deployment is really painful when we are building the large scale application and deploy into the customer side. The developers are facing so much of problem while fixing it in the customer side, so the containers will overcome those problems and make a smooth deployment in the customer side. Now a day the containers play a vital role and great feature for the developers to make the life easier. Followed by a lot of speaker explains the trends and technologies about cloud platform from Amazon, Google, etc., they talks about how the cloud is playing the important role in the current industry and 12 factors to cloud success and how Docker image is important and what benefits its bring out from kubernetes in the deployment also interesting in the GIDS.
Now a days IoT is one of the hot topics in the industry, people are moving towards more and more to automate the things to reduce the human efforts. I gained some clear picture about how the systems are connected each other and how they are talking thru the cloud platform, little things about UPnP protocols in the Internet Of Things, how its evolving in the technologies are very interesting.
Mr. Venkat Subramaniam, founder of agile developer explains the Functional to Reactive programming techniques. He performed a coding session about concepts like Functional compositions, Lazy evaluation, immutability, declarative programming vs Imperative programming, it really grabbed the attention of developer, and got good understating about why the reactive programming is important to the developers, how its changing their life, how do we become a smart programmers in the software development and so on.
Finally, GIDS 2017 was really very interesting event I attended this month and it brings a lot to know for the software developers to become a smart developer in the world!!!
Summary of “Golang”
For the past few days I was engaged in learning a new programming language called “Golang”, it’s a simple programming language for developing robust scalable software applications. I have explored many new things in the learning session. I would like to share my ideas and my learning experiences On Golang programming language.
Go Vs other programming languages?
Golang is a general purpose programming language which can compete with C and C++ languages, Go has a simple syntax with a minimum set of standard key words, the interesting thing with Go is, one can build complex applications, with simple programming model. We can put our effort in higher level abstract things. When we look the syntax of Golang, it eliminates lot of standard which are usual in C#, Java or C++. Go inherits many concepts from C language. To give few example, like how simple the language is, we don’t need to specify a semicolon (;) to terminate the line, no access modifiers in Golang. We can do it by declaring the function name starting with upper case or lower case. Most important things are, its compilation time is much faster than other languages, and even compiling very large projects is done in few minutes.
Generally, it eliminates some standard patterns from Programming Language Theory, the language designers have made it simpler to achieve more performance in critical real world application. The compiler directly converts from high level language to machine language, i.e. we don’t need any intermediate steps like JVM for Java, so the compiler quickly builds the application. Once the basic syntax in “Golang” is familiarized, we can develop application with its inbuilt standard packages without depending on any 3rd party Frameworks/tools.
Go for Fresher’s than experienced developers
I would say comparatively Golang will be more attractive to the fresh software engineers than the experienced developers, because when we start working with Golang its provide very simple syntax to concurrent programs. The learning curve is bit faster, but for the experience developers it will take some time to understand the core benefits of Golang programming language because for the past few years If someone developed their real world application using Microsoft technologies or any other technologies, it will not be so easy to quickly adapt this new language, it will take some time to understand the language features. Once core concepts are understood, one can build real world applications.
“Unlearn something to learn new things”
Go syntax is really different than other programming language, for example: We don’t need while loop but we can achieve the same thing via “for” loop, so by learning one looping statement we can do the same thing what we did for other programming languages, so it gives flexibility to build the application.
Concurrency with GO
I have learned concurrency mechanism in this session. It is one the core feature of GO, very useful and we can use the standard library packages rather than using any additional packages like a TPL and so on, with simple keyword we can execute multiple function concurrently using the keyword “go” calling a routine, so the go routines will be running independently and automatically, this will increase the performance as well, here the channel is playing the important roll to synchronize the go routines.
Go with Interfaces
Go provide language features like interface and inheritance. Similar concepts like in other languages, we achieve the same thing what we achieve in other programming languages like C#, which means I would not say Go programming language is perfect object oriented programming languages.
Comparing to other Object Oriented languages. Go is much simpler, for defining the interfaces and types and implementing those interfaces by concrete methods on respective struct, so the compiler will check who are all implementing those methods from the Interface, they can have an access to the interfaces. Its bit difficult to grasp or understand the things who came from traditional object oriented programming.
Go With Web
We can create web application using standard library package called “http” and some sub packages as well, here the http middle ware stuffs is interesting to manage the shared functionalities, i.e. log the application trigger or any other authentication mechanism in the real world application and so on. JWT is also important concept to understand the token based message exchanging mechanism of JSON data across the client and server. There are lot of built in packages available to extend the application and customize more.
Unit testing
This particular section was really interesting for writing unit test for our application, here we are simply using the “testing” shared library packages and start writing the unit testing, for the assertion we can use Omega packages, but we don’t want to mention the packages as well, there are lot of interesting points in the unit testing, because it provides some interesting features like “Benchmark” test, it is really useful to benchmark the flow and produce the average result across different scenario, another interesting things is we can run the unit tests in parallel and can skip some long running unit tests using a flag by command line.
Conclusion
Overall the session was really useful to understand the concept of new programming language and to understand why its differ from other languages and so on, for me the interesting features which attracted myself to GOLANG is its concurrency, channels, interfaces, inheritance, unit testing, packages, so overall Go is a language, easy to learn for fresher’s compare to the other experienced developers, because they can quickly understand and adopt. An experienced C# developer, will take some time to understand the changes, if someone is open minded and willing to unlearn some standard programming concepts he learned and practiced in Java or C#.
One needs to put some more effort and change the way he programs to speed up the learning process and use Go for building complex robust scalable solutions.
Thanks to Mr. Siju Varghese, Solution Architect who specializes in Go cloud and Docker.
Clean Coding
I would like to share my knowledge about clean coding practices in the software development.
Everyone can do the coding but we are not sure whether it’s a quality code or professional code, it become a habit of writing a clean code, programming is not only solving the problem but its telling what is the way we are choosing to solving the problem, is that aspects clean coding is playing one of the major role in the software development.
In my point of view clean code should bring the following points.
- Other developers can understand what we have written.
- Extendibility.
- Avoid duplication.
- ……
- …..
Simple example
The above is doing the addition operation, it should work but it’s not professional coding practices, I have to ask few questions like what is a, b and Do? and so on.
What about this code? Everyone can easily understand what it is doing and so on, so this is one of the ways of writing the clean code.
Always we have to keep in mind the following things while writing the coding.
- Proper naming (class, methods, members, properties and so on).
- Documentation.
- SOLID principles.
- Think about future.
- Write unit tests.
- Refactor if it’s not readable.
- Put the classes in the proper namespace.
- Consistency
- Culture independent.
- Machine independent.
- Easy to configure the system.
- Memory leaking issues.
- Think about performance issues.
- Error handling
Those points are very important and it’s not enough to write the professional code, we need to learn more about best practices and defining rules within the team and develop a code.
Conclusion
Initially it will be hard to follow all the points, but once love this and practice it, it will come naturally and you will become great professional developers.
ALL THE BEST