populating a string array

  • Thread starter Thread starter Sangeetha
  • Start date Start date
S

Sangeetha

I came across this array initialisation which I can't follow

----------------------------------------------
public static string[] AllOperators
{

get

{

return m_AllOps;

}

}





m_allOps is a private member variable of the class and is declared as

static string[] m_AllOps = { "+", "-", "*", "/" };

-------------------------------------------



Can anyone help me understand it!!



Thanks

Sangeetha
 
What is so strange about it?
m_AllOps is a private array. It is being initialized as in standard C:

char* array[] = { "abc","def",ghi"};

and it is a property so that you can make it read-only (see there's no set
method for it).

Nothing really strange right?

If you still do not understand then do some research on Properties ......
This is a good place to start:
http://www.softsteel.co.uk/tutorials/cSharp/cIndex.html


I hope I was helpfull!

Fábio
 
The doubt I have is in the declaration of the array AllOperators. I never
declared the array using a "get" keyword as under:

public static string[] AllOperators { get { return m_AllOps;} }

--

Fábio G Ribeiro said:
What is so strange about it?
m_AllOps is a private array. It is being initialized as in standard C:

char* array[] = { "abc","def",ghi"};

and it is a property so that you can make it read-only (see there's no set
method for it).

Nothing really strange right?

If you still do not understand then do some research on Properties ......
This is a good place to start:
http://www.softsteel.co.uk/tutorials/cSharp/cIndex.html


I hope I was helpfull!

Fábio


Sangeetha said:
I came across this array initialisation which I can't follow

----------------------------------------------
public static string[] AllOperators
{

get

{

return m_AllOps;

}

}





m_allOps is a private member variable of the class and is declared as

static string[] m_AllOps = { "+", "-", "*", "/" };

-------------------------------------------



Can anyone help me understand it!!



Thanks

Sangeetha
 
...
The doubt I have is in the declaration of
the array AllOperators. I never declared
the array using a "get" keyword as under:

I think you misunderstood Fabio.

The array as such is declared and initialized with:

static string[] m_AllOps = { "+", "-", "*", "/" };

AllOperator is not an array, but a "Property".

A Property is a .NET-construction that in most cases is used to declare an
accessor (get) and/or a mutator (set) to a private field.

In this case it's an example of only an accessor to the array m_AllOps, as
the Property only has a "get"-part.

public static string[] AllOperators
{
get { return m_AllOps; }
}

In that way you have made sure that the array m_AllOps can be read "from
outside", but it cannot be exchanged for another array, as there's no
mutator (set).

However, there's still the possibility to change the *contents* of the array
m_AllOps, but that's another question...

// Bjorn A
 
thanks for the reply..now is c what Fabio was trying to explain....
Bjorn Abelli said:
...
The doubt I have is in the declaration of
the array AllOperators. I never declared
the array using a "get" keyword as under:

I think you misunderstood Fabio.

The array as such is declared and initialized with:

static string[] m_AllOps = { "+", "-", "*", "/" };

AllOperator is not an array, but a "Property".

A Property is a .NET-construction that in most cases is used to declare an
accessor (get) and/or a mutator (set) to a private field.

In this case it's an example of only an accessor to the array m_AllOps, as
the Property only has a "get"-part.

public static string[] AllOperators
{
get { return m_AllOps; }
}

In that way you have made sure that the array m_AllOps can be read "from
outside", but it cannot be exchanged for another array, as there's no
mutator (set).

However, there's still the possibility to change the *contents* of the array
m_AllOps, but that's another question...

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top