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

  • SEARCH
  • Home
  • 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 3392336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:54:32+00:00 2026-05-18T03:54:32+00:00

I am doing the Nerd Dinner tutorial for ASP.NET MVC and I met one

  • 0

I am doing the Nerd Dinner tutorial for ASP.NET MVC and I met one construction in the C# language that seemed very strange. The title of this question is a bit vague, because I have trouble defining what this is. This also made it difficult for me to search about the topic, hence I decide to ask a question about it.

In the Nerd Dinner tutorial I see the following code fragment:

public static class ControllerHelpers {

    public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {

        foreach (RuleViolation issue in errors) {
            modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
        }
    }
}

And later they show:

//
// GET: /Dinners/Edit/2

public ActionResult Edit(int id) {

    Dinner dinner = dinnerRepository.GetDinner(id);

    return View(dinner);
}

//
// POST: /Dinners/Edit/2

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {

    Dinner dinner = dinnerRepository.GetDinner(id);

    try {

        UpdateModel(dinner);

        dinnerRepository.Save();

        return RedirectToAction("Details", new { id=dinner.DinnerID });
    }
    catch {

        ModelState.AddRuleViolations(dinner.GetRuleViolations());

        return View(dinner);
    }
}

The parts that puzzle me are:

public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) 

and

ModelState.AddRuleViolations(dinner.GetRuleViolations());

It looks like you define the AddRuleViolations function in the ControllerHelpers class and then invoke it as if it was an instance function of the ModelState property. Is this observation correct? If yes, why would you need this? It looks very strange to me to define a method in one class as if it was a method of another class.

Note: ModelState is a property of the current class and not a class it self.

  • 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-18T03:54:32+00:00Added an answer on May 18, 2026 at 3:54 am

    It’s because it’s an extension method. That’s what the “this” bit is at the start of the first parameter.

    The idea of extension methods is that they allow you to effectively add functionality to existing classes. So if you have:

    public static class StringExtensions
    {
        public static string Reverse(this string text)
        {
            char[] chars = text.ToCharArray();
            Array.Reverse(chars);
            return new string(chars);
        }
    }
    

    then you can call it like this:

    string x = "hello world";
    string y = x.Reverse();
    

    that actually gets compiled as if you’d written:

    string x = "hello world";
    string y = StringExtensions.Reverse(x);
    

    Extension methods have to be declared in top-level non-generic static classes. They’re heavily used in LINQ.

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

Sidebar

Related Questions

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this
Serious n00b warning here; please take mercy! So I finished the Nerd Dinner MVC
I am building my first few projects with ASP.NET MVC, and I have a
When doing TDD , how to tell that's enough tests for this class /
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)
I've seen this on nerd dinner and other sites. On page load (in JavaScript,
How are people doing their validations on data? Basically I have a requirement that
Doing a very simple loop to display various data, for _test in @test ...
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
When doing an INSERT with a lot of data, ie: INSERT INTO table (mediumtext_field)

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.