Dictionary vs KeyValuePair vs Struct - C#
Dictionary :
// Declare Dictionary and assign value
Dictionary<string, object> parameter = new Dictionary<string, object>();
parameter["View"] = viewComp;
parameter["Play"] = true;
FunctionName(parameter);
// Use Dictionary
private void FunctionName(object parameter)
{
Dictionary<string, object> dict = (Dictionary<string, object>)parameter;
GameObject viewComp = (GameObject)dict ["View"];
bool play = (bool)dict["Play"];
UseValuesInFunction(viewComp, play);
}
KeyValuePair :
KeyValuePair<GameObject, bool> parameter = new KeyValuePair<GameObject, bool>(viewComp, true);
FunctionName(parameter);
// Use KeyValuePair
private void FunctionName(object parameter)
{
KeyValuePair<GameObject, bool> param = (KeyValuePair<GameObject, bool>)parameter;
GameObject viewComp = (GameObject)dict ["View"];
bool play = (bool)dict["Play"]; UseValuesInFunction(viewComp, play);
}
Struct:
public struct StructData
{
public GameObject GameObj;
public bool Play;
public StructData(GameObject gameObj, bool play)
{
GameObj = gameObj;
Play = play;
}
}
StructData parameter = new StructData(particle,play);
FunctionName(parameter);
public void FunctionName(object parameter)
{
StructData param = (StructData)parameter;
GameObject particle = (GameObject)param.ParticleSys;
bool play = (bool)param.Play;
}
_________________________________________________
Other types of cases :
List<object> parameter = new List<object>();
parameter.Add(new KeyValuePair<string, object>("View", viewComp));
parameter.Add(new KeyValuePair<string, object>("Play", true));
FunctionName(parameter);
private void FunctionName(object parameter)
{
List<object> param = (List<object>)parameter;
GameObjectViewComponent viewComp = null;
bool play = false;
for (int i = 0, count = param.Count; i < count; i++)
{
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)param[i];
switch(pair.Key)
{
case "View":
viewComp = (GameObjectViewComponent)pair.Value;
break;
case "Play":
play = (bool)pair.Value;
break;
}
}
UseValuesInFunction(viewComp, play);
}
//or
FunctionName2(parameter);
private void FunctionName2(object parameter)
{ // Here you will have to read values exactly in order you have entered.
List<object> param = (List<object>)parameter;
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)param[0];
GameObjectViewComponent viewComp = (GameObjectViewComponent)pair.Value;
pair = (KeyValuePair<string, object>)param[1];
bool play = (bool)pair.Value;
UseValuesInFunction(viewComp, play);
}
Another way of doing this is to just use the List of objects.
List<object>
List<object> parameter = new List<object>();
parameter.Add(particle);
parameter.Add(effectLocator);
parameter.Add(delay);
parameter.Add(play);
FunctionName(parameter);
public void FunctionName(object parameter)
{
List<object> param = (List<object>)parameter;
ParticleSystem particle = (ParticleSystem)param[0];
Transform effectLocator = (Transform)param[1];
float delay = (float)param[2];
bool play = (bool)param[3];
.....
.....
}
// Declare Dictionary and assign value
Dictionary<string, object> parameter = new Dictionary<string, object>();
parameter["View"] = viewComp;
parameter["Play"] = true;
FunctionName(parameter);
// Use Dictionary
private void FunctionName(object parameter)
{
Dictionary<string, object> dict = (Dictionary<string, object>)parameter;
GameObject viewComp = (GameObject)dict ["View"];
bool play = (bool)dict["Play"];
UseValuesInFunction(viewComp, play);
}
KeyValuePair :
KeyValuePair<GameObject, bool> parameter = new KeyValuePair<GameObject, bool>(viewComp, true);
FunctionName(parameter);
// Use KeyValuePair
private void FunctionName(object parameter)
{
KeyValuePair<GameObject, bool> param = (KeyValuePair<GameObject, bool>)parameter;
GameObject viewComp = (GameObject)dict ["View"];
bool play = (bool)dict["Play"]; UseValuesInFunction(viewComp, play);
}
Struct:
public struct StructData
{
public GameObject GameObj;
public bool Play;
public StructData(GameObject gameObj, bool play)
{
GameObj = gameObj;
Play = play;
}
}
StructData parameter = new StructData(particle,play);
FunctionName(parameter);
public void FunctionName(object parameter)
{
StructData param = (StructData)parameter;
GameObject particle = (GameObject)param.ParticleSys;
bool play = (bool)param.Play;
}
_________________________________________________
Other types of cases :
List<object> parameter = new List<object>();
parameter.Add(new KeyValuePair<string, object>("View", viewComp));
parameter.Add(new KeyValuePair<string, object>("Play", true));
FunctionName(parameter);
private void FunctionName(object parameter)
{
List<object> param = (List<object>)parameter;
GameObjectViewComponent viewComp = null;
bool play = false;
for (int i = 0, count = param.Count; i < count; i++)
{
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)param[i];
switch(pair.Key)
{
case "View":
viewComp = (GameObjectViewComponent)pair.Value;
break;
case "Play":
play = (bool)pair.Value;
break;
}
}
UseValuesInFunction(viewComp, play);
}
//or
FunctionName2(parameter);
private void FunctionName2(object parameter)
{ // Here you will have to read values exactly in order you have entered.
List<object> param = (List<object>)parameter;
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)param[0];
GameObjectViewComponent viewComp = (GameObjectViewComponent)pair.Value;
pair = (KeyValuePair<string, object>)param[1];
bool play = (bool)pair.Value;
UseValuesInFunction(viewComp, play);
}
Another way of doing this is to just use the List of objects.
List<object>
List<object> parameter = new List<object>();
parameter.Add(particle);
parameter.Add(effectLocator);
parameter.Add(delay);
parameter.Add(play);
FunctionName(parameter);
public void FunctionName(object parameter)
{
List<object> param = (List<object>)parameter;
ParticleSystem particle = (ParticleSystem)param[0];
Transform effectLocator = (Transform)param[1];
float delay = (float)param[2];
bool play = (bool)param[3];
.....
.....
}
Comments