I have this dictionary and tuples set up in SetValue() as below :-
var myDict = new Dictionary<string, Tuple<string, string>>();
private void SetValue()
{
var myTuple1= Tuple.Create("ABC", "123");
var myTuple2= Tuple.Create("DEF", "456");
myDict.Add("One", myTuple1)
myDict.Add("Two", myTuple2)
}
I am trying to retrive the tuple in GetValue() as below :-
private void GetValue()
{
var myTuple = new Tuple<string, string>("",""); //Is this correct way to initialize tuple
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}
}
My question is whether this is the correct way to initialize tuple while retrieving the same from a dictionary ? Is there a better code?
var myTuple = new Tuple<string, string>("","");
If it's an out parameter, the object doesn't need to be initialized before being used. You should just be able to do:
Tuple<string,string> myTuple;
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}
You don't need to create an instance for an out parameter. Just declare the local variable as Tuple but don't assign a value.
Tuple<string, string> myTyple;
Related
I have a simple for loop but I want to convert it to Linq and return empty Dict. if object is null. I have not used it before. Can anyone help me out on this ?
private Dictionary<string, class1> getInfo(IEnumerable<class2> infos)
{
Dictionary<string, class1> trs = new();
if (infos is null)
return trs;
//This loop I want to convert to linq
for(class2 info in infos)
{
class1 tr = new()
{
bi = info.bi,
state = bi.State,
res = Enum.value;
};
trs.Add(info.value1, tr);
}
return trs;
}
You can use .ToDictionary() to create a dictionary from IEnumerable<class2>
Dictionary<string, class1> trs = infos
?.ToDictionary(key => Key.value1,
value => new class1() {
bi = value.bi,
state = bi.State,
res = Enum.value
}) ?? new Dictionary<string, class1>();
Free Advice : I highly recommend you to use proper naming conventions while declaring class and variables, this will make your code more readable and easy to understand
I need to create an expression that extract a specific item from a dictionary.
The topic is to put the name of the item to extract inside the Expression.
I'll try to explain it better with an example.
I have this dictionary:
Dictionary<string, object> myDictionary = new Dictionary<string, object>
{
{ "Name", "My first name" },
{ "Age", 42 }
};
I wrote this code to compose an Expression that works with IDictionary:
private Expression<Func<IDictionary<string, object>, string, object>> GetDictionaryExpression()
{
var dictionary = Expression.Parameter(typeof(IDictionary<string, object>), "dict");
var keyParam = Expression.Parameter(typeof(string));
var indexer = typeof(IDictionary<string, object>).GetProperty("Item");
var indexerExpr = Expression.Property(dictionary, indexer, keyParam);
return Expression.Lambda<Func<IDictionary<string, object>, string, object>>(indexerExpr, dictionary, keyParam);
}
Finally, I can write this code:
var x = GetDictionaryExpression();
var y = x.Compile().Invoke(myDictionary, "Name"); // My first name
This code works.
What I need is to create an expression that doesn't need the field "Name" at runtime, but put it inside the Expression.
In other words, something that will let me have something like this:
var x = GetNameFromDictionary(); // <== How to write this method that extract only the 'Name' value from the dictionary?
var y = x.Compile().Invoke(visionRecord); // My first name
Thank you in advance.
I am applying Google Places Api on my app and converting the Java to C# code. But when I am getting the nearby places in the list, I got this error:
Cannot implicitly convert type 'Java.Lang.Object' to 'Android.Runtime.JavaDictionary'. An explicit conversion exists (are you missing a cast?)
private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
{
for (int eachPlace = 0; eachPlace < placeList.Size(); eachPlace++)
{
MarkerOptions markerOptions = new MarkerOptions();
JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace); /*This is the error*/
}
}
Screenshot of the code
Any know how to fix this? Thank you.
You could use System.Collections.Generic.List instead of JavaList, for example :
using System.Linq;
...
private void showNearbyPlaces(List<JavaDictionary<string, string>> placeList)
{
for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
{
JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);
}
}
Try explicitly casting it:
JavaDictionary<string, string> googlePlace = (type)placeList.Get(eachPlace)
If that doesn't work, I'd suggest converting the placeList to a more primitive object (strings most likely), and then reconstructing it as a dictionary:
JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace).ConvertToString();
public JavaDictionary<string, string> ConvertToString(this [placeListType] placeList)
{
// Create a dictionary
// foreach object in placeList
// Get the first string and assign it
// Get the second string and assign it
// Create a new object with them as parameters
// Add object to dictionary
// Return dictionary
}
Use C# IDictionary instead like this,
IDictionary<string, string> placeList= new Dictionary<string, string>();
placeList.Add(new KeyValuePair<string, string>("your string", "your new string"));
How would one create a series of objects which are named object(x) where x is a number? like something like
int x = 1;
Object object(x) = new Object();
Every answer will be appreciated!
I suggest using Dictionary<int, Object> which is close to the desired syntax:
int x = 1;
// If you insist on object as a name you have to use # prefix
// since object is a reserved word
Dictionary<int, Object> #object = new Dictionary<int, object>();
#object.Add(1, new object());
...
// If you want to access the istance use #object[x]
object myObject = #object[x];
I want to create a list of strings whose name should be the value of string variable.
Overall I want to assign a variable value to a (object) listname.
(Pseudo Code):
string s = "listname";
list<String>.Name = s;
Closest thing you can do is use a Dictionary to reference the variables.
Dictionary<string, List<string>> myLists = new Dictionary<string, List<string>>();
var s = "listname";
myLists.Add(s, new List<string>());
// To access
var list = myLists[s];