Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Overview of C# 6.0 Language Improvements

Tech 1

Inline Property Initializers and Read-Only Auto-Properties

Properties can now be initialized directly at their declaration, eliminating the need for a constructor. Additionally, getter-only auto-properties can be assigned during initialization.

public class Employee
{
    public int Id { get; set; } = 0;
    public string FullName { get; set; } = "Unknown";
    public DateTime HireDate { get; } = DateTime.Now;
}

String Interpolation

Prepend a string with $ to embed expressions directly in side curly braces. Format specifiers and alignment are supported.

var emp = new Employee { Id = 42, FullName = "Alice" };
string line1 = $"Employee: {emp.FullName}, ID: {emp.Id:D5}";
string line2 = $"Hired on {emp.HireDate:yyyy-MM-dd}";
Console.WriteLine($"{line1}\n{line2}");

Expression-Bodied Members

Methods and read-only properties consisting of a single expression can be written using the => lambda‑style syntax.

public class Person
{
    public DateTime BirthDate { get; set; }

    // Expression-bodied property (read‑only)
    public int Age => DateTime.Today.Year - BirthDate.Year;

    // Expression-bodied method
    public string GetDisplayName(string firstName, string lastName) => $"{lastName}, {firstName}";
}

Indexer‑Style Collection Initialization

Dictionary and other indexable collections can be populated using an indexer syntax within the initializer block, improving readability.

var departments = new Dictionary<int, string>
{
    [1] = "Engineering",
    [2] = "Marketing",
    [3] = "Finance"
};

Static Using Directives

Import a static class with using static and call its methods without the class qualifier, reducing repetitive code.

using static System.Console;

public class Logging
{
    public static void WriteMessage(string text)
    {
        WriteLine($"[INFO] {text}");
    }
    public static int MaxValue(int x, int y) => Math.Max(x, y); // also using Math
}

The nameof Operator

nameof obtains the string name of a variable, type, or member at compile time, avoiding magic strings that break during refactoring.

public class Validator
{
    public static void EnsureNotNegative(int quantity)
    {
        if (quantity < 0)
            throw new ArgumentOutOfRangeException(
                nameof(quantity),
                $"Value must be non‑negative but was {quantity}.");
    }
}

Null‑Conditional Operators

The ?. and ?[] operators return null when the left-hand side is null, avoiding NullReferenceException. Combined with the null‑coalescing operator ??, a fallback value can be provided.

string[] items = { "one", null, "three" };

foreach (var item in items)
{
    int length1 = item?.Length ?? 0;   //0 for null entries
    Console.WriteLine(length1);
}

// Chaining with properties
Employee manager = null;
string name = manager?.FullName ?? "No manager";

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.