C# Linq unique not work on lists [duplicate] - c#

This question already has answers here:
Distinct() doesn't work
(4 answers)
Closed 7 years ago.
I am trying with the following code to check if a list contains duplicated data:
internal class Program
{
private static void Main(string[] args)
{
var list = new List<Obj>() { new Obj() { id = "1", name = "1" }, new Obj() { id = "1", name = "1" } };
Console.WriteLine(AllItemsAreUnique(list));
}
public static bool AllItemsAreUnique<T>(IEnumerable<T> items)
{
return items.Distinct().Count() == items.Count();
}
}
internal class Obj
{
public string id;
public string name;
}
And the result is true! Why?

Why?
By default, the comparison will use references and in this case, the two object references are not the same.
You need to implement IEquatable<T> to provide a type-specific Equals() method for Distinct() to use.

Related

How to assign a value to an enum? [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
Trying to assign a value to the _cat.CatType.
I was wondering how to do this without getting a null reference error?
using System;
public class Program
{
private CatClass _cat;
public void Main()
{
_cat.CatType = CatType.Active;
Console.WriteLine(_cat.CatType.ToString());
}
public enum CatType
{
New,
Active,
Inactive
}
public class CatClass
{
public CatType CatType
{
get;
set;
}
}
}
Ideally I want to assign it something like this _cat.CatType = CatType.Active
You need to initialise it with the new keyword
Used to create objects and invoke constructors
public void Main()
{
_cat = new CatClass();
_cat.CatType = CatType.Active;
Console.WriteLine(_cat.CatType.ToString());
}
You need to create an instance of the class.
private CatClass _cat = new CatClass;
Instantiate _cat with the new keyword first:
_cat = new CatClass
{
CatType = CatType.Active
};

Providing Access to a Class Private Member Attributes [duplicate]

This question already has answers here:
Properties vs Methods
(16 answers)
Closed 4 years ago.
I was wondering what is more efficient or the best practice for returning attributes of private members. For example:
class Foo
{
private List<int> fooList;
public Foo()
{
Random random = new Random();
fooList = new List<int>(random.Next(1, 100));
}
//
public int Count { get { return fooList.Count; } }
// or
public int Count() { return fooList.Count; }
}
Which is best if I do not want to give public access to my list?
Based on your example, you should stay with a property. Because the fooList.Count is a property.

Why does Newtonsoft.Json not serialise this property? [duplicate]

This question already has answers here:
How do I get json.net to serialize members of a class deriving from List<T>?
(3 answers)
Closed 5 years ago.
Consider the following program: (.NET Fiddle Link)
public class Program
{
public static void Main()
{
var carsa = new ListOfCarsA();
carsa.Cars.Add("Toyota");
carsa.Cars.Add("Lexus");
Console.WriteLine(JsonConvert.SerializeObject(carsa, Formatting.Indented));
var carsb = new ListOfCarsB();
carsb.Add("Nissan");
carsb.Add("Infiniti");
Console.WriteLine(JsonConvert.SerializeObject(carsb, Formatting.Indented));
}
}
public class ListOfCarsA
{
public string CollectionName { get { return "CarsA"; } }
public List<string> Cars { get; set; }
public ListOfCarsA()
{
Cars = new List<string>();
}
}
public class ListOfCarsB : List<string>
{
public string CollectionName { get { return "CarsB"; } }
}
This then outputs the following:
{
"CollectionName": "CarsA",
"Cars": [
"Toyota",
"Lexus"
]
}
And
[
"Nissan",
"Infiniti"
]
Why does the property CollectionName not get serialised and output CarsB, but the same property on the ListOfCarsA results in CarsA being serialised?
What is the solution to this problem - How could I have a class similar to ListOfCarsB but still have any extra members serialised? I have tried using the attributes [JsonProperty("CollectionName"] and [JsonRequired] but these seem to do nothing.
This is because your second example IS a list, whilst the first example only contains one. Json.Net knows how to create json from lists and so is ignoring the rest of your custom code. Not much you can do about this except write a custom formatter

Setting Property Value using Reflection [duplicate]

This question already has an answer here:
Can I set a property value with Reflection?
(1 answer)
Closed 6 years ago.
How do I set my property value from array via reflection in C#?
public class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public void SetValues(string[] items)
{
}
}
I need to use SetValues method to set property values from items array.
Considering you said you Need to use SetValues(string[] items) {...}, and that you'd have an Employee object such as:
Employee emp = new Employee();
I believe you're looking for:
string[] values = new string[] {"someName", "someID"};
typeof(Employee).GetMethod("SetValues").Invoke(emp, new object[]{ values });
Now SetValues would have to convert (in its body) "someID" to int with something like:
ID = int.Parse(items[1]);

i am trying to pass values to an array in another class [duplicate]

This question already has answers here:
How do define get and set for an array data member?
(9 answers)
Closed 6 years ago.
i am trying to pass values to an array in another class
this is an example of what i am trying to do in detail:
public class CustomString
{
private string[] StringToAppend;
public string[] StringToAppend1
{
get
{
return StringToAppend;
}
set
{
StringToAppend = value;
}
}
public Class Form1:Form
{
CustomString strng1 = new CustomString();
strng1.StringToAppend1 = {"sssf","vfdr";} //Fails to compile Here
}
strng1.StringToAppend1 = {"sssf","vfdr";} //Fails to compile Here
That is the incorrect syntax for initialising a string array
strng1.StringToAppend1 = new[]{"sssf","vfdr"} ;
Above is the correct syntax.

Categories