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

C# Constants

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.

  • Set up the environment

Create a new C# Console Application project in Visual Studio.

  • Defining Constants

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;
  • Using Constants

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 in Classes

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}");
  • Rules for Constants

There are a few rules to keep in mind when working with constants in C#:

  • Constants must be assigned a value at the time of declaration.
  • Constants cannot be changed after they have been assigned a value.
  • Constants can only be of the following data types: bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong, ushort, and string.
  • Constants are implicitly 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.

  1. C# const keyword example:

    • Description: The const keyword in C# is used to declare constants, which are values that cannot be changed after they are assigned.
    • Code:
      const int MaxValue = 100;
      
  2. Defining constants in C#:

    • Description: Highlighting how to define and use constants in C# for values that remain constant throughout the program.
    • Code:
      const double Pi = 3.14;
      double area = Pi * radius * radius;
      
  3. Constants in C# classes and structs:

    • Description: Demonstrating how to define constants within classes and structs in C#.
    • Code:
      public class MathConstants
      {
          public const double Pi = 3.14;
      }
      
  4. Global constants in C#:

    • Description: Showing how to create global constants accessible across multiple files using the internal access modifier.
    • Code:
      // Constants.cs
      internal class Constants
      {
          internal const string AppName = "MyApp";
      }
      
      // Program.cs
      Console.WriteLine(Constants.AppName);
      
  5. Constants in C# interfaces:

    • Description: Discussing the usage of constants within C# interfaces to define values shared by implementing classes.
    • Code:
      public interface IConstants
      {
          const int MaxAttempts = 3;
      }
      
  6. Compile-time constants in C#:

    • Description: Introducing compile-time constants, which must be initialized with a compile-time constant expression.
    • Code:
      const int DaysInWeek = 7; // Compile-time constant
      
  7. C# enum with constants:

    • Description: Demonstrating how to use enums as a way to group related constants together.
    • Code:
      public enum DaysOfWeek
      {
          Monday,
          Tuesday,
          Wednesday,
          Thursday,
          Friday,
          Saturday,
          Sunday
      }