C#idiomatic C#
identifier-namescsharp/fundamentals/coding-style

Language

C# identifier naming rules and conventions

Learn the rules for valid identifier names in the C# programming language. In addition, learn the common naming conventions used by the .NET runtime team and the .NET docs team.

  • page csharp/fundamentals/coding-style/identifier-names
  • updated 10/10/2025

An identifier is the name you assign to a type (class, interface, struct, delegate, or enum), member, variable, or namespace.

This article covers the essential rules for valid C# identifiers and the naming conventions used to help you write consistent, professional code.

Naming rules

Valid identifiers must follow these rules. The C# compiler produces an error for any identifier that doesn't follow these rules:

  • Identifiers must start with a letter or underscore (_).
  • Identifiers can contain Unicode letter characters, decimal digit characters, Unicode connecting characters, Unicode combining characters, or Unicode formatting characters. For more information on Unicode categories, see the Unicode Category Database.

You can declare identifiers that match C# keywords by using the @ prefix on the identifier. The @ isn't part of the identifier name. For example, @if declares an identifier named if. These verbatim identifiers are primarily for interoperability with identifiers declared in other languages.

For a complete definition of valid identifiers, see the Identifiers article in the C# Language Specification.

Naming conventions

In addition to the rules, conventions for identifier names are used throughout the .NET APIs. These conventions provide consistency for names, but the compiler doesn't enforce them. You're free to use different conventions in your projects.

By convention, C# programs use PascalCase for type names, namespaces, and all public members. In addition, the dotnet/docs team uses the following conventions, adopted from the .NET Runtime team's coding style:

  • Interface names start with a capital I.

  • Attribute types end with the word Attribute.

  • Enum types use a singular noun for nonflags, and a plural noun for flags.

  • Identifiers shouldn't contain two consecutive underscore (_) characters. Those names are reserved for compiler-generated identifiers.

  • Use meaningful and descriptive names for variables, methods, and classes.

  • Prefer clarity over brevity.

  • Use PascalCase for class names and method names.

  • Use camelCase for method arguments, local variables, and private and internal non-constant fields.

  • Private and internal non-constant instance fields start with an underscore (_).

  • To maintain consistency across all access modifiers, use PascalCase for constant names, both fields and local constants, including private and internal constants.

  • Static fields start with s_. This convention isn't the default Visual Studio behavior, nor part of the Framework design guidelines, but is configurable in editorconfig.

  • Avoid using abbreviations or acronyms in names, except for widely known and accepted abbreviations.

  • Use meaningful and descriptive namespaces that follow the reverse domain name notation.

  • Choose assembly names that represent the primary purpose of the assembly.

  • Avoid using single-letter names, except for simple loop counters. Also, syntax examples that describe the syntax of C# constructs often use the following single-letter names that match the convention used in the C# language specification. Syntax examples are an exception to the rule.

    • Use S for structs, C for classes.
    • Use M for methods.
    • Use v for variables, p for parameters.
    • Use r for ref parameters.

In the following examples, guidance pertaining to elements marked public is also applicable when working with protected and protected internal elements, all of which are intended to be visible to external callers.

Pascal case

Use pascal casing ("PascalCasing") when naming a class, interface, struct, or delegate type.

csharp
public class DataService
{
}
csharp
public record PhysicalAddress(
    string Street,
    string City,
    string StateOrProvince,
    string ZipCode);
csharp
public struct ValueCoordinate
{
}
csharp
public delegate void DelegateType(string message);

When naming an interface, use pascal casing in addition to prefixing the name with an I. This prefix clearly indicates to consumers that it's an interface.

csharp
public interface IWorkerQueue
{
}

When naming public members of types, such as fields, properties, events, use pascal casing. Also, use pascal casing for all methods and local functions.

csharp
public class ExampleEvents
{
    // A public field, these should be used sparingly
    public bool IsValid;

    // An init-only property
    public IWorkerQueue WorkerQueue { get; init; }

    // An event
    public event Action EventProcessing;

    // Method
    public void StartEventProcessing()
    {
        // Local function
        static int CountQueueItems() => WorkerQueue.Count;
        // ...
    }
}

When writing positional records, use pascal casing for parameters as they're the public properties of the record.

csharp
public record PhysicalAddress(
    string Street,
    string City,
    string StateOrProvince,
    string ZipCode);

For more information on positional records, see Positional syntax for property definition.

Camel case

Use camel casing ("camelCasing") when naming private or internal non-constant fields, and prefix them with _. Use camel casing when naming local variables, including instances of a delegate type.

csharp
public class DataService
{
    private IWorkerQueue _workerQueue;
}

When working with static fields that are private or internal, use the s_ prefix and for thread static use t_.

csharp
public class DataService
{
    private static IWorkerQueue s_workerQueue;

    [ThreadStatic]
    private static TimeSpan t_timeSpan;
}

When writing method parameters, use camel casing.

csharp
public T SomeMethod<T>(int someNumber, bool isValid)
{
}

Primary constructor parameters

How you name primary constructor parameters depends on the type being declared:

  • For class and struct types: Use camel casing, consistent with other method parameters.

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/identifier-names/PrimaryConstructorExamples.csClassPrimaryConstructor

csharp
using System;
using Microsoft.Extensions.Logging;

namespace IdentifierNamingExamples;

// <ClassPrimaryConstructor>
public class DataService(IWorkerQueue workerQueue, ILogger logger)
{
    public void ProcessData()
    {
        // Use the parameters directly
        logger.LogInformation("Processing data");
        workerQueue.Enqueue("data");
    }
}
// </ClassPrimaryConstructor>

// <StructPrimaryConstructor>
public struct Point(double x, double y)
{
    public double Distance => Math.Sqrt(x * x + y * y);
}
// </StructPrimaryConstructor>

// <RecordPrimaryConstructor>
public record Person(string FirstName, string LastName);
public record Address(string Street, string City, string PostalCode);
// </RecordPrimaryConstructor>

// Supporting interfaces for examples
public interface IWorkerQueue
{
    void Enqueue(string item);
    int Count { get; }
}

// Add a simple main method to make it an executable
public class Program
{
    public static void Main()
    {
        // Example usage
        var person = new Person("John", "Doe");
        var address = new Address("123 Main St", "Anytown", "12345");
        Console.WriteLine($"{person.FirstName} {person.LastName}");
        Console.WriteLine($"{address.Street}, {address.City} {address.PostalCode}");
    }
}

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/identifier-names/PrimaryConstructorExamples.csStructPrimaryConstructor

csharp
using System;
using Microsoft.Extensions.Logging;

namespace IdentifierNamingExamples;

// <ClassPrimaryConstructor>
public class DataService(IWorkerQueue workerQueue, ILogger logger)
{
    public void ProcessData()
    {
        // Use the parameters directly
        logger.LogInformation("Processing data");
        workerQueue.Enqueue("data");
    }
}
// </ClassPrimaryConstructor>

// <StructPrimaryConstructor>
public struct Point(double x, double y)
{
    public double Distance => Math.Sqrt(x * x + y * y);
}
// </StructPrimaryConstructor>

// <RecordPrimaryConstructor>
public record Person(string FirstName, string LastName);
public record Address(string Street, string City, string PostalCode);
// </RecordPrimaryConstructor>

// Supporting interfaces for examples
public interface IWorkerQueue
{
    void Enqueue(string item);
    int Count { get; }
}

// Add a simple main method to make it an executable
public class Program
{
    public static void Main()
    {
        // Example usage
        var person = new Person("John", "Doe");
        var address = new Address("123 Main St", "Anytown", "12345");
        Console.WriteLine($"{person.FirstName} {person.LastName}");
        Console.WriteLine($"{address.Street}, {address.City} {address.PostalCode}");
    }
}
  • For record types: Use Pascal casing, as the parameters become public properties.

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/identifier-names/PrimaryConstructorExamples.csRecordPrimaryConstructor

csharp
using System;
using Microsoft.Extensions.Logging;

namespace IdentifierNamingExamples;

// <ClassPrimaryConstructor>
public class DataService(IWorkerQueue workerQueue, ILogger logger)
{
    public void ProcessData()
    {
        // Use the parameters directly
        logger.LogInformation("Processing data");
        workerQueue.Enqueue("data");
    }
}
// </ClassPrimaryConstructor>

// <StructPrimaryConstructor>
public struct Point(double x, double y)
{
    public double Distance => Math.Sqrt(x * x + y * y);
}
// </StructPrimaryConstructor>

// <RecordPrimaryConstructor>
public record Person(string FirstName, string LastName);
public record Address(string Street, string City, string PostalCode);
// </RecordPrimaryConstructor>

// Supporting interfaces for examples
public interface IWorkerQueue
{
    void Enqueue(string item);
    int Count { get; }
}

// Add a simple main method to make it an executable
public class Program
{
    public static void Main()
    {
        // Example usage
        var person = new Person("John", "Doe");
        var address = new Address("123 Main St", "Anytown", "12345");
        Console.WriteLine($"{person.FirstName} {person.LastName}");
        Console.WriteLine($"{address.Street}, {address.City} {address.PostalCode}");
    }
}

For more information on primary constructors, see Primary constructors.

For more information on C# naming conventions, see the .NET Runtime team's coding style.

Type parameter naming guidelines

The following guidelines apply to type parameters on generic type parameters. Type parameters are the placeholders for arguments in a generic type or a generic method. You can read more about generic type parameters in the C# programming guide.

  • Do name generic type parameters with descriptive names, unless a single letter name is completely self explanatory and a descriptive name wouldn't add value.

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/coding-conventions/Program.csTypeParametersOne

csharp
using System.Text;

namespace Coding_Conventions_Examples
{
    class Program
    {
        public static void DelegateExamples()
        {
            //<snippet14a>
            Action<string> actionExample1 = x => Console.WriteLine($"x is: {x}");

            Action<string, string> actionExample2 = (x, y) =>
                Console.WriteLine($"x is: {x}, y is {y}");

            Func<string, int> funcExample1 = x => Convert.ToInt32(x);

            Func<int, int, int> funcExample2 = (x, y) => x + y;
            //</snippet14a>

            //<snippet15a>
            actionExample1("string for x");

            actionExample2("string for x", "string for y");

            Console.WriteLine($"The value is {funcExample1("1")}");

            Console.WriteLine($"The sum is {funcExample2(1, 2)}");
            //</snippet15a>
        }
        //<snippet14b>
        public delegate void Del(string message);

        public static void DelMethod(string str)
        {
            Console.WriteLine($"DelMethod argument: {str}");
        }
        //</snippet14b>

        static void Main(string[] args)
        {

            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                PerformanceCounterCategory();
            //</snippet1>

            int startX = 1;
            int endX = 2;
            int previousX = 3;
            //<snippet2>
            if ((startX > endX) && (startX > previousX))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>

            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = [
                new Name { FirstName = "Anderson", LastName = "Redmond" },
                new Name { FirstName = "Jones", LastName = "Seattle" },
                new Name { FirstName = "Anderson", LastName = "Redmond" }
            ];
            int n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
            //</snippet6>

            Console.WriteLine($"{nameList[n].LastName}, {nameList[n].FirstName}");
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();
            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            var message = "This is clearly a string.";
            var currentTemperature = 27;
            //</snippet8>

            //<snippet9>
            int numberOfIterations = Convert.ToInt32(Console.ReadLine());
            int currentMaximum = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            var inputInt = Console.ReadLine();
            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh = "";
            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (char ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13>
            string[] vowels = [ "a", "e", "i", "o", "u" ];
            //</snippet13>

            //<snippet15b>
            Del exampleDel2 = DelMethod;
            exampleDel2("Hey");
            //</snippet15b>
            //<snippet15c>
            Del exampleDel1 = new Del(DelMethod);
            exampleDel1("Hey");
            //</snippet15c>


            // #16 is below Main.
            Console.WriteLine(ComputeDistance(1,2,3,4));

            // 17 requires System.Drawing
            //<snippet17a>
            Font bodyStyle = new Font("Arial", 10.0f);
            try
            {
                byte charset = bodyStyle.GdiCharSet;
            }
            finally
            {
                bodyStyle?.Dispose();
            }
            //</snippet17a>
            //<snippet17b>
            using (Font arial = new Font("Arial", 10.0f))
            {
                byte charset2 = arial.GdiCharSet;
            }
            //</snippet17b>
            //<snippet17c>
            using Font normalStyle = new Font("Arial", 10.0f);
            byte charset3 = normalStyle.GdiCharSet;
            //</snippet17c>

            //<snippet18>
            Console.Write("Enter a dividend: ");
            int dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            int divisor = Convert.ToInt32(Console.ReadLine());

            if ((divisor != 0) && (dividend / divisor) is var result)
            {
                Console.WriteLine($"Quotient: {result}");
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>


            //<snippet19>
            var firstExample = new ExampleClass();
            //</snippet19>
            // Can't show `ExampleClass instance1 = new()` because this project targets net48.

            //<snippet20>
            ExampleClass secondExample = new ExampleClass();
            //</snippet20>

            //<snippet21a>
            var thirdExample = new ExampleClass { Name = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3 };
            //</snippet21a>
            //<snippet21b>
            var fourthExample = new ExampleClass();
            fourthExample.Name = "Desktop";
            fourthExample.ID = 37414;
            fourthExample.Location = "Redmond";
            fourthExample.Age = 2.3;
            //</snippet21b>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an example to Static Members.

            ExampleClass.totalInstances = 1;

            List<Customer> Customers = [ new Customer { Name = "Jones", ID = 432, City = "Redmond" } ];

            // Check shop name to use this.
            List<Distributor> Distributors = [ new Distributor { Name = "ShopSmart", ID = 11302, City = "Redmond" } ];

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in Customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorName = distributor.Name };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in Customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom

            var customerDistributorNames =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in Customers
                from distributor in Distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }

        //<snippet16>
        static double ComputeDistance(double x1, double y1, double x2, double y2)
        {
            try
            {
                return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            }
            catch (System.ArithmeticException ex)
            {
                Console.WriteLine($"Arithmetic overflow or underflow: {ex}");
                throw;
            }
        }
        //</snippet16>
    }

    class Name
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }

    class Distributor
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class Customer
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class ExampleClass
    {
        public static int totalInstances;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? Location { get; set; }
        public double Age { get; set; }

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }

        public static int ResultSoFar() => 0;
    }

    class BaseClass
    {
        protected static int totalInstances;

        static BaseClass() => totalInstances = 0;

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }
    }

    class DerivedClass : BaseClass
    {
    }

    class CompoundFrom
    {
        // The element type of the data source.
        public class Student
        {
            public string? LastName { get; set; }
            public ICollection<int> Scores { get; set; } = default!;
        }

        static void Main()
        {

            // Use a collection initializer to create the data source. Note that
            // each element in the list contains an inner sequence of scores.
            List<Student> students = [
                new Student {LastName="Omelchenko", Scores = [97, 72, 81, 60]},
                new Student {LastName="O'Donnell", Scores = [75, 84, 91, 39]},
                new Student {LastName="Mortensen", Scores = [88, 94, 65, 85]},
                new Student {LastName="Garcia", Scores = [97, 89, 85, 82]},
                new Student {LastName="Beebe", Scores = [35, 72, 91, 70]}
            ];

            //<snippet30>
            var scoreQuery = from student in students
                             from score in student.Scores
                             where score > 90
                             select new { Last = student.LastName, score };
            //</snippet30>

            // <interpolatedStrings>
            // Execute the queries.
            Console.WriteLine("scoreQuery:");
            foreach (var student in scoreQuery)
            {
                Console.WriteLine($"{student.Last} Score: {student.score}");
            }
            // </interpolatedStrings>

            // <rawStringLiterals>
            var message = """
                This is a long message that spans across multiple lines.
                It uses raw string literals. This means we can 
                also include characters like \n and \t without escaping them.
                """;
            // </rawStringLiterals>
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form2 : Form
    {
        //<snippet22>
        public Form2()
        {
            this.Click += (s, e) =>
                {
                    MessageBox.Show(
                        ((MouseEventArgs)e).Location.ToString());
                };
        }
        //</snippet22>

        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form1 : Form
    {
        //<snippet23>
        public Form1()
        {
            this.Click += new EventHandler(Form1_Click);
        }

        void Form1_Click(object? sender, EventArgs e)
        {
            MessageBox.Show(((MouseEventArgs)e).Location.ToString());
        }
        //</snippet23>

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace GenericTypeParameters
{
    public class WrapParameters
    {
        //<TypeParametersOne>
        public interface ISessionChannel<TSession> { /*...*/ }
        public delegate TOutput Converter<TInput, TOutput>(TInput from);
        public class List<T> { /*...*/ }
        //</TypeParametersOne>

        //<TypeParametersTwo>
        public int IComparer<T>() => 0;
        public delegate bool Predicate<T>(T item);
        public struct Nullable<T> where T : struct { /*...*/ }
        //</TypeParametersTwo>

        class wrap
        {
            //<TypeParametersThree>
            public interface ISessionChannel<TSession>
            {
                TSession Session { get; }
            }
            //</TypeParametersThree>
        }
    }//WrapParameters
}

namespace Constructors
{
    // <PrimaryRecord>
    public record Person(string FirstName, string LastName);
    // </PrimaryRecord>

    // <PrimaryClass>
    public class LabelledContainer<T>(string label)
    {
        public string Label { get; } = label;
        public required T Contents 
        { 
            get;
            init;
        }
    }
    // </PrimaryClass>
}
  • Consider using T as the type parameter name for types with one single letter type parameter.

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/coding-conventions/Program.csTypeParametersTwo

csharp
using System.Text;

namespace Coding_Conventions_Examples
{
    class Program
    {
        public static void DelegateExamples()
        {
            //<snippet14a>
            Action<string> actionExample1 = x => Console.WriteLine($"x is: {x}");

            Action<string, string> actionExample2 = (x, y) =>
                Console.WriteLine($"x is: {x}, y is {y}");

            Func<string, int> funcExample1 = x => Convert.ToInt32(x);

            Func<int, int, int> funcExample2 = (x, y) => x + y;
            //</snippet14a>

            //<snippet15a>
            actionExample1("string for x");

            actionExample2("string for x", "string for y");

            Console.WriteLine($"The value is {funcExample1("1")}");

            Console.WriteLine($"The sum is {funcExample2(1, 2)}");
            //</snippet15a>
        }
        //<snippet14b>
        public delegate void Del(string message);

        public static void DelMethod(string str)
        {
            Console.WriteLine($"DelMethod argument: {str}");
        }
        //</snippet14b>

        static void Main(string[] args)
        {

            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                PerformanceCounterCategory();
            //</snippet1>

            int startX = 1;
            int endX = 2;
            int previousX = 3;
            //<snippet2>
            if ((startX > endX) && (startX > previousX))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>

            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = [
                new Name { FirstName = "Anderson", LastName = "Redmond" },
                new Name { FirstName = "Jones", LastName = "Seattle" },
                new Name { FirstName = "Anderson", LastName = "Redmond" }
            ];
            int n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
            //</snippet6>

            Console.WriteLine($"{nameList[n].LastName}, {nameList[n].FirstName}");
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();
            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            var message = "This is clearly a string.";
            var currentTemperature = 27;
            //</snippet8>

            //<snippet9>
            int numberOfIterations = Convert.ToInt32(Console.ReadLine());
            int currentMaximum = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            var inputInt = Console.ReadLine();
            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh = "";
            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (char ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13>
            string[] vowels = [ "a", "e", "i", "o", "u" ];
            //</snippet13>

            //<snippet15b>
            Del exampleDel2 = DelMethod;
            exampleDel2("Hey");
            //</snippet15b>
            //<snippet15c>
            Del exampleDel1 = new Del(DelMethod);
            exampleDel1("Hey");
            //</snippet15c>


            // #16 is below Main.
            Console.WriteLine(ComputeDistance(1,2,3,4));

            // 17 requires System.Drawing
            //<snippet17a>
            Font bodyStyle = new Font("Arial", 10.0f);
            try
            {
                byte charset = bodyStyle.GdiCharSet;
            }
            finally
            {
                bodyStyle?.Dispose();
            }
            //</snippet17a>
            //<snippet17b>
            using (Font arial = new Font("Arial", 10.0f))
            {
                byte charset2 = arial.GdiCharSet;
            }
            //</snippet17b>
            //<snippet17c>
            using Font normalStyle = new Font("Arial", 10.0f);
            byte charset3 = normalStyle.GdiCharSet;
            //</snippet17c>

            //<snippet18>
            Console.Write("Enter a dividend: ");
            int dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            int divisor = Convert.ToInt32(Console.ReadLine());

            if ((divisor != 0) && (dividend / divisor) is var result)
            {
                Console.WriteLine($"Quotient: {result}");
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>


            //<snippet19>
            var firstExample = new ExampleClass();
            //</snippet19>
            // Can't show `ExampleClass instance1 = new()` because this project targets net48.

            //<snippet20>
            ExampleClass secondExample = new ExampleClass();
            //</snippet20>

            //<snippet21a>
            var thirdExample = new ExampleClass { Name = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3 };
            //</snippet21a>
            //<snippet21b>
            var fourthExample = new ExampleClass();
            fourthExample.Name = "Desktop";
            fourthExample.ID = 37414;
            fourthExample.Location = "Redmond";
            fourthExample.Age = 2.3;
            //</snippet21b>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an example to Static Members.

            ExampleClass.totalInstances = 1;

            List<Customer> Customers = [ new Customer { Name = "Jones", ID = 432, City = "Redmond" } ];

            // Check shop name to use this.
            List<Distributor> Distributors = [ new Distributor { Name = "ShopSmart", ID = 11302, City = "Redmond" } ];

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in Customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorName = distributor.Name };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in Customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom

            var customerDistributorNames =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in Customers
                from distributor in Distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }

        //<snippet16>
        static double ComputeDistance(double x1, double y1, double x2, double y2)
        {
            try
            {
                return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            }
            catch (System.ArithmeticException ex)
            {
                Console.WriteLine($"Arithmetic overflow or underflow: {ex}");
                throw;
            }
        }
        //</snippet16>
    }

    class Name
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }

    class Distributor
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class Customer
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class ExampleClass
    {
        public static int totalInstances;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? Location { get; set; }
        public double Age { get; set; }

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }

        public static int ResultSoFar() => 0;
    }

    class BaseClass
    {
        protected static int totalInstances;

        static BaseClass() => totalInstances = 0;

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }
    }

    class DerivedClass : BaseClass
    {
    }

    class CompoundFrom
    {
        // The element type of the data source.
        public class Student
        {
            public string? LastName { get; set; }
            public ICollection<int> Scores { get; set; } = default!;
        }

        static void Main()
        {

            // Use a collection initializer to create the data source. Note that
            // each element in the list contains an inner sequence of scores.
            List<Student> students = [
                new Student {LastName="Omelchenko", Scores = [97, 72, 81, 60]},
                new Student {LastName="O'Donnell", Scores = [75, 84, 91, 39]},
                new Student {LastName="Mortensen", Scores = [88, 94, 65, 85]},
                new Student {LastName="Garcia", Scores = [97, 89, 85, 82]},
                new Student {LastName="Beebe", Scores = [35, 72, 91, 70]}
            ];

            //<snippet30>
            var scoreQuery = from student in students
                             from score in student.Scores
                             where score > 90
                             select new { Last = student.LastName, score };
            //</snippet30>

            // <interpolatedStrings>
            // Execute the queries.
            Console.WriteLine("scoreQuery:");
            foreach (var student in scoreQuery)
            {
                Console.WriteLine($"{student.Last} Score: {student.score}");
            }
            // </interpolatedStrings>

            // <rawStringLiterals>
            var message = """
                This is a long message that spans across multiple lines.
                It uses raw string literals. This means we can 
                also include characters like \n and \t without escaping them.
                """;
            // </rawStringLiterals>
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form2 : Form
    {
        //<snippet22>
        public Form2()
        {
            this.Click += (s, e) =>
                {
                    MessageBox.Show(
                        ((MouseEventArgs)e).Location.ToString());
                };
        }
        //</snippet22>

        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form1 : Form
    {
        //<snippet23>
        public Form1()
        {
            this.Click += new EventHandler(Form1_Click);
        }

        void Form1_Click(object? sender, EventArgs e)
        {
            MessageBox.Show(((MouseEventArgs)e).Location.ToString());
        }
        //</snippet23>

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace GenericTypeParameters
{
    public class WrapParameters
    {
        //<TypeParametersOne>
        public interface ISessionChannel<TSession> { /*...*/ }
        public delegate TOutput Converter<TInput, TOutput>(TInput from);
        public class List<T> { /*...*/ }
        //</TypeParametersOne>

        //<TypeParametersTwo>
        public int IComparer<T>() => 0;
        public delegate bool Predicate<T>(T item);
        public struct Nullable<T> where T : struct { /*...*/ }
        //</TypeParametersTwo>

        class wrap
        {
            //<TypeParametersThree>
            public interface ISessionChannel<TSession>
            {
                TSession Session { get; }
            }
            //</TypeParametersThree>
        }
    }//WrapParameters
}

namespace Constructors
{
    // <PrimaryRecord>
    public record Person(string FirstName, string LastName);
    // </PrimaryRecord>

    // <PrimaryClass>
    public class LabelledContainer<T>(string label)
    {
        public string Label { get; } = label;
        public required T Contents 
        { 
            get;
            init;
        }
    }
    // </PrimaryClass>
}
  • Do prefix descriptive type parameter names with "T".

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/coding-conventions/Program.csTypeParametersThree

csharp
using System.Text;

namespace Coding_Conventions_Examples
{
    class Program
    {
        public static void DelegateExamples()
        {
            //<snippet14a>
            Action<string> actionExample1 = x => Console.WriteLine($"x is: {x}");

            Action<string, string> actionExample2 = (x, y) =>
                Console.WriteLine($"x is: {x}, y is {y}");

            Func<string, int> funcExample1 = x => Convert.ToInt32(x);

            Func<int, int, int> funcExample2 = (x, y) => x + y;
            //</snippet14a>

            //<snippet15a>
            actionExample1("string for x");

            actionExample2("string for x", "string for y");

            Console.WriteLine($"The value is {funcExample1("1")}");

            Console.WriteLine($"The sum is {funcExample2(1, 2)}");
            //</snippet15a>
        }
        //<snippet14b>
        public delegate void Del(string message);

        public static void DelMethod(string str)
        {
            Console.WriteLine($"DelMethod argument: {str}");
        }
        //</snippet14b>

        static void Main(string[] args)
        {

            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                PerformanceCounterCategory();
            //</snippet1>

            int startX = 1;
            int endX = 2;
            int previousX = 3;
            //<snippet2>
            if ((startX > endX) && (startX > previousX))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>

            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = [
                new Name { FirstName = "Anderson", LastName = "Redmond" },
                new Name { FirstName = "Jones", LastName = "Seattle" },
                new Name { FirstName = "Anderson", LastName = "Redmond" }
            ];
            int n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
            //</snippet6>

            Console.WriteLine($"{nameList[n].LastName}, {nameList[n].FirstName}");
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();
            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            var message = "This is clearly a string.";
            var currentTemperature = 27;
            //</snippet8>

            //<snippet9>
            int numberOfIterations = Convert.ToInt32(Console.ReadLine());
            int currentMaximum = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            var inputInt = Console.ReadLine();
            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh = "";
            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (char ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13>
            string[] vowels = [ "a", "e", "i", "o", "u" ];
            //</snippet13>

            //<snippet15b>
            Del exampleDel2 = DelMethod;
            exampleDel2("Hey");
            //</snippet15b>
            //<snippet15c>
            Del exampleDel1 = new Del(DelMethod);
            exampleDel1("Hey");
            //</snippet15c>


            // #16 is below Main.
            Console.WriteLine(ComputeDistance(1,2,3,4));

            // 17 requires System.Drawing
            //<snippet17a>
            Font bodyStyle = new Font("Arial", 10.0f);
            try
            {
                byte charset = bodyStyle.GdiCharSet;
            }
            finally
            {
                bodyStyle?.Dispose();
            }
            //</snippet17a>
            //<snippet17b>
            using (Font arial = new Font("Arial", 10.0f))
            {
                byte charset2 = arial.GdiCharSet;
            }
            //</snippet17b>
            //<snippet17c>
            using Font normalStyle = new Font("Arial", 10.0f);
            byte charset3 = normalStyle.GdiCharSet;
            //</snippet17c>

            //<snippet18>
            Console.Write("Enter a dividend: ");
            int dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            int divisor = Convert.ToInt32(Console.ReadLine());

            if ((divisor != 0) && (dividend / divisor) is var result)
            {
                Console.WriteLine($"Quotient: {result}");
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>


            //<snippet19>
            var firstExample = new ExampleClass();
            //</snippet19>
            // Can't show `ExampleClass instance1 = new()` because this project targets net48.

            //<snippet20>
            ExampleClass secondExample = new ExampleClass();
            //</snippet20>

            //<snippet21a>
            var thirdExample = new ExampleClass { Name = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3 };
            //</snippet21a>
            //<snippet21b>
            var fourthExample = new ExampleClass();
            fourthExample.Name = "Desktop";
            fourthExample.ID = 37414;
            fourthExample.Location = "Redmond";
            fourthExample.Age = 2.3;
            //</snippet21b>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an example to Static Members.

            ExampleClass.totalInstances = 1;

            List<Customer> Customers = [ new Customer { Name = "Jones", ID = 432, City = "Redmond" } ];

            // Check shop name to use this.
            List<Distributor> Distributors = [ new Distributor { Name = "ShopSmart", ID = 11302, City = "Redmond" } ];

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in Customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorName = distributor.Name };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in Customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom

            var customerDistributorNames =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in Customers
                from distributor in Distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }

        //<snippet16>
        static double ComputeDistance(double x1, double y1, double x2, double y2)
        {
            try
            {
                return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            }
            catch (System.ArithmeticException ex)
            {
                Console.WriteLine($"Arithmetic overflow or underflow: {ex}");
                throw;
            }
        }
        //</snippet16>
    }

    class Name
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }

    class Distributor
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class Customer
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class ExampleClass
    {
        public static int totalInstances;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? Location { get; set; }
        public double Age { get; set; }

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }

        public static int ResultSoFar() => 0;
    }

    class BaseClass
    {
        protected static int totalInstances;

        static BaseClass() => totalInstances = 0;

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }
    }

    class DerivedClass : BaseClass
    {
    }

    class CompoundFrom
    {
        // The element type of the data source.
        public class Student
        {
            public string? LastName { get; set; }
            public ICollection<int> Scores { get; set; } = default!;
        }

        static void Main()
        {

            // Use a collection initializer to create the data source. Note that
            // each element in the list contains an inner sequence of scores.
            List<Student> students = [
                new Student {LastName="Omelchenko", Scores = [97, 72, 81, 60]},
                new Student {LastName="O'Donnell", Scores = [75, 84, 91, 39]},
                new Student {LastName="Mortensen", Scores = [88, 94, 65, 85]},
                new Student {LastName="Garcia", Scores = [97, 89, 85, 82]},
                new Student {LastName="Beebe", Scores = [35, 72, 91, 70]}
            ];

            //<snippet30>
            var scoreQuery = from student in students
                             from score in student.Scores
                             where score > 90
                             select new { Last = student.LastName, score };
            //</snippet30>

            // <interpolatedStrings>
            // Execute the queries.
            Console.WriteLine("scoreQuery:");
            foreach (var student in scoreQuery)
            {
                Console.WriteLine($"{student.Last} Score: {student.score}");
            }
            // </interpolatedStrings>

            // <rawStringLiterals>
            var message = """
                This is a long message that spans across multiple lines.
                It uses raw string literals. This means we can 
                also include characters like \n and \t without escaping them.
                """;
            // </rawStringLiterals>
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form2 : Form
    {
        //<snippet22>
        public Form2()
        {
            this.Click += (s, e) =>
                {
                    MessageBox.Show(
                        ((MouseEventArgs)e).Location.ToString());
                };
        }
        //</snippet22>

        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form1 : Form
    {
        //<snippet23>
        public Form1()
        {
            this.Click += new EventHandler(Form1_Click);
        }

        void Form1_Click(object? sender, EventArgs e)
        {
            MessageBox.Show(((MouseEventArgs)e).Location.ToString());
        }
        //</snippet23>

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace GenericTypeParameters
{
    public class WrapParameters
    {
        //<TypeParametersOne>
        public interface ISessionChannel<TSession> { /*...*/ }
        public delegate TOutput Converter<TInput, TOutput>(TInput from);
        public class List<T> { /*...*/ }
        //</TypeParametersOne>

        //<TypeParametersTwo>
        public int IComparer<T>() => 0;
        public delegate bool Predicate<T>(T item);
        public struct Nullable<T> where T : struct { /*...*/ }
        //</TypeParametersTwo>

        class wrap
        {
            //<TypeParametersThree>
            public interface ISessionChannel<TSession>
            {
                TSession Session { get; }
            }
            //</TypeParametersThree>
        }
    }//WrapParameters
}

namespace Constructors
{
    // <PrimaryRecord>
    public record Person(string FirstName, string LastName);
    // </PrimaryRecord>

    // <PrimaryClass>
    public class LabelledContainer<T>(string label)
    {
        public string Label { get; } = label;
        public required T Contents 
        { 
            get;
            init;
        }
    }
    // </PrimaryClass>
}
  • Consider indicating constraints placed on a type parameter in the name of parameter. For example, a parameter constrained to ISession might be called TSession.

The code analysis rule CA1715 can be used to ensure that type parameters are named appropriately.

Extra naming conventions

  • Examples that don't include using directives, use namespace qualifications. If you know that a namespace is imported by default in a project, you don't have to fully qualify the names from that namespace. Qualified names can be broken after a dot (.) if they're too long for a single line, as shown in the following example.

sample · csharpdocs/csharp/fundamentals/coding-style/snippets/coding-conventions/program.csSnippet1

csharp
using System.Text;

namespace Coding_Conventions_Examples
{
    class Program
    {
        public static void DelegateExamples()
        {
            //<snippet14a>
            Action<string> actionExample1 = x => Console.WriteLine($"x is: {x}");

            Action<string, string> actionExample2 = (x, y) =>
                Console.WriteLine($"x is: {x}, y is {y}");

            Func<string, int> funcExample1 = x => Convert.ToInt32(x);

            Func<int, int, int> funcExample2 = (x, y) => x + y;
            //</snippet14a>

            //<snippet15a>
            actionExample1("string for x");

            actionExample2("string for x", "string for y");

            Console.WriteLine($"The value is {funcExample1("1")}");

            Console.WriteLine($"The sum is {funcExample2(1, 2)}");
            //</snippet15a>
        }
        //<snippet14b>
        public delegate void Del(string message);

        public static void DelMethod(string str)
        {
            Console.WriteLine($"DelMethod argument: {str}");
        }
        //</snippet14b>

        static void Main(string[] args)
        {

            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                PerformanceCounterCategory();
            //</snippet1>

            int startX = 1;
            int endX = 2;
            int previousX = 3;
            //<snippet2>
            if ((startX > endX) && (startX > previousX))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>

            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = [
                new Name { FirstName = "Anderson", LastName = "Redmond" },
                new Name { FirstName = "Jones", LastName = "Seattle" },
                new Name { FirstName = "Anderson", LastName = "Redmond" }
            ];
            int n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
            //</snippet6>

            Console.WriteLine($"{nameList[n].LastName}, {nameList[n].FirstName}");
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();
            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            var message = "This is clearly a string.";
            var currentTemperature = 27;
            //</snippet8>

            //<snippet9>
            int numberOfIterations = Convert.ToInt32(Console.ReadLine());
            int currentMaximum = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            var inputInt = Console.ReadLine();
            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh = "";
            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (char ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13>
            string[] vowels = [ "a", "e", "i", "o", "u" ];
            //</snippet13>

            //<snippet15b>
            Del exampleDel2 = DelMethod;
            exampleDel2("Hey");
            //</snippet15b>
            //<snippet15c>
            Del exampleDel1 = new Del(DelMethod);
            exampleDel1("Hey");
            //</snippet15c>


            // #16 is below Main.
            Console.WriteLine(ComputeDistance(1,2,3,4));

            // 17 requires System.Drawing
            //<snippet17a>
            Font bodyStyle = new Font("Arial", 10.0f);
            try
            {
                byte charset = bodyStyle.GdiCharSet;
            }
            finally
            {
                bodyStyle?.Dispose();
            }
            //</snippet17a>
            //<snippet17b>
            using (Font arial = new Font("Arial", 10.0f))
            {
                byte charset2 = arial.GdiCharSet;
            }
            //</snippet17b>
            //<snippet17c>
            using Font normalStyle = new Font("Arial", 10.0f);
            byte charset3 = normalStyle.GdiCharSet;
            //</snippet17c>

            //<snippet18>
            Console.Write("Enter a dividend: ");
            int dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            int divisor = Convert.ToInt32(Console.ReadLine());

            if ((divisor != 0) && (dividend / divisor) is var result)
            {
                Console.WriteLine($"Quotient: {result}");
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>


            //<snippet19>
            var firstExample = new ExampleClass();
            //</snippet19>
            // Can't show `ExampleClass instance1 = new()` because this project targets net48.

            //<snippet20>
            ExampleClass secondExample = new ExampleClass();
            //</snippet20>

            //<snippet21a>
            var thirdExample = new ExampleClass { Name = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3 };
            //</snippet21a>
            //<snippet21b>
            var fourthExample = new ExampleClass();
            fourthExample.Name = "Desktop";
            fourthExample.ID = 37414;
            fourthExample.Location = "Redmond";
            fourthExample.Age = 2.3;
            //</snippet21b>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an example to Static Members.

            ExampleClass.totalInstances = 1;

            List<Customer> Customers = [ new Customer { Name = "Jones", ID = 432, City = "Redmond" } ];

            // Check shop name to use this.
            List<Distributor> Distributors = [ new Distributor { Name = "ShopSmart", ID = 11302, City = "Redmond" } ];

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in Customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorName = distributor.Name };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in Customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom

            var customerDistributorNames =
                from customer in Customers
                join distributor in Distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in Customers
                from distributor in Distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }

        //<snippet16>
        static double ComputeDistance(double x1, double y1, double x2, double y2)
        {
            try
            {
                return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            }
            catch (System.ArithmeticException ex)
            {
                Console.WriteLine($"Arithmetic overflow or underflow: {ex}");
                throw;
            }
        }
        //</snippet16>
    }

    class Name
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }

    class Distributor
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class Customer
    {
        public static int total;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? City { get; set; }
    }

    class ExampleClass
    {
        public static int totalInstances;
        public string? Name { get; set; }
        public int ID { get; set; }
        public string? Location { get; set; }
        public double Age { get; set; }

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }

        public static int ResultSoFar() => 0;
    }

    class BaseClass
    {
        protected static int totalInstances;

        static BaseClass() => totalInstances = 0;

        public static int IncrementTotal()
        {
            totalInstances += 1;
            return totalInstances;
        }
    }

    class DerivedClass : BaseClass
    {
    }

    class CompoundFrom
    {
        // The element type of the data source.
        public class Student
        {
            public string? LastName { get; set; }
            public ICollection<int> Scores { get; set; } = default!;
        }

        static void Main()
        {

            // Use a collection initializer to create the data source. Note that
            // each element in the list contains an inner sequence of scores.
            List<Student> students = [
                new Student {LastName="Omelchenko", Scores = [97, 72, 81, 60]},
                new Student {LastName="O'Donnell", Scores = [75, 84, 91, 39]},
                new Student {LastName="Mortensen", Scores = [88, 94, 65, 85]},
                new Student {LastName="Garcia", Scores = [97, 89, 85, 82]},
                new Student {LastName="Beebe", Scores = [35, 72, 91, 70]}
            ];

            //<snippet30>
            var scoreQuery = from student in students
                             from score in student.Scores
                             where score > 90
                             select new { Last = student.LastName, score };
            //</snippet30>

            // <interpolatedStrings>
            // Execute the queries.
            Console.WriteLine("scoreQuery:");
            foreach (var student in scoreQuery)
            {
                Console.WriteLine($"{student.Last} Score: {student.score}");
            }
            // </interpolatedStrings>

            // <rawStringLiterals>
            var message = """
                This is a long message that spans across multiple lines.
                It uses raw string literals. This means we can 
                also include characters like \n and \t without escaping them.
                """;
            // </rawStringLiterals>
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form2 : Form
    {
        //<snippet22>
        public Form2()
        {
            this.Click += (s, e) =>
                {
                    MessageBox.Show(
                        ((MouseEventArgs)e).Location.ToString());
                };
        }
        //</snippet22>

        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace Coding_Conventions_WF2
{
    public partial class Form1 : Form
    {
        //<snippet23>
        public Form1()
        {
            this.Click += new EventHandler(Form1_Click);
        }

        void Form1_Click(object? sender, EventArgs e)
        {
            MessageBox.Show(((MouseEventArgs)e).Location.ToString());
        }
        //</snippet23>

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace GenericTypeParameters
{
    public class WrapParameters
    {
        //<TypeParametersOne>
        public interface ISessionChannel<TSession> { /*...*/ }
        public delegate TOutput Converter<TInput, TOutput>(TInput from);
        public class List<T> { /*...*/ }
        //</TypeParametersOne>

        //<TypeParametersTwo>
        public int IComparer<T>() => 0;
        public delegate bool Predicate<T>(T item);
        public struct Nullable<T> where T : struct { /*...*/ }
        //</TypeParametersTwo>

        class wrap
        {
            //<TypeParametersThree>
            public interface ISessionChannel<TSession>
            {
                TSession Session { get; }
            }
            //</TypeParametersThree>
        }
    }//WrapParameters
}

namespace Constructors
{
    // <PrimaryRecord>
    public record Person(string FirstName, string LastName);
    // </PrimaryRecord>

    // <PrimaryClass>
    public class LabelledContainer<T>(string label)
    {
        public string Label { get; } = label;
        public required T Contents 
        { 
            get;
            init;
        }
    }
    // </PrimaryClass>
}
  • You don't have to change the names of objects that were created by using the Visual Studio designer tools to make them fit other guidelines.