C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll cover the basics of constants in C#. A constant is a value that cannot be changed after it has been assigned. Constants are useful when you need to define a fixed value that should not be modified throughout the lifetime of your program.
Create a new C# Console Application project in Visual Studio.
To define a constant in C#, use the const
keyword followed by the data type, identifier, and value. The value of a constant must be assigned at the time of declaration.
Here's an example of defining a constant:
const double Pi = 3.14159265359;
Constants can be used in expressions, calculations, or as arguments to methods, just like regular variables.
Here's an example of using a constant in a calculation:
double radius = 5; double area = Pi * radius * radius; Console.WriteLine($"The area of a circle with radius {radius} is {area}");
Constants can be defined within classes or structs. They are implicitly static, which means you can access them using the class name without creating an instance of the class.
Here's an example of defining a constant within a class:
public class MathConstants { public const double Pi = 3.14159265359; }
To use the constant, you can access it using the class name:
double radius = 5; double area = MathConstants.Pi * radius * radius; Console.WriteLine($"The area of a circle with radius {radius} is {area}");
There are a few rules to keep in mind when working with constants in C#:
bool
, byte
, char
, decimal
, double
, float
, int
, long
, sbyte
, short
, uint
, ulong
, ushort
, and string
.static
, which means they can be accessed without creating an instance of the class or struct they are defined in.In this tutorial, we've covered the basics of constants in C#. Constants are useful for defining fixed values that should not change during the execution of your program, and they can be used in calculations, expressions, or as arguments to methods. By using constants, you can make your code more readable and maintainable.
C# const keyword example:
const
keyword in C# is used to declare constants, which are values that cannot be changed after they are assigned.const int MaxValue = 100;
Defining constants in C#:
const double Pi = 3.14; double area = Pi * radius * radius;
Constants in C# classes and structs:
public class MathConstants { public const double Pi = 3.14; }
Global constants in C#:
internal
access modifier.// Constants.cs internal class Constants { internal const string AppName = "MyApp"; } // Program.cs Console.WriteLine(Constants.AppName);
Constants in C# interfaces:
public interface IConstants { const int MaxAttempts = 3; }
Compile-time constants in C#:
const int DaysInWeek = 7; // Compile-time constant
C# enum with constants:
public enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }