C# Explicitly and Implicitly Type

Explicitly typed variables are declared with a specific data type, such as intstring, or double

For example:

int x = 10; // x is explicitly typed as an int

string name = "Alice"; // name is explicitly typed as a string

double pi = 3.14; // pi is explicitly typed as a double

Implicitly typed variables are declared with the var keyword, which lets the compiler infer the data type from the assigned value. 

For example:

var x = 10; // x is implicitly typed as an int
var name = "Alice"; // name is implicitly typed as a string
var pi = 3.14; // pi is implicitly typed as a double

The advantage of using implicitly typed variables is that you can write less code and avoid specifying redundant or obvious type information. However, you should only use var when the data type is clear from the context, and when the variable has a specific and narrow scope. Otherwise, using explicitly typed variables can improve the readability and maintainability of your code.

Comments

Popular posts from this blog

C# | Association, Aggregation and Composition

Throw vs Throw ex in C#

String[] args in C#