user1920206
user1920206

Reputation: 69

What does get do in c#?

I took this code from elsewhere in order to show and hide forms effectively. Whilst I understand most of it, I don't understand what get does in this context and in general. can this be explained? WinForms

public class FormProvider
{
    public static Form1 frm1
    {
        get
        {
            if (_frm1 == null)
            {
                _frm1 = new Form1();
            }
            return _frm1;
        }
    }

Upvotes: 0

Views: 1119

Answers (4)

Tigran
Tigran

Reputation: 62246

It's nothing else other than syntax wrapper over automatically generated function

public static Form1 get_frm1()
{       
   return _frm1;       
}

So every time you reference FormProvider.frm1, it's like you are calling FormProvider.get_frm1()

In fact if you try to code something like this:

public class A {

    public string Name {get;}

    public string get_Name() {
       return "James Bond";
    }
}

it will give compile-time error, as there is a collision of definitions.

Upvotes: 5

Parimal Raj
Parimal Raj

Reputation: 20575

its is used as getter method for the property

From MSDN


Properties combine aspects of both fields and methods. To the user of an object, a property appears to be a field, accessing the property requires the same syntax. To the implementer of a class, a property is one or two code blocks, representing a get accessor and/or a set accessor. The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property that has both accessors is read-write.

Unlike fields, properties are not classified as variables. Therefore, you cannot pass a property as a ref (C# Reference) or out (C# Reference) parameter.


The set Accessor

The set accessor resembles a method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property. In the following example, a set accessor is added to the Value property:

**When you assign a value to the property, the set accessor is invoked by using an argument that provides the new value**

consider this class for example

class DemoClass
{
    private int value;
    public Int32 Value
    {
        get 
        { 
            return value;
        }
        set
        {
            this.value = value;
        }
    }
}

if look at the compiled assembly in the reflector u will notice like

.class private auto ansi beforefieldinit DemoClass
    extends [mscorlib]System.Object
{
    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
    {
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: call instance void [mscorlib]System.Object::.ctor()
        L_0006: ret 
    }


    .property instance int32 Value
    {
        .get instance int32 ConsoleApplication1.DemoClass::get_Value()
        .set instance void ConsoleApplication1.DemoClass::set_Value(int32)
    }


    .field private int32 value

}

Upvotes: 0

Fede
Fede

Reputation: 44038

Those are called Properties in C#

Properties are a way to encapsulate fields in such a way that they can be accessed with these so called "Getter" and "Setter" methods, that could contain any logic needed.

In this case, the "Getter" will return an existing instance of your Form1 (if there is one) or create a new instance if none is found, assign it to the _form1 field and return that.

I suggest you research on the link above for further understanding.

Upvotes: 1

Khan
Khan

Reputation: 18142

frm1 is a property.

Properties generally have a getter get and an optional setter set.

These are used to provide special implementation when accessing your Property

In this case, if the property's backer _frm1 (which is a field) is null, the getter is responsible for setting it to a new instance of Form1.

Upvotes: 0

Related Questions