Can't print Stack<T> C# [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 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.

Related

Why IEqualityComparer doesn't work as expected? [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 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.

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")

EndsWith() not working properly [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 6 years ago.
Improve this question
I want to add the corresponding index of all the strings in the tres4 array, if they match the end of the input string. Yet, my list gets filled with all the indexes 1-12, as opposed to only those, matching the end of my input string. Only 1 should be added to my List in this case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Encoding
{
class Program
{
static void Main(string[] args)
{
string[] tres4 = {
"CHU",
"TEL",
"OFT",
"IVA",
"EMY",
"VNB",
"POQ",
"ERI",
"CAD",
"K-A",
"IIA",
"YLO",
"PLA"
};
string message = "CHUTEL";
List<int> digits = new List<int>();
for (int i = 0; i < tres4.Length; i++)
{
if (message.EndsWith(tres4[i]));
{
digits.Add(i);
}
}
Console.WriteLine(String.Join(", ", digits));
}
}
}
You have an extra semicolon:
if (message.EndsWith(tres4[i]));
See the semicolon at the end? Remove it and it'll work.
Next time you should give a debugger a try, it would show you the problem right away.
Remove ; from if condition
if (message.EndsWith(tres4[i]))

Facing this error : "No overload for method 'ReadLine' takes 1 arguments" [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 7 years ago.
Improve this question
hello guys i am new to c# programming but i was just try to take an input from the user by c# console application and save that info to an string array but its giving me an error
Error 1 No overload for method 'ReadLine' takes 1 arguments
namespace demo_try
{
class Program
{
static void Main(string[] args)
{
//string[] sarry ={"hi -", "me"};
//for (int i = 0; i < sarry.Length; i++)
//{
// Console.Write("- {0} -", sarry[i]);
//}
//foreach (var n in sarry)
//{
// Console.Write("-{0}",n);
//}
string [] sarray= new string [10] ;
for (int i = 0; i < sarray.Length; i++)
{
Console.Write("Enter the values for an array {0}", sarray[i]);
Console.ReadLine(sarray[i]);
}
}
}
}
Error 1 No overload for method 'ReadLine' takes 1 argumentts
that would be great if you guys help me out in this matter :)
The error is pretty self explanatory. Read this documentation for reference https://msdn.microsoft.com/en-us/library/system.console.readline%28v=vs.110%29.aspx
You need to change your code to
sarray[i] = Console.ReadLine();

"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) }.

Categories

Resources