nepalcargoservices.com

# Embracing Primary Constructors in C# 12: A Modern Approach

Written on

Chapter 1: Introduction to Primary Constructors

In C# 12, the introduction of primary constructors marks a significant advancement in how we define the behavior of classes and structs. This feature enables developers to attach parameters directly to the struct or class declaration, streamlining the object creation process.

This innovative syntax replaces older methods, allowing for more expressive and concise code.

Section 1.1: Key Use Cases

  1. Base Constructor Invocation: The parameters specified in the primary constructor can be directly passed to the base class constructor, facilitating smooth initialization of inherited properties.
  2. Member Field Initialization: These parameters are frequently utilized to set up member fields or properties, allowing for direct value assignment during the construction phase.

Subsection 1.1.1: Older Syntax Example

Before the advent of C# 12, the syntax for struct and constructor declaration was more verbose. Here’s an example of the previous style:

public readonly struct Distance

{

public readonly double Magnitude { get; }

public readonly double Direction { get; }

public Distance(double dx, double dy)

{

Magnitude = Math.Sqrt(dx * dx + dy * dy);

Direction = Math.Atan2(dy, dx);

}

}

Section 1.2: Adopting the New Syntax

With the new syntax, you can create a struct named Distance that incorporates primary constructor parameters dx and dy. This allows us to compute two read-only properties—Magnitude and Direction—right from the constructor:

public readonly struct Distance(double dx, double dy)

{

public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy);

public readonly double Direction { get; } = Math.Atan2(dy, dx);

}

Chapter 2: Leveraging Primary Constructors in Inheritance

Primary constructors also enhance the way we handle inheritance in C#. A derived class can call the primary constructor of its base class using the base keyword.

Here’s an example featuring a BankAccount class:

public class BankAccount(int accountNumber, decimal initialBalance)

{

// Additional initialization specific to the BankAccount

// ...

}

In this case, the SavingsAccount class derives from BankAccount, invoking its primary constructor as follows:

public class SavingsAccount(int accountNumber, decimal initialBalance) : BankAccount(accountNumber, initialBalance)

{

// Additional initialization specific to SavingsAccount

// ...

}

Section 2.1: Customizing Behavior in Derived Classes

Derived classes can further tailor their functionality in constructors. For instance, a CheckingAccount class might extend BankAccount while invoking the base constructor:

public class CheckingAccount : BankAccount

{

public CheckingAccount(int accountNumber, decimal initialBalance)
: base(accountNumber, initialBalance)

{

// Additional initialization specific to CheckingAccount

// ...

}

}

Conclusion

As we navigate the dynamic world of programming languages, C# 12 introduces the powerful concept of primary constructors. This feature allows us to define class and struct behavior with enhanced clarity and efficiency.

Let’s move forward, leaving behind outdated syntax, and embrace this modern method of object creation.

Thank you for joining the C# community! If you’ve enjoyed this content, please show your support by clapping and following the author! 👏️️

Follow us on: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr

Explore more content on our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Connecting Neon with Drizzle in Next.js: A User-Friendly Guide

Discover how to seamlessly connect Neon with Drizzle in Next.js through this detailed tutorial.

Innovative Clean Synthetic Fuel Development for a Sustainable Future

Scientists have created a clean synthetic fuel from renewable energy, capturing CO2 and aiming for net-zero emissions.

Mastering Literature Reviews: AI Techniques for Success

Discover how to enhance your literature review writing using AI tools and strategies for an effective academic process.