DEV Community

Cover image for Introduction to C# 9 - iFour Technolab
Harshal Suthar
Harshal Suthar

Posted on • Originally published at ifourtechnolab.com

Introduction to C# 9 - iFour Technolab

C# is a strongly typed and object-orientated programming language. It is developed by Microsoft and it runs on the .Net Framework. It comes from the C family, which mostly looks like other most popular languages like Java and C++. The first version of C# is Released by Microsoft in 2001 and now C#’s latest version is C#9.

Using C#, we can do so many things like can build Mobile applications, Desktop applications, Web applications, Web services, Web sites, games, VR, Database applications.

After all main question is why we use c#? First of all, it is an object-oriented programming language which is gives you more clear structure to programs and you can reuse the code. It cost low for developing, as you see it is close to java and C++ so for programmers have easy to switch other languages also it is easy to use and also easy to learn.

New Features in C# 9

C#9 introduce us new feature that is about record. It is a reference type that provides some methods to provide value semantics for equality. In the new version of C#, we have Top-level statements using which we can remove unnecessary code from so many applications. Also, using these statements you can replace all boilerplate with the using statement.

C#9 have some new things for pattern matching and enhanced pattern matching compare to the older version. In the new version with new performance and interop these new features are improved support for native interop and low-level libraries which requires high performance: native sized integers, function pointers. Some of the new features described in detail below:

Init Only Setters

It provides syntax to initialize members of an object. For more clearance which value is setting in which property it’s made by property initializers. Here is one disadvantage of it is it’s properties need to be settable, in C#9 replaced “set accessors” with “initaccessors” for properties and indexers. Property initializer syntax is used by callers to set these values in creation completed. Change state window is provided by init only setters. Change state window is closed when the phase is ending and also constrictor phase is closed after all initialization is complete with property initializes and with expressions.

Some records represent immutable data that is copied, other than updated over time. It is grate to built-in data type immutable but now the problem is how we instantiate object? For that init-only property came.

The init-only is set o nly when the object is initialized and its syntax is just written “init” word add as modifier we add.

Read More: List Of Debugging Tools For C-sharp .Net Development

Now see how can you declare init, before it you need to declare it only setters in any type you write. Below is small code snippet to understand easily.

public structStudentAdmition
{
    public DateTimeAdmitionDate{ get; init; }
    public decimal ScoreInPr{ get; init; }
    public decimal ScoreInPersentage{ get; init; }

    public override string ToString() =>
        $"Ad={ AdmitionDatett} on {AdmitionDate:M/d/yyyy}: " +
}

Enter fullscreen mode Exit fullscreen mode

Callers have used property initializer syntax for set values when we still preserving the immutability:

Below is showing a small part of the code example to understand in an easy way.

var now = new StudentAdmition
{ 
     AdmitionDate= DateTime.Now, 
     ScoreInPr= 78.7,
     ScoreInPersentage = 68.0 
};

Enter fullscreen mode Exit fullscreen mode

By changing an observation after initialization is shown an error for assigning to an init-only property outside of initialization:


// Error! CS8852.
now.ScoreInPr =  78.7

Enter fullscreen mode Exit fullscreen mode

This Init only setters are useful for set base class properties from derived classes. It also set derived properties using helpers in the base class. You can declare init-only setters for any struct and class which you want.

Init-only is a powerful feature of c#9. It allows you to create immutable properties without creating a constructor that takes the initial property values. You can also set the init-only property with an object initializer, and after that, you can't change itas they are immutable. It is very useful if you plan to work with immutable data in C#.

Fit and Finish Features

Many of the other features in C# helps you write code more efficiently. In C# 9, you can miss out on the type in a “new”?expression when the pre-created object's type is already declared. The most common use of it is in field declarations:

public Liststudents = new();

Enter fullscreen mode Exit fullscreen mode

The“new” is also used when you need to create a new object of any class or also use it to pass arguments in methods. Assume Studentsof() method with the below type:

public StudentModelStudentsof(string branchName, intbranchId)

Enter fullscreen mode Exit fullscreen mode

You can also call it from this below way also.

varstudentOf= Students.StudentsOf(“B.Tech”, new());

Enter fullscreen mode Exit fullscreen mode

One more beautiful feature of this is you can combine this with “init-only” for initializing a new object.

StudentsBranchs Branch= new() { Branch= "B.Tech., CE" };

Enter fullscreen mode Exit fullscreen mode

You can also return the instance of class and method by default constructor by using a return new(); statement.

Planning to Hire C# Web Development Company? Your Search ends here.

In C#9 you can use the “static” access modifier in lambda expressions and any anonymous methods. Static modifier with lambda expressions are “Static lambda expressions”which are comparable to the static local functions. A static anonymous method and lambda expressions are can’t capture instance state and local variables. It prevents co-accidentally from taking other variables.

In C#9 have also a new feature for “foreach loop”, the foreach loop will automatically be recognized and also use an extension method “GetEnumerator” otherwise satisfies the ForEach condition. With this change you can add Foreach support to any type. Just you have a on limit when enumerating and object make sense in your personal project.

As you know, in some order to be able to initiate on a collection in older C# with the foreach loop the collection is must need to expose a public method “GetEnumerator()”actually it doesn’t exist. In C#9 it is allowed to create an extension method of IEnumerator and IAsyncEnumerator interface for foreach loop.

Conclusion

We hope after reading this blog you will get an idea about what is C# and its latest version and how many features new released by Microsoft in this latest version. You will also know about some features in a basic way from new features. C#9 have so many new things and features to make coding more efficient and easier. C#9 also has a more enhanced pattern comparing to the older version.

Top comments (0)