Saturday, 28 May 2016

type inference

C# is strongly typed and default; data type declaration is an explicit type.
C# 3.0 introduced “var” keyword to declare implicit types. Purpose of var keyword is used to store any type in a variable. Once implicit type declare, compiler know the type of the variable after you cannot assign any other data type to that variable.

Syntax:

var str = "Sample";
Now if we assign integer value to name variable as like below:

str = 33;
It gives an error message: “Cannot implicitly convert type 'int' to 'string'”.

No comments:

Post a Comment