简体   繁体   中英

What does this code do in c# and what is it's purpose?

 public class A {
    public Par mParams;
    public Par Parameters {
        get { return mParams; }
        set { mParams = value; }
    }
}

I am new to c#

What is public Par Parameters ? This seems neither a class or a function. Confused here.

Think of it like a public Par getParameters() and public void setX(Par p) method in Java. So, it is closest to a "function" but it is actually called Property. You can use it like this:

A myObject = new A();
a.Parameters = new Par(...);

This is a property which is backed by a public field, in this case, it is somewhat redundant, mParms should be declared as protected or private .

I recommend that you review this MSDN Programming Guide on Properties. It explains quite well, how they work and what they're used for.

The block of code from Public Par Parameters is a Property

I suspect the line public Par mParams; should actually be private. Its meant to be the underlying variable storing the value of the property.

Its worth pointing out that you do not explicitly need mParams any more in C#. You can define an automatic property, where the compiler creates its own underlying private variable using:

Public Par Parameters { get; set; }

public Par Parameters is a property, used to set or get the value of mParams.

It's shorthand for accessors and mutators. In another example:

private int i = 0;
public int myNumber {
    get { return i; }
    set { i = value; }
}

Allows you to change the variable i . Like so in code:

className.myNumber = 20;
// className.i is now 20

Parameters is a Property of type Par . It has an access modifier ( public ), which means it is accessible from anywhere in your code.

Your example is a little redundant, because the mParams field is actually publicly accessible, and the property that exposes it doesn't do anything apart from returning and setting the field. However, you could potentially add extra code in each of the "accessors" (the get and set bits). For example to do validation, notify something that a property has been changed, etc.

You use properties in much the same way as fields:

A foo = new A();

// Calls the "get" accessor of the Parameters property
if (foo.Parameters == null)
{
    // Calls the "set" accessor of the Parameters property
    foo.Parameters = new Par();
}

It is considered a best practice to not allow direct access to member fields (variables) from outside a class. In a typical scenario, the field should therefore be private (or sometimes protected):

private Par mParams;

public Par Parameters
{
    get { return mParams; }
    set { mParams = value; }
}

There are a few slightly different syntaxes you will want to learn about as well. First, there is the auto-implemented property :

public Par Parameters
{
    get;
    set;
}

For auto-implemented properties, the C# compiler generates a backing field for you automatically. This saves you from writing some code if our property getter and setter don't need to contain any logic.

You can also use properties to restrict access in ways you cannot achieve with fields:

public Par Parameters
{
    get;
    private set;
}

Notice the set "accessor" has it's own access modifier. The result is a property that is publicly readable, but only the class itself is allowed to set it. This is similar to:

private Par mParams;

public Par Parameters
{
    get { return mParams; }
}

Which does effectively the same thing, but without an auto-implemented property.

In languages that do not support properties, all this would be achieved by creating two methods : GetParameters and SetParameters . In fact, the C# compiler generates some very similarly named methods in the background.

It's a property , which works very much like a pair of methods (functions) whose only purpose is to give you access to the mParams field. As a bit of syntactic sugar , C# gives you the ability to assign and read to it much as you would a variable. Think of it as two methods, getParameters and setParameters , which you can't call directly , but which are invoked when you access Parameters .

Parameters = new Par(); //Works as though you had run: setParameters(new Par());
Par x = Parameters;     //Works as though you had run: Par x = getParameters();

This allows you to use the shorter, more convenient and expressive variable-like syntax, but what you're actually doing is running two "invisible" methods created by the compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM