✅ Hi im a beginner
im trying to make the function that i created in java to C# but i dont know whats the problem
37 Replies
that is the code im trying to recreate
This is the C# one
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/What problem are you talking about?
itemz
As seen in the foreach loopEven without the foreach it says abt the static thing
$static
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying
static
members, take the following considerations:
• If there are to be multiple instances of a class, do not use static
• If you need to track state, do not use static
• If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats
static
is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/staticWait I'll sen dss
You call Class.MethodName();
That is only allowed if it is a static method
Yes
You want to access the reference then call CallMethod
It is the same as in Java, except the for-loop looks different
I removed the public in the class now it says abt the protection level
Do you know java?
not entirely, i started the beginner level till using class and stuff
but i really want to learn c# more
java and c# are very similar
First, C# does not use ArrayList, it is legacy and should not be used.
We use List<T> where T is the type
So example
List<int>
will be a list of integersohh, the thing i want to replicate too is that, when i put 3 variable in 1 whole list then add another list for another 3 variable
It is better to describe the problem you want to solve instead of describing how you want to solve a problem you do not mention
ok ok
Anything non-static cannot be accessed globally, meaning it cannot be accessed
Item.Callitems();
You need a reference. Like myItem.Callitems();
In your foreach loop itemz
was the referenceok ill try
Replace ArrayList with List<T>
Where T is the datatype
In your case that would be
List<Items>
Ohhh i can use the class as the
T
Ok ok i get it now
Thank you
i thought i can only use string or something, didnt know i can use the class
No, you can use any datatype.
You did it already in the java code
That is also called generics
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-classes
yeah, its been a while when i did that code so i forgot abt it haha
cuz i thought c# and java is different
thanks guys
C# is influenced by java and delphi
how can i conclude this post?
ohh thats why
Type
/close
if you want to close this threadOk ok thank you so much