Force all values in a dictionary to be unique? [duplicate] - c#

This question already has answers here:
Getting multiple keys of specified value of a generic Dictionary?
(16 answers)
C# dictionary type with unique keys and values
(5 answers)
Closed 6 years ago.
I need to maintain a mapping between enum values, and a multi-value type (name+number). e.g.
ABC : {"john", 123}
DEF : {"paul", 234}
GHI : {"ringo", 345}
I planned to do this using a Dictionary of some type but strictly speaking not only the keys should be unique, but the values too - it's like a bi-directional dictionary.
Then I need to be able to write a method MyEnum lookup(string name, int number) which I'd planned would call .Single(x => x.Value.name == name && x.Value.number == number) But I'm struggling to find the right types and apparently the KeyValuePair returned cannot be null which is a problem if there is no match.
What is a good way to approach this?

Related

How to get the item type of a List? [duplicate]

This question already has answers here:
How to get the type of T from a member of a generic class or method
(17 answers)
What is '1 in Collection type Name
(1 answer)
Closed 1 year ago.
Suppose I have a "Type mytype", and if I do "mytype.ToString()", I get "System.Collections.Generic.List`1[Example1.Employee]", how do I get the list item type, which is "Example1.Employee"? Do I have to parse this string to get this list item type? Is there a more elegant way, such as "mytype.ItemType"?
Also, why is there a "`1" appendix at the end of "System.Collections.Generic.List`1"?
And, why is there a "`2" at the end of "System.Collections.Generic.Dictionary`2"?
Simple:
Type element = list.GetType().GenericTypeArguments[0]
And the "`1" just indicates the number of generic parameters.

Copy properties from object A to B when that property is NULL [duplicate]

This question already has answers here:
Merging two objects in C#
(7 answers)
Closed 2 years ago.
I have two objects that are both type Shoppingcart.
One is called shoppingcartA, the other shoppingcartDefault.
shoppingcartA has some of the properties set, but some are NULL.
I want to replace every property of shoppingcartA that is NULL with the value that shoppingcartDefault has.
The problem is that I don't know the names of these properties (or I do, but there are 100 properties and I don't want to manualy type them all).
I've looked at a foreach that loops over every property that shopingcartA has but couldn't find a way to then take that same property from shoppingcartDefault and stick it in there.
You can map like this
foreach (var propertyInfo in test2.GetType().GetProperties())
{
if (propertyInfo.GetValue(test2) == null)
{
propertyInfo.SetValue(test2, propertyInfo.GetValue(test1));
}
}

Having created an enum, how would I be able to assign them a decimal type value in a get method? [duplicate]

This question already has answers here:
Can you use Enum for Double variables?
(6 answers)
Closed 3 years ago.
I am trying to create a program in where I am trying to give/assign a created enumeration list, a monetary value to be returned. However I am unsure how I can do this.
public decimal GetAccessoriesCost()
{
?????????
}
public enum Accessories
{
None,
StereoSystem,
LeatherInterior,
StereoAndLeather,
ComputerNavigation,
StereoAndNavigation,
LeatherAndNavigation,
All
}```
I want Stereo to be 20.20
and Leather to be 10.10
The enums in C# (unlike Java) have to be cardinal (something that is basically an integer or a whole number). see this answer for more details Can you use Enum for Double variables?
The reason enums have this constraint is to be closer to c++ and c where the enums are a fancy way to group integer constants in a namespace and probably generate a sequence.

Get distinct parameters from a List [duplicate]

This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).

Using a Hashtable to store only keys? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Which collection for storing unique strings?
I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.
Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?
.NET 3.5 includes the HashSet<T> collection type, which sounds like what you want.
HashSet<T>

Categories