How to Use Primary Constructors for Compact Classes in C#

Primary constructors, introduced in C# 12, offer a more concise way to define class parameters and initialize fields.

This feature reduces boilerplate code and makes classes more readable.

Traditional Approach vs Primary Constructor

Before primary constructors, you would likely write something like the following:

public class UserService
{
    private readonly ILogger _logger;
    private readonly IUserRepository _repository;

    public UserService(ILogger logger, IUserRepository repository)
    {
        _logger = logger;
        _repository = repository;
    }

    public async Task<User> GetUserById(int id)
    {
        _logger.LogInformation("Fetching user {Id}", id);
        return await _repository.GetByIdAsync(id);
    }
}

With primary constructors, this becomes:

public class UserService(ILogger logger, IUserRepository repository)
{
    public async Task<User> GetUserById(int id)
    {
        logger.LogInformation("Fetching user {Id}", id);
        return await repository.GetByIdAsync(id);
    }
}

Key Benefits

  1. Reduced Boilerplate: No need to declare private fields and write constructor assignments
  2. Parameters Available Throughout: Constructor parameters are accessible in all instance methods
  3. Immutability by Default: Parameters are effectively readonly without explicit declaration

Real-World Example

Here's a practical example using primary constructors with dependency injection:

public class OrderProcessor(
    IOrderRepository orderRepo,
    IPaymentService paymentService,
    ILogger<OrderProcessor> logger)
{
    public async Task<OrderResult> ProcessOrder(Order order)
    {
        try
        {
            logger.LogInformation("Processing order {OrderId}", order.Id);
            
            var paymentResult = await paymentService.ProcessPayment(order.Payment);
            if (!paymentResult.Success)
            {
                return new OrderResult(false, "Payment failed");
            }

            await orderRepo.SaveOrder(order);
            return new OrderResult(true, "Order processed successfully");
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Failed to process order {OrderId}", order.Id);
            throw;
        }
    }
}

Tips and Best Practices

  1. Use primary constructors when the class primarily needs dependencies for its methods
  2. Combine with records for immutable data types:
public record Customer(string Name, string Email)
{
    public string FormattedEmail => $"{Name} <{Email}>";
}
  1. Consider traditional constructors for complex initialization logic

Primary constructors provide a cleaner, more maintainable way to write C# classes, especially when working with dependency injection and simple data objects.

0
68

Related

Reading a file line by line is useful when handling large files without loading everything into memory at once.

✅ Best Practice: Use File.ReadLines() which is more memory efficient.

Example

foreach (string line in File.ReadLines("file.txt"))
{
    Console.WriteLine(line);
}

Why use ReadLines()?

Reads one line at a time, reducing overall memory usage. Ideal for large files (e.g., logs, CSVs).

Alternative: Use StreamReader (More Control)

For scenarios where you need custom processing while reading the contents of the file:

using (StreamReader reader = new StreamReader("file.txt"))
{
    string? line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

Why use StreamReader?

Lets you handle exceptions, encoding, and buffering. Supports custom processing (e.g., search for a keyword while reading).

When to Use ReadAllLines()? If you need all lines at once, use:

string[] lines = File.ReadAllLines("file.txt");

Caution: Loads the entire file into memory—avoid for large files!

3
243

When working with relational databases, JOIN operations allow you to retrieve data from multiple tables based on a common column.

SQL Server supports different types of joins, each serving a specific purpose. Let’s break them down with examples.

1. INNER JOIN

The INNER JOIN returns only the rows where there is a match in both tables.

SELECT A.id, A.name, B.order_id
FROM Customers A
INNER JOIN Orders B ON A.id = B.customer_id;
  • If a customer has no matching order, they won’t appear in the result.

2. LEFT JOIN (or LEFT OUTER JOIN)

The LEFT JOIN returns all rows from the left table (Customers), and only matching rows from the right table (Orders). If there’s no match, NULL values are returned for the right table columns.

SELECT A.id, A.name, B.order_id
FROM Customers A
LEFT JOIN Orders B ON A.id = B.customer_id;
  • Customers without orders will still appear, but order_id will be NULL.

3. RIGHT JOIN (or RIGHT OUTER JOIN)

The RIGHT JOIN works the opposite of LEFT JOIN, returning all rows from the right table (Orders) and only matching rows from the left table (Customers).

SELECT A.id, A.name, B.order_id
FROM Customers A
RIGHT JOIN Orders B ON A.id = B.customer_id;
  • Orders without a matching customer will still appear, but name will be NULL.

4. FULL JOIN (or FULL OUTER JOIN)

The FULL JOIN returns all records from both tables. If there’s no match, NULL values will be shown in the missing columns.

SELECT A.id, A.name, B.order_id
FROM Customers A
FULL JOIN Orders B ON A.id = B.customer_id;
  • This ensures that all customers and all orders appear in the results, even if there’s no match.

Quick Summary:

Join Type Includes Matching Rows Includes Non-Matching Rows (Left Table) Includes Non-Matching Rows (Right Table)
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN

Understanding these joins can help you extract data efficiently and ensure that your queries return the expected results. Happy querying!

0
59

When working with URLs in C#, encoding is essential to ensure that special characters (like spaces, ?, &, and =) don’t break the URL structure. The recommended way to encode a string for a URL is by using Uri.EscapeDataString(), which converts unsafe characters into their percent-encoded equivalents.

string rawText = "hello world!";
string encodedText = Uri.EscapeDataString(rawText);

Console.WriteLine(encodedText); // Output: hello%20world%21

This method encodes spaces as %20, making it ideal for query parameters.

For ASP.NET applications, you can also use HttpUtility.UrlEncode() (from System.Web), which encodes spaces as +:

using System.Web;

string encodedText = HttpUtility.UrlEncode("hello world!");
Console.WriteLine(encodedText); // Output: hello+world%21

For .NET Core and later, Uri.EscapeDataString() is the preferred choice.

26
416