c# – What is @ in a view and controller for?

Question:

I program in PHP and I'm starting to learn how to program in C# ASP.Net MVC, but I have some doubts:

  1. What is the use of @ (at) both in the controller and in the view ?
  2. What are these calls for { get; set; } that people put in the model ?

    public int id { get; set; } public string name { get; set; }

Answer:

What is the use of @ (at) both in the controller and in the view ?

In View , tells the Razor engine that the following code should be interpreted by .NET. Can be used in a row:

@if (condicao) { ... }

Or it can serve as the opening of a block:

@{
    var minhaVariavel = ...;
}

In theory, the Razor engine interprets any .NET code, but the most commonly used ones are C# and VB.NET.

I don't highly recommend using the second because business logic in the View should be discouraged, as the View is for presentation, not logic. It's important to know that the feature exists, but that it shouldn't be used like in PHP.

What are these calls for { get; set; } that people put in the model ?

These are auto properties . It's a concise way of writing the following:

private int _id;
public int id { 
    get { return _id; }
    set { _id = value; } 
}

As property definition is used a lot, this leaner writing saves programmer effort.

Scroll to Top