Array property syntax in C# - c#

I have a a class that has an integer array property and I am trying to figure out the right syntax for it. The integer array gets instantiated in the class constructor.
class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { /* Some logic */ }
set { /* Some logic */ }
}
public DemoClass(int elements)
{
// Here, the array should get instantiated using the elements.
}
}
How does the get/set block syntax work if I want my client code to retrieve a number from the array through the property MyNumbers?
How can I send it the right index?
What do I have to initialize?

Are you looking for:
class DemoClass
{
public int[] MyNumbers { get; private set; }
public DemoClass(int elements)
{
MyNumbers = new int[elements];
}
}
As for normal properties that do nothing except publicize a private field (as you seem to want):
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value; }
}

CA1819: Properties should not return arrays
http://msdn.microsoft.com/en-us/library/0fss9skc.aspx
Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.
To fix a violation of this rule, either make the property a method or change the property to return a collection instead of an array

If the number of element in the array is fixed, I would only provide a getter for the array and leave off the setter. You will still be able to assign values to individual elements in the array, but this will prevent someone from swapping the whole array out from under you (or setting it to null. The code would look like this:
class DemoClass
{
public int[] MyNumbers
{ get; private set; }
public DemoClass(int elements)
{
MyNumbers = new int[elements];
}
}
If the number of elements are not fixed, then you should use a List<int> rather than an array, and then you definitely want a property with no setter.

class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value; }
}
public DemoClass(int[] elements)
{
myNumbers = elements;
// Here, the array should get instantiated using the elements.
}
}

It is called Auto-Implemented Properties . So if you have syntax like
public int[] MyNumbers { get; set; }
C# compiler will automatically create for you backing field. This feature was introduced in C# 3.0, and before that you always had to implement property with backing field.
You can read more at: New C# "Orcas" Language Features: Automatic Properties, Object Initializers, and Collection Initializers

class DemoClass
{
private int[] myNumbers;
public int[] MyNumbers
{
get { return myNumbers; }
set { myNumbers = value;}
}
public DemoClass(int elements)
{
// Here, the array should get instantiated using the elements.
MyNumbers = new int[5] { 1, 2, 3, 4, 5};
}
}

Related

How do I give a class an array that can be accessed by class methods?

I have just started learning OOP. I am making my own class which builds on an array. It has an attribute called length and an array that is of that length.
However, the actual value of length is only declared in the constructor, so my array is stuck as a private variable within the constructor.
How do I implement one such that the array has a certain user-chosen length and is able to be accessed by the class' methods?
public class myClass
{
private int length; //This is an attribute of my class
public myClass(int myLength)
{
length = myLength;
int[] myArray = new int[length];
}
}
I want myArray to be accessible but this is not possible because it is a local variable in the constructor. I think if it was in Python I could just make it a global variable. (Although I think that I would still like to keep this array private as it is also an attribute).
Thanks!
Note: This is not homework but rather something I've been challenging myself to do.
Here's how your class could look like, the OOP way:
public class MyClass
{
public readonly int Length;
public int[] Data { get; private set; }
public MyClass(int dataLength)
{
Length = dataLength;
Data = new int[dataLength];
}
}
Here:
The Data array is constructed with the user-specified length.
You can access both Length and Data from inside and outside the class
Once instantiated, the Length property cannot be modified
Another way would be to make Length return directly the Length property of the array, as long as it was instantiated:
public class MyClass
{
public int Length { get { return Data == null ? 0 : Data.Length; } }
public int[] Data { get; private set; }
public MyClass(int dataLength)
{
Data = new int[dataLength];
}
}
Revised answer as you have added more code to the question:
You have unwittingly solved your own problem. Take a look at your private int length; declaration. After the object the initialized with the constructor public myClass(int myLength), the length variable is still accessible within the object.
The sample code below has a new public int GetLengthPlusOne() method to access the length variable. Similarly, you can now use the myArray variable in another method.
MyOtherClass
public class MyOtherClass
{
public void SampleMethod()
{
MyClass cls = new MyClass(5);
Console.WriteLine(cls.GetLengthPlusOne()); //Output: 6
var arr = cls.myArray;
}
}
MyClass
public class MyClass
{
private int length; //This is an attribute of my class
/*
* Declaring this within the class instead of the constructor allows it
* to be persisted in an instance of the class. This is a property.
*/
public int[] myArray { get; set; }
public MyClass(int myLength)
{
length = myLength;
myArray = new int[length];
}
public int GetLengthPlusOne()
{
return length + 1;
}
}
Side note on naming conventions:
For C#, Class names start capitalized (MyClass), whilst instances of a class start with a lower-case (myClass). Have a look at the documentation for more info.

Is there any better way to encapsulate an array?

How I do it currently:
class Foo
{
public int[] A { get { return (int[])a.Clone(); } }
private int[] a;
}
I think it's bad because it creates a clone and casts whenever I access it. I know I can work around it by introducing an additional variable like this
var foo = new Foo();
// as soon as you have to access foo.A do this
int[] fooA = foo.A;
// use fooA instead of foo.A from now on
but still it just looks bad.
I also dislike the java way of encapsulating
int get(int index) { return a[index]; }
because I dont get the advantages of using an array.
Is there any better way to do this?
edit: I want an array of encapsulated variables. The problem is that
public int[] A { get; private set; }
is not an array of encapsulated variables because I can modify elements of the array from outside of the class.
edit: It should also work with multidimensional arrays
Arrays implement IReadOnlyList<T> which exposes all of the relevant information you want (an iterator, an indexer, count, etc.) without exposing any of the mutable functionality of the array.
class Foo
{
public IReadOnlyList<int> A { get { return a; } }
private int[] a;
}
alternatively, you could use an iterator/generator to return the items as requested:
class Foo
{
public IEnumerable<int> A
{
get
{
foreach (int i in a)
yield return i;
}
}
private int[] a;
}
... then iterate over them normally or use LINQ to get them as a new array or other type of collection:
int[] arr = foo.A.ToArray();
Why not expose A as a implementation of IReadOnlyList
class Foo
{
public IReadOnlyList<int> A { get { return a; } }
private int[] a;
}
This allows you to return the Array as a collection where they can use the index but cannot change the contents of the array itself.
sounds like you need an indexer
...
public int this[int i]{
get{return a[i];}
set{a[i] = value;}
}
....
https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

Need explanation on these bits of codes

i have recently stumbled upon a project(8-puzzle solver using A* alg) in which some codes are weird to me , because i have never seen the likes of it before .
what does this line mean ? what is this ?!
this[StateIndex]
whats this notation ? i cant undersand it at all !
i posted a sample of the class so that you can see it almost all together .
and one more question , is it not wrong to have a class implemented like StateNode? it used only a constructor to initialize its fields , and yet worst, declared them all public ! should he/she not have implemented Propertise for this task?
public enum Direction
{
Up = 1, Down = 2, Left = 3, Right = 4, UpUp = 5, DownDown = 6, LeftLeft = 7, RightRight = 8, Stop = 9
}
class StateNode
{
public int Parent;
public List<int> Childs;
public Direction Move;
public Direction ParentMove;
public byte[,] State;
public byte Depth;
public byte NullRow;
public byte NullCol;
public StateNode()
{ }
public StateNode(int NewParent, Direction NewMove, Direction ParentMove, byte NewDepth, byte NewNullRow, byte NewNullCol)
{
this.Parent = NewParent;
this.State = new byte[5, 5];
this.Move = NewMove;
this.ParentMove = ParentMove;
this.Depth = NewDepth;
this.NullRow = NewNullRow;
this.NullCol = NewNullCol;
this.Childs = new List<int>();
}
}
class StateTree : List<StateNode>
{
public static long MakedNodes;
public static long CheckedNodes;
public static byte MaxDepth;
public List<int> Successor1(int StateIndex)
{
List<int> RetNodes = new List<int>();
StateNode NewState = new StateNode();
//Up
if (this[StateIndex].NullRow + 1 <= 3 && this[StateIndex].ParentMove != Direction.Up)
{
NewState = ChangeItemState(this[StateIndex], StateIndex, Direction.Up, Direction.Down, Convert.ToByte(this[StateIndex].Depth + 1), this[StateIndex].NullRow, this[StateIndex].NullCol, Convert.ToByte(this[StateIndex].NullRow + 1), this[StateIndex].NullCol);
this.Add(NewState);
RetNodes.Add(this.Count - 1);
StateTree.MakedNodes++;
this[StateIndex].Childs.Add(this.Count - 1);
if (NewState.Depth > StateTree.MaxDepth)
StateTree.MaxDepth = NewState.Depth;
}
//Down
//Left
//Right
return RetNodes;
}
}
In your concrete case it's just access to the element, as it used inside the class that is derived from the List<T>
But it can be also indexer which enables index acces to your class object.
For example declare class like this:
public class ListWrapper
{
private List<int> list = ...
public int this[int index]
{
return list[index];
}
}
and after use it like
var lw = new ListWrapper();
//fill it with data
int a = lw[2]; //ACCESS WITH INDEX EVEN IF THE TYPE IS NOT COLLECTION BY ITSELF
this[StateIndex] is using the current class' indexer property. The indexer property is what allows you to access an element in a collection or list object as if it was an array. For instance:
List<string> strings = new List<string>();
strings.Add("Item 1");
strings.Add("Item 2");
strings.Add("Item 3");
string x = strings[0]; // Returns the first item in the list ("Item 1")
When you want to access the indexer property of your own class, however, you have to preface it with the this keyword. You'll notice that in your example, the StateTree class doesn't implement an indexer property, so that may be adding to your confusion. The reason it works is because StateTree inherits from List<StateNode> which does implement an indexer property.
But don't get confused between classes with indexer properties and arrays. Arrays are a completely different thing, though the syntax is similar. An array is a list of objects which can be accessed by an index. An indexer property is an unnamed property of a single object that acts as an array. So for instance, List<string> has an indexer property, so you can access the items it contains using the same syntax as an array index (as shown in the above example). However, you can still make an array of List<string> objects. So for instance:
List<string> strings1 = new List<string>();
strings1.Add("Item 1.1");
strings1.Add("Item 1.2");
List<string> strings2 = new List<string>();
strings2.Add("Item 2.1");
strings2.Add("Item 2.2");
List<string>[] stringsArray = new List<string>[] { strings1, strings2 };
object result;
result = stringsArray[0]; // Returns strings1
result = stringsArray[0][1]; // Returns "Item 1.2"
result = stringsArray[1][0]; // Returns "Item 2.1"
As far as StateNode goes, there's nothing technically wrong with it, and it's not unusual to have a constructor that initializes all the field values, but it's always better to use properties instead of public fields.
its Indexed Properties in C# .net .
you can check Tutorial : http://msdn.microsoft.com/en-us/library/aa288464(v=vs.71).aspx check here
this[StateIndex] is pointing to an element within the class. Because StateTree inherits from a List<T>, you have a collection that's accessible by index (in this case this[N] where N is the element's index.
this[StateIndex] is how you give a class and indexed property e.g
public class IndexedClass
{
private List<String> _content;
public IndexedClass()
{
_content = new List<String>();
}
public Add(String argValue)
{
_content.Add(argValue);
}
public string this[int index]
{
get
{
return _content[index];
}
set
{
_content[Index] = value;
}
}
}
so now you can do
IndexedClass myIndex = new IndexedClass();
myIndex.Add("Fred");
Console.Writeline(myIndex[0]);
myIndex[0] = "Bill";
Console.Writeline(myIndex[0]);
As for statenode if it's local to the class (a helper) then you could argue it as okay, I don't like it though, another ten minutes work it could be done properly. If it's public in the assembly, then it's not accpetable in my opinion. But that is an opinion.

Dynamically add values to List<double> using get & set

Is it possible to do somethink like
public class TestClass
{
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.Add(this); //how to add to the List??
}
}
}
The reason I want to do this (I do not know if this is a best method, just as far as my knowledge allows) because I have to get data from xml files that do not have always same number of data in them.
Later I want to fill a ListView rows and using list I can count how many items are and how many columns will be needed.
Here is a schematic of xml file:
and there are also Trigger and PostTrigger nodes in xml file with same data sorting.
and here is the listview I want to achive:
Link to full size image
So, there are some pin groups and each pingroup has lots of data, the above code I gave, was just to hold 1 of the voltage nodes in xml file.
I am pretty much listening for your ideas!
Thanks.
No, and it defies usage of properties - you should implement it as an Add (or similarly aptly named) method.
You can't add this, because this is a TestClass, not a double; and you can't add value, as otherwise suggested, because that is a List<double>, and Add requires a double.
It's not clear how you would use this, but it looks like a very bad idea to me. Setting a collection as a property is slightly unusual already, but it's even odder for that set operation to mutate the list. It's additionally weird that you're not using the value variable within the setter... why not?
You should consider what the calling code would look like, and whether that's really the clearest way of expressing the semantics you want.
set { preTvoltage.AddRange(value); }
As Jon Skeet is saying, this is not what you should do. Instead, do
TestClass t = new TestClass();
t.PreTvoltage.Add(...);
declaring the property as
public List<double> PreTvoltage
{
get { return preTvoltage; }
}
The type of a getter and setter must match.
You could have:
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.AddRange(value); //add all items in list assigned.
}
}
However, this seems like a bad idea as it would be confusing to users why the value got did not match the value just set. I would have the two operations as separate members, and the setter either not exist or else overwrite the existing preTvoltage entirely.
You can not implement it like this, the preferable way is to make collection controls like:
private IList<double> _preTvoltage = new List<double>();
public IEnumerable<double> preTvoltage
{
get
{
return preTvoltage.AsEnumerable();
}
}
public void AddTvoltage(double item)
{
_preTvoltage.Add(item);
}
Well I managed to solve my problem this way:
public class ITestData
{
public string pinName { get; set; } //Name of the pin
public double stressLevel { get; set; } //Stress level for latchup
public int psuCount { get; set;} //Number of PSU's
public List<double[]> preTrigger = new List<double[]>();
public List<double[]> inTrigger = new List<double[]>();
public List<double[]> postTrigger = new List<double[]>();
public void AddPreTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
preTrigger.Add(data);
}
public void AddInTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
inTrigger.Add(data);
}
public void AddPostTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
postTrigger.Add(data);
}
}

inheritance - exercise

Alright, so I'm currently learning inheritance, and I've done something, this is the code:
class array
{
int [] arr;
public array(int[] arr)
{
this.arr = arr;
}
public int Biggest()
{
return arr.Max();
}
public int Min()
{
return arr.Min();
}
public int SumArr()
{
return arr.Sum();
}
}
class array2 : array
{
public array2(int [] arr):base(arr)
{
}
public double Average()
{
return
}
}
Now, in the derived class I need to get the average of the array and I can't do arr.Average()
The error says: Error 1 'ConsoleApplication1.array.arr' is inaccessible due to its protection level C:\Users\x\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 35 20 ConsoleApplication1
Anyone can tell me what am I doing wrong?
Thanks for helpers!
arr is declared as private since you didn't specify a visibility type. If you change it to protected, then your subclass array2 will be able to access it.
protected int [] arr;
In your example, you've made the arr array implictly private where you have omitted the modifier itself. If you want to provide access to said field in your derived classes, you'd need to use an access modifier of protected:
protected int[] arr;
But it's not recommend to expose fields this way, as fields should really be for private class state. What you should do, is encapsulate read-only access to that field, via a property:
protected int[] Array {
get { return this.arr; }
}
Which means in your derived class, you can:
public double Average()
{
return this.Array.Average();
}
In C#, the default access level for a class member is private. You haven't specified an access level for int [] arr, so it is private and hence visible to the other class.
That's because your arr field is private. consider using a write-private read-protected property instead, like this:
class array
{
protected int[] arr {get; private set; }
public array(int[] arr)
{
this.arr = arr;
}
public int Biggest()
{
return arr.Max();
}
public int Min()
{
return arr.Min();
}
public int SumArr()
{
return arr.Sum();
}
}
class array2 : array
{
public array2(int [] arr):base(arr)
{
}
public double Average()
{
return arr.Average();
}
}
Since you didn't explicitly define an access type for arr, it is implicitly set to private. To be able to access this from the derived class, you should set it to protected.
protected int [] arr;
There are better ways to do this, e.g. through a property, but that's the general idea.
Add public or protected to the arr variable:
public int [] arr;
protected int [] arr;
If you don't specify otherwise, the compiler will default to making your declarations private.

Categories