Create Array Parameters in C#
// November 14th, 2009 // Programming
Today i have request from ‘bad friend’ code name ‘mashudi’ to create Nearest Neighbor Class, Artificial Intelegent system that can compute distance from one to other node.
From this project I will share how to create a constructor or maybe you need for method that use array parameters.
Design of implementation in KNN node class will be like this :
KNNNode node1 = new KNNNode("Rose", 2.5, 4, 4.2);
KNNNode node2 = new KNNNode("Height", 2.5, 4, 4.2, 4.5, 6.3);
So you can add more parameters to that class.
Here my code in KNNNode class :
using System.Collections.Generic;
namespace Algorithm.KNN
{
public class KNNNode
{
List val;
string label;
public string Label
{
get { return label; }
set { label = value; }
}
// 'params float[] val' it will create array parameters
public KNNNode(string label, params float[] val)
{
//set label
this.label = label;
//Set actual size of val
this.val = new List(val.Length);
//iterate from first to last argument
//and fill to list
foreach (float v in val)
{
this.val.Add(v);
}
}
public int Count
{
get {
return val.Count;
}
}
//prevent default constructor inisialization
private KNNNode()
{ }
//Direct access to list value
public float this[int index]
{
get { return val[index]; }
set { val[index] = value; }
}
}
}
Hope it’s help
Willy Achmat Fauzi



