Keywords in C#: Types of Keywords with Examples

Learn what keywords are in C#, the different categories of keywords, and see practical examples of how each type is used in real code.

What are Keywords?

Keywords are reserved words in C# that have special meaning to the compiler and cannot be used as identifiers, such as variable or method names, because the language relies on them to define its structure and behavior.

Value Type Keywords

Keywords like int, float, double, char, bool, and decimal define value type variables, each representing a specific kind of data with a fixed size and range.

Modifier Keywords

Access modifiers such as public, private, protected, and internal control the visibility of classes, methods, and variables, while modifiers like static, sealed, and abstract define how members behave.

Control Flow Keywords

Keywords such as if, else, switch, for, while, foreach, break, and continue direct the flow of execution in a program, allowing you to build logic that responds to different conditions.

Contextual Keywords

Some keywords, like var, async, await, and yield, are contextual, meaning they only carry special meaning in specific contexts and can otherwise be used as regular identifiers elsewhere in the code.

Conclusion: Understanding the different categories of C# keywords helps you read and write code more fluently, since these reserved words form the essential vocabulary of the entire language.

Keywords Working Together in One Program

Reading a small program and naming the role of each keyword is a good way to make the categories stick:

using System;

namespace Shop
{
    public sealed class Order
    {
        private const decimal TaxRate = 0.18m;
        private readonly string id;

        public Order(string id) { this.id = id; }

        public decimal Total(params decimal[] prices)
        {
            decimal sum = 0;
            foreach (decimal p in prices)
            {
                if (p < 0) throw new ArgumentException("Price cannot be negative");
                sum += p;
            }
            return sum + sum * TaxRate;
        }
    }

    internal static class Program
    {
        static void Main()
        {
            var order = new Order("A-17");
            try
            {
                Console.WriteLine(order.Total(100m, 250m));   // 413.00
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Here namespace, class and using organise code; public, private and internal control access; sealed, static, const and readonly change behaviour; foreach, if and return control flow; and try, catch, throw and new handle errors and objects.

Other Keyword Groups Worth Knowing

GroupExamplesPurpose
Reference typesclass, interface, delegate, object, stringDeclare or name reference types
Parameter modifiersref, out, in, paramsControl how arguments are passed
Exception handlingtry, catch, finally, throwDetect and respond to errors
Operatorsis, as, typeof, sizeof, checkedTest types, convert and check arithmetic
Literalstrue, false, nullFixed values
Newer contextual wordsrecord, init, required, whenRecords, init-only properties, required members, filters

Some keywords have more than one job. using imports a namespace at the top of a file, but inside a method it also creates a scope that disposes an object, such as a file stream, when the block ends. new creates objects and can also hide an inherited member.

Using a Keyword as a Name With @

Reserved keywords cannot be identifiers, but C# lets you prefix one with @ to use it as a name anyway:

int @class = 3;            // a variable called "class"
Console.WriteLine(@class); // 3

This is mostly useful when working with code generated from other languages or with JSON fields named like keywords. In your own code, choosing a different name is clearer.

Frequent Confusions

  • var is not dynamic. The type is fixed at compile time from the value on the right. dynamic is the keyword that defers type checking to runtime.
  • const versus readonly. A const must be known at compile time; a readonly field can be set in the constructor.
  • string versus String. The keyword is simply an alias for System.String; both mean the same type.
  • ref versus out. A ref argument must be assigned before the call; an out parameter must be assigned inside the method.

Tip: most editors colour keywords differently, so if a variable name suddenly changes colour, you have probably typed a reserved word.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.