Why IEqualityComparer doesn't work as expected? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Why EqualityComparer MyEqComp doesn't work?
It should left in arr only 2 elements with equal Values - myDic["1"]and myDic["3"] with Value="456". But instead it consists of myDic["1"] and myDic["2"] but their Values are not equal.
Function GetHashCode returns the same hash code for 1st and 3rd elements!
public class Program
{
public static Dictionary<string,string> myDic = new Dictionary<string,string>();
public static void Main()
{
myDic.Add("1","456");
myDic.Add("2","567");
myDic.Add("3","456");
var arr=myDic.Distinct(new MyEqComp());
foreach (var n in arr)
{
Console.WriteLine("key="+n.Key+",value="+n.Value+"\n");
}
}
class MyEqComp:IEqualityComparer<KeyValuePair<string,string>>{
public bool Equals(KeyValuePair<string,string> pair1, KeyValuePair<string,string> pair2){
Console.WriteLine("pair1="+pair1+",pair2="+pair2);
return pair1.Value == pair2.Value;
}
public int GetHashCode(KeyValuePair<string,string> pair){
var code = pair.Value.GetHashCode();
Console.WriteLine("code="+code);
return code;
}
}
}
And the output
code=-121858068
key=1,value=456
code=437991364
key=2,value=567
code=-121858068
pair1=[1, 456],pair2=[3, 456]

Answer: Code is correct and works as expected.

Related

How can I compare two string values for equality? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to find the klas of 5TIF2 and dont now how to compare it to the class
private void BtnFind_Click(object sender, RoutedEventArgs e)
{
Leerling oleerling = new Leerling();
oleerling.MijnId = TxtId.Text;
oleerling.Voornaam = TxtVoornaam.Text;
oleerling.Naam = TxtNaam.Text;
oleerling.Klas = TxtKlas.Text;
if (oleerling.Klas = "5TIF2")
{
LstLeerlingen.Items.RemoveAt(2);
LstLeerlingen.Items.RemoveAt(3);
}
else
{
LstLeerlingen.Items.RemoveAt(0);
LstLeerlingen.Items.RemoveAt(1);
}
}
You need to use the operator == to check if a statement is true or false, instead you have used =, which is for assigning values.
You need:
if (oleerling.Klas == "5TIF2")

Can't print Stack<T> C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi I have a question about my code. Everything should work without a problem, but when I try to print on the console, the stack gives me the following
input:
4
Output:
System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.
Stack1 [System.UInt64]
my question is if i need to add a new library because even with int it gives me the same.
using System;
using System.Collections.Generic;
namespace ConsoleApp29
{
class Program
{
static void Main(string[] args)
{
Stack<ulong> nm = new Stack<ulong>();
ulong p = ulong.Parse(Console.ReadLine());
for(ulong i = 0; i < p; i++)
{
nm.Push(i);
}
foreach(int i in nm)
{
Console.Write(nm);
}
}
}
}
nm, which you are printing, is the entire Stack object; you want to be printing i, the current element of nm.

How to resolve “CS1503 Argument 1: cannot convert from 'method group' to 'bool''” compilation error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The compiler shows error
CS1503 Argument 1: cannot convert from 'method group' to 'bool'.
I don't understand why Console.WriteLine(Сalculate) does not output. Thank you for answer.
The code here:
public static double Calculate(string userInput)
{
var parts = userInput.Split(' ');
var sum = double.Parse(parts[0]);
var rate = double.Parse(parts[1]);
var time = double.Parse(parts[2]);
return sum * (1 - Math.Pow(rate/ rate, time)) / (1 - rate/ rate);
}
static void Main()
{
Calculate(Console.ReadLine());
Console.ReadKey();
Console.WriteLine(Сalculate);
Console.ReadKey();
}
You can do this:
static void Main()
{
// get result from your method
var result = Calculate(Console.ReadLine());
Console.ReadKey();
// print result
Console.WriteLine(result);
Console.ReadKey();
}

"A namespace cannot directly contain members such as fields or methods" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

C# Multi Spintax - Spin Text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

Categories

Resources