Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:37:44+00:00 2026-05-16T03:37:44+00:00

We use the .NET 2.0 framework with C# 3.0 (I think it’s the last

  • 0

We use the .NET 2.0 framework with C# 3.0 (I think it’s the last version of C# which can run on the 2.0 version of the framework, correct me if I am wrong).

Is there something built into C# which can make this type of parameter validation more convenient?

public ConnectionSettings(string url, string username, string password,
                          bool checkPermissions)
{
    if (username == null) {
        throw new ArgumentNullException("username");
    }

    if (password == null) {
        throw new ArgumentNullException("password");
    }

    if (String.IsNullOrEmpty(url)) {
        throw new ArgumentException("Must not be null or empty, it was " +
            (url == null ? url : "empty"), "url");
    }

    this.url = url;
    this.username = username;
    this.password = password;
    this.checkPermissions = checkPermissions;
}

That sort of parameter validation becomes a common pattern and results in a lot of “almost boilerplate” code to wade through in our public methods.

If there is nothing built in. Are there any great free libraries which we could use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:37:45+00:00Added an answer on May 16, 2026 at 3:37 am

    I normally create static helper methods…

    E.g.

    public static void CheckNotNull(object value, string parameterName)
    {
       if(value == null) { throw new ArgumentNullException(parameterName); }
    }
    

    Means you can condense your code down to something similar to below and just makes it a little tidier.

    CheckNotNull(username, "username");
    CheckNotNull(password, "password"); 
    

    Or you can wrap it up as an extension method:

    public static void CheckNotNull<T>(this T value, string parameterName)
    {
       if(value == null) { throw new ArgumentNullException(parameterName); }
    }
    

    And use like:

    username.CheckNotNull("username");
    password.CheckNotNull("password");
    

    And if you’re feeling really fancy, you could probably interrogate the parameter names by using reflection. Reflection’s kinda slow, so you’d only do this if you were going to throw an exception, but it saves you typing the literal parameter name all the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Full .NET framework you can use the Color.FromArgb() method to create a
Ive recently been asked to recommend a .NET framework version to use in a
Can I use two different back ends for an .net Entity Framework project? I
In the .net framework, there's a generic IEnumerable<T> interface which inherits from the not-generic
I am just beginning the move to the .NET Framework 3.5. I use CruiseControl.NET
I have a .NET Framework 2.0 app and would like to use the Windows
I wonder if is possible to use FTS with LINQ using .NET Framework 3.5.
I would like to use the ASP.NET Membership Provider security framework but would like
How can I use .NET DataSet.Select method to search records that match a DateTime?
In .net frameworks 1.1, I use System.Configuration.ConfigurationSettings.AppSettings[name]; for application settings. But in .Net 2.0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.