List of All New Features in C# 6.0: Part 2

Hi guys. I am back with two new features of C# 6.0 in Visual Studio 2015 Preview. Before reading this article, I highly recommend reading the previous part:

visual studio

This article explainis parameterless constructors in structs and how it is now easier to create a custom exception class by shortcuts in Visual Studio 2015 and we have many new features in C# 6.0 that have already been posted in my last article. For more you can go through with that.

List of All New Features in C# 6.0
  1. Parameterless constructors in structs.
  2. Creating a custom exceptions class.
Parameterless constructors in Structs

Parameterless constructors in structs in C# 6.0 is now possible, we can create a parameterless constructor in a struct explicitly in Visual Studio 2015. Let's see a property of structs for C# 5.0 and prior to understand structs first.

Structs Overview

Structs have the following properties:
  • Structs are value types whereas classes are reference types.

  • Unlike classes, structs can be instantiated without using a new operator.

  • Structs can declare constructors, but they must take parameters until C# 5.0.

  • A struct cannot inherit from another struct or class and it cannot be the base of a class. All structs inherit directly from System.ValueType, that inherits from System.Object.

  • A struct can implement interfaces.

We use structs for small code, or in other words we can say we can write a structs definition anywhere where we have a class only for a few properties or methods so thereis no need to use the memory in the heap, we can use stack memory by using structs and the object of structs can call the default constructor. That means that with parameterless constructors there is no need to use a new keyword because until C# 5.0 we are unable to create a parameterless constructor in structs like Int32 is a struct and we can use that without using the new keyword but in the case of a class we must use new so that we can create a memory block in the heap and at the time of new we must call the constructor of the class without a parameter or with a parameter explicitly.

Structs have a default constructor (parameterless constructor) and there is no need to call that with the new keyword because structs are value types so we can call their constructors implicitly. Let's have an example in C# 5.0.

I just wrote a struct of Student Type to store the records and also tryied to write a default or parameterless constructor but at compile time it will generate an error.

parameterless constructor

I just wote a struct of Student Type to store the records and also attempted to write a default or parameterless constructor but at compile time it will generate an error.

error

Structs can't contain explicit Parameterless Constructors because the object of a structs type is a Value Type stored in stack memory. That's why there is no need to call a parameterless constructor with the new keyword explicitly. So let's see, when I write a constructor with parameters it's again returning an error. Let's see that.

class

console error

In that example code that you have already see above we have still 2 errors because we don't assign the default value for all those Data Members. Those are declared in my struct so it's necessary to assign all the data members in my constructor of structs and then my source code will be built successfully.

build program

Code

  1. public struct Stu  
  2. {  
  3.     public Stu(int Rollno,string Name)  
  4.     {  
  5.         this.rollno = Rollno;  
  6.         this.name = Name;  
  7.     }  
  8.     int rollno;  
  9.     public int Rollno  
  10.     {  
  11.         get { return rollno; }  
  12.         set { rollno = value; }  
  13.     }  
  14.     string name;  
  15.     public string Name  
  16.     {  
  17.         get { return name; }  
  18.         set { name = value; }  
  19.     }  
  20. }  
So I think it's enough to understand how to use a parameter constructor in C# 5.0 of structs. Let's see how to use all the properties of structs by the object of structs without using the new keyword because it's able to call their parameterless constructor that is declared by the compiler implicitly we can't write that manually in C# 5.0.

use the object of struct

See the output. When we are using a property by an object without assigning the object with the new keyword we call the parameterless constructor.

Output

output

Now we need to assign the object with the new keyword so it shows 2 constructors but a struct has only 1 explicitly and 1 by default created by the compiler. See the image below.
compiler
So you can use any of them, if we are not assigned the object the parameterless constructor will be called implicitly.

In C# 6.0

In C# 6.0 with Visual Studio 2015 we are able to create a parameterless constructor in structs too. In previous versions of C# we are not able to create a parameterless constructor so all the value type data members must be assigned with their default values by the parameterless constructor created by the compiler. For example if we have a variable of Int32 Type then to that will be assigned 0 by default but now you can assign any default value to every data member of your structs like a class but still we have a condition such as if we are going to create a parameterless constructor in my struct so every member must be assigned in to that constructor but that condition is not applicable for class types so now enjoy the new behaviour of structs with a parameterless constructor. Let's see the example:

Struct class

That code gets Build successful because we assigned all data members of the struct but if we assigned only one and one or more and the rest remains to be assigned then that will generate an error as in the following:

stu

So assign all the data members in your default or parameterless constructor and use that without the new keyword.

Creating custom exceptions Class

Creating a Custom Exception class is a new benefit of Visual Studio 2015. In Visual Studio 2012 or in any Visual Studio if we need to handle my exception by a catch block by a custom exception class so we need to write a class file first and then we must inharet a System.Exception class and for using the Message property to be overiden and difine the custom Exception Message as in the following:

exceptions
And we can use that class as an Exception class by the throw keyword and handle in a catch block directly in a System.Exception type object.

main function

But in Visual Studio 2015 it's too easy to create a custom exception class

In Visual Studio 2015 it's too easy to create that custom exception class. There is no need to create that class manually, we need to use the throw keyword and create an object by the new keyword.

throw new Myexception

When you click then it will show you all the maximum options.

Myexception

And also it shows the preview of that code that can be written by Visual Studio.

preview of that code

Now just press Enter and that will create a serializable Exception Class in a new file like the following:

serializable

Now override any property in the System.Exception class and use them as a normal Exception class.
I hope you enjoy this article.

Thanks.

Up Next
    Ebook Download
    View all
    Learn
    View all