C
C#11mo ago
drewhosick

Why Do you have to Create an instance of a Class in some cases for methods and not others?

I was trying to program the usual Random Number Generator earlier today as a guessing game. I kept running into an issue because I tried Random.Next(1,10); and it was spitting out an error. I then found an example of the code and it was having me create the line Random rnd = new Random(); and then I could use rnd.Next(1,10). Why is it that I can't use Random.Next but in other situations I can do something like variable.Length(); I'm confused as to why it's making me create an instance of a Random object to use a method but not when using things like Length or Convert.ToInt32();
12 Replies
Sossenbinder
Sossenbinder11mo ago
The key difference is whether the class or method is static or not. That in turn is mostly dependant on whether the respective component contains instance specific state
drewhosick
drewhosick11mo ago
I guess I still feel very new to C# so it seems confusing to me but I'll keep plugging away. Thanks.
Cattywampus
Cattywampus11mo ago
Random.Next means you're accessing the Next method in that Random class. Same thing like:
class MyClass()
{
public void MethodA()
{
Console.WriteLine("Hello, World");
}
}

MyClass instance = new();
instance.MethodA();
class MyClass()
{
public void MethodA()
{
Console.WriteLine("Hello, World");
}
}

MyClass instance = new();
instance.MethodA();
You can see the complete list of methods in Random class here https://learn.microsoft.com/en-us/dotnet/api/system.random?view=net-8.0
drewhosick
drewhosick11mo ago
Makes sense but why can you use methods without creating objects in certain cases like on an array or variable? Is it because the object is already an instance of that type?
Cattywampus
Cattywampus11mo ago
you can do that to statics eg:
class MyClass()
{
public static void MethodA()
{
Console.WriteLine("Hello, World");
}
}

MyClass.MethodA();
class MyClass()
{
public static void MethodA()
{
Console.WriteLine("Hello, World");
}
}

MyClass.MethodA();
drewhosick
drewhosick11mo ago
Ok. Thank you
Cattywampus
Cattywampus11mo ago
aight, goodluck
Pobiega
Pobiega11mo ago
Exactly this. An array is an instance, and a variable points to an instance of some object.
drewhosick
drewhosick11mo ago
thanks
mindhardt
mindhardt11mo ago
And there is no static Random.Next because random instances contain their seed data, so their result can be recreated if necessary (like for tests and stuff)
MODiX
MODiX11mo ago
Hin
REPL Result: Success
var randomOne = new Random(1);
var randomWithSameSeed = new Random(1);

(randomOne.Next(), randomWithSameSeed.Next())
var randomOne = new Random(1);
var randomWithSameSeed = new Random(1);

(randomOne.Next(), randomWithSameSeed.Next())
Result: ValueTuple<int, int>
{
"item1": 534011718,
"item2": 534011718
}
{
"item1": 534011718,
"item2": 534011718
}
Compile: 428.848ms | Execution: 32.385ms | React with ❌ to remove this embed.
mindhardt
mindhardt11mo ago
So randoms created with same seed yield same results But the general recommendation if you want it to really be random is to use Random.Shared.Next() (available in .net 6+ iirc)
Want results from more Discord servers?
Add your server