Title here
Summary here
Our first program will demonstrate constants of character, string, boolean, and numeric values.
using System;
public class Program
{
const string s = "constant";
public static void Main()
{
Console.WriteLine(s);
// A `const` statement can appear anywhere a `var` statement can.
const int n = 500000000;
// Constant expressions perform arithmetic with arbitrary precision.
const double d = 3e20 / n;
Console.WriteLine(d);
// A number can be given a type by using it in a context that requires one,
// such as a variable assignment or a function call.
Console.WriteLine((long)d);
// Example: math.Sin expects a `double`.
Console.WriteLine(Math.Sin(n));
}
}
To run the program, save the code into a Program.cs
file and use the csc
(C# compiler) to compile it, then run the resulting executable file.
$ csc Program.cs
$ Program.exe
constant
6E+11
600000000000
-0.28470407323754404
Constants are a fundamental part of many languages, allowing for compile-time constants and optimization. Having now understood constants in C#, let’s proceed to explore other language features and constructs.