Variables in C#
In C#, variables are explicitly declared and used by the compiler to check type-correctness of method calls.
To run the program, save it as Variables.cs
and use the dotnet
command:
In C#, variables are strongly typed, but the var
keyword allows for type inference, similar to Go’s :=
syntax. However, var
in C# must be used with an initializer, and once the type is inferred, it cannot be changed.
C# also has the concept of default values for variables. When a variable is declared but not initialized, it’s given a default value based on its type. For value types like int
, the default is 0, while for reference types, it’s null
.
Unlike Go, C# doesn’t have a built-in fmt
package. Instead, it uses Console.WriteLine()
for standard output. For more complex string formatting, C# offers string interpolation (as shown in the b
and c
example) and composite formatting.
C# is typically used within the .NET framework, which provides a rich set of libraries and tools for building various types of applications.