Updated April 7, 2023
Introduction to C# object to dictionary
In C# object is one of the root and parent class for all the subclasses; based on the requirement, we can able to convert the object to other types like a dictionary, strings like that dictionary is one of the generic collection class that can be used for to store the datas as key/value pairs by using the System.Collections.Generic package the dictionary class will be imported while we are passing the object reference as a parameter. It will be converted to the dictionary datas with the help of its reference. We can also pass the different set of parameters in Dictionary classes.
Syntax
In C#, it has some pre-defined keywords, variables, and methods for accessing the application. Like that, we used the dictionary as one of the collection classes that represent the key-value pairs. It has a collection of words and their meanings; the dictionary collection will be of words that has related to the specific English dictionary words.
using System;
Access Modifiers class class name
{
constructor()
{
---some c# code logics----
}
Access Modifiers return type Method name(object reference name)
{
var reference name = new Dictionary<datatypes, Type>();
conditional statements or loops for to convert the object reference to dictionary reference
----some c# code logics depends upon the requirement—
}
}
The above codes are the basic syntax for converting the object values to dictionary values. Based on the user requirements, we need to call some default methods, keywords and variables.
How object to dictionary function work in C#?
The dictionary is one of the collection classes that will be used to store the datas as key-value pairs; with no specific order, it will be either in ascending, descending orders. The System.Collection.A generic namespace is used for implementing the dictionary classes and their function for implementing the application. We can also use the interface for implementing the dictionary classes that is IDictionary<TKey, TValue> is one of the basic interfaces for using the dictionary characteristics.
Whenever and wherever we used the keys on the method, it must be a unique one, and it will not accept the duplicate keys; also, the null keys are not accepting the dictionary, but when in case of values, it accepts null and duplicate like map classes in java. The values are associated, and they can be accessed with parameters, so while we are passing the parameters, it may be of keys and values or some other types also called with the arguments. Also, the values are accessible with associated keys that are each key have separate values and the index for generating the values. When we use a dictionary, we can also set the size limit, and also we used different data types with the same method with two different sets of arguments.
Examples of C# object to dictionary
Below is the different example of C# object to dictionary:
Example #1
using System;
class demo
{
public string first { get; set; }
public int second { get; set; }
public int third { get; set; }
public override bool Equals(object vars)
{
var vars1 = vars as demo;
if (object.ReferenceEquals(vars1, null))
return false;
return first == vars1.first && second == vars1.second && third == vars1.third;
}
public override int GetHashCode()
{
var vars2 = 234;
if (first != null)
vars2 = first.GetHashCode();
vars2 = unchecked((vars2 * 625) ^ second);
vars2 = unchecked((vars2 * 725) ^ third);
return vars2;
}
public override string ToString()
{
return string.Format("Welcome To My Domain its a first example for dictionary concepts", first, second, third);
}
public static void Main()
{
demo d = new demo();
Console.WriteLine(d.ToString());
Console.WriteLine("Your first example is ended and completed while we can overwride we can also overwrite the method values");
}
}
Output:
In the first example, we used dictionary classes, and its namespace will be used for converting the object to dictionary datas. We also used some default methods for creating the application based on the requirements we need to translate the datas.
Example #2
using System;
using System.Collections.Generic;
namespace Examples
{
public class demo
{
static void Main(string[] args)
{
dem01 d = new dem01()
{
num=76325,
strings="welcome to my domain its a first object creation for example2"
};
dem01 d1 = new dem01()
{
num=7792,
strings="welcome to my domain its a second object creation for example2"
};
Dictionary<int, dem01> dvalues = new Dictionary<int, dem01>();
dvalues.Add(d.num, d);
dvalues.Add(d1.num, d1);
dem01 d3 = dvalues[7792];
Console.WriteLine("dem01 7792 in dem01 dictionary");
Console.WriteLine("num=435376, strings=Thank you user for entering the second example values",
d3.num, d3.strings);
Console.WriteLine();
Console.WriteLine("Thank you user for entering the second example values");
foreach (KeyValuePair<int, dem01> vars1 in dvalues)
{
Console.WriteLine("vars = " + vars1.Key);
dem01 d4 = vars1.Value;
Console.WriteLine("num=8799, strings=Thank you user for entering the second example values",
d4.num, d4.strings);
}
Console.WriteLine();
Console.WriteLine("Thank you user for entering the second example values");
foreach (var vars1 in dvalues)
{
Console.WriteLine("vars = " + vars1.Key);
dem01 d5 = vars1.Value;
Console.WriteLine("num=86234, strings=Thank you user for entering the second example values",
d5.num, d5.strings);
}
Console.WriteLine();
Console.WriteLine("Thank you user for entering the second example values");
foreach (int vars in dvalues.Keys)
{
Console.WriteLine(vars + "Thank you users ");
}
Console.WriteLine();
Console.WriteLine("Thank you user for entering the second example values");
foreach (int vars in dvalues.Keys)
{
Console.WriteLine(vars + " ");
dem01 d6 = dvalues[vars];
Console.WriteLine("num=86234, strings=Thank you user for entering the second example values",
d6.num, d6.strings);
}
Console.WriteLine();
Console.WriteLine("Thank you user for entering the second example values");
foreach (dem01 d7 in dvalues.Values)
{
Console.WriteLine("num=86234, strings=Thank you user for entering the second example values",
d7.num, d7.strings);
}
if (!dvalues.ContainsKey(86234))
{
dvalues.Add(86234, d);
}
Console.WriteLine();
if (dvalues.ContainsKey(7792))
{
dem01 d4 = dvalues[7792];
}
else
{
Console.WriteLine("vars does not exist in the dictionary");
}
Console.ReadKey();
}
}
public class dem01
{
public int num { get; set; }
public string strings { get; set; }
}
}
Output:
In the second example, we used a dictionary in two different objects. That is, we can convert the two-class instances and objects to dictionaries.
Example #3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Examples
{
class demo
{
static void Main(string[] args)
{
string[] str = new string[]
{
"Siva",
"Raman",
};
var vars = str.ToDictionary(vars1 => vars1,
vars1 => false);
foreach (var vars2 in vars)
{
Console.WriteLine("Welcome To my Domain",
vars2.Key,
vars2.Value);
}
Console.Read();
}
}
}
Output:
In the final example, we used string[] array class, and we can use that object to dictionary values. Using forEach loop, we can iterate the string values and print them on the output console.
Conclusion
In C#, we can convert the classes objects to other types like dictionaries etc. Based on the requirement, we can able to convert the datas here; we used default classes of the c# or some other pre-defined or built-in classes to the dictionary types values; it is a very fast data structure.
Recommended Articles
This is a guide to C# object to dictionary. Here we discuss How object to dictionary function work in C# and Examples along with the outputs. You may also have a look at the following articles to learn more –