Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the [Parameter] Attribute in c#

Tags:

c#

blazor

I was watching a tutorial in Blazor. Then I came across this code and I can't seem to find it in the Internet or I think I'm not using the right terms for searching atleast.

@code{
[Parameter]
    public IList<Todo> Todo {get; set;}
}

Is it only exclusive in blazor or it is available in c#. Kindly give some references. Thanks in advance.

like image 345
Errol Paleracio Avatar asked Oct 23 '19 07:10

Errol Paleracio


People also ask

What is the use of __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What are attributes in C programming?

Attributes are a mechanism by which the developer can attach extra information to language entities with a generalized syntax, instead of introducing new syntactic constructs or keywords for each feature.

Where is __ attribute __ defined?

The __attribute__ is added just after the variable name, and though it can appear unwieldy, it's a style you can get used to: int main(int argc __attribute__((unused)), char **argv) { ... Additional uses shown, each with a comment showing the compiler warning it might have generated.

What is attribute in embedded C?

Master C and Embedded C Programming- Learn as you go Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.


1 Answers

This is explained in Create and use ASP.NET Core Razor components, specifically in the Component Parameters section.

[Parameter] is used to mark the component parameters that can be set when the component is used in another page. Borrowing from the doc example this component doesn't have any parameters :

<div class="panel panel-default">
    <div class="panel-heading">@Title</div>
    <div class="panel-body">@ChildContent</div>

    <button class="btn btn-primary" @onclick="OnClick">
        Trigger a Parent component method
    </button>
</div>

@code {
    public string Title { get; set; }

    public RenderFragment ChildContent { get; set; }

    public EventCallback<MouseEventArgs> OnClick { get; set; }
}

Without the [Parameter] attribute, those are just public properties that can't be set from other pages. The following line would be invalid :

<ChildComponent Title="Panel Title from Parent" />

While this :

<div class="panel panel-default">
    <div class="panel-heading">@Title</div>
    <div class="panel-body">@ChildContent</div>

    <button class="btn btn-primary" @onclick="OnClick">
        Trigger a Parent component method
    </button>
</div>

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }
}

Allows us to set the parameters whenever we use that component :

<ChildComponent Title="Panel Title from Parent"
                OnClick="@ShowMessage">
    Content of the child component is supplied
    by the parent component.
</ChildComponent>
like image 127
Panagiotis Kanavos Avatar answered Oct 16 '22 07:10

Panagiotis Kanavos