DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Optional Parameters in C# 4

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 17, 10 · Interview
Like (0)
Save
Tweet
Share
5.69K Views

Join the DZone community and get the full member experience.

Join For Free
When working with C#, I frequently take advantage of method overloading where I have multiple methods with the same name, but varying parameters.  Sometimes I need to be able to pass completely different data types and sometimes I just do it to allow for default options.  More often than not, it's the latter reason.  Probably the biggest area I do this is when building out data access layers.  Using my standard Person & Address data model, let's assume I have a method for GetPeople that returns a list of people.  I would then have a second method that accepts a boolean on whether or not I wanted to pre-load the address data.  The parameterless method would simply call the method with the boolean parameter with a default value:
public List<Person> GetPeople()
{
GetPeople(false);
}

public List<Person> GetPeople(bool fetchAddresses)
{
// query the database and return a list of people
}
In cases where I know I only want the person data, I can call GetPeople either without a parameter or by passing in "false".  Where I do want the address data also, I have to call GetPeople and pass in "true".  If you can imagine with a more complex example, it is easy to have several methods that do nothing but call a larger method passing in the defaults. In most cases only one method will actually have any real logic.

C# 4 has a feature to help with this called "Optional Parameters" that allows you to set the default value of a parameter, thus allowing you to call the method with a shorter set of parameters.  Any parameter not specified in the call, is then set to the default.  This would allow me to build one method:
public List<Person> GetPeople(bool fetchAddresses = false)
{
// query the database and return a list of people
}
I can now call this method either with or without a parameter since I am specifying a default.  If I call it with a parameter, it will use the data I pass, otherwise fetchAddresses will be false.  While this is cool, it may not fit every purpose and I have seen several arguments on whether or not this is a good idea.  You will have to evaluate the use of the classes.  If it is something that will be internal to a project, then this can help save a lot of code.  If you are writing an API, I would recommend sticking to the old style to make the API more maintainable.  This is specific to .NET 4 so it obviously wouldn't be available with previous versions of .NET.

Optional Parameters can definitely simplify cases where you might need to use method overloads, just make sure it is the right solution based on who is consuming your classes.
csharp

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Spring Boot, Quarkus, or Micronaut?
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Tracking Software Architecture Decisions

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: