C
C#17mo ago
Juliandyce

❔ can someone explain the me the new operator

Especially the syntax behind this example: Random rnd = new Random(); Can u compare the random before the = with int, string and the random after then says that a random number should be generated and new simply says that it should be regenerated every time the program is executed?
29 Replies
Jimmacle
Jimmacle17mo ago
new is used to instantiate objects in order to use a Random, you need to create an instance of it
Juliandyce
JuliandyceOP17mo ago
so can u explain what an instance is.
Jimmacle
Jimmacle17mo ago
it's like having a blueprint of a bike versus actually having a bike new Random() takes the "blueprint" of the Random class definition and creates an actual object that you can call methods and things on you can make as many different Random objects as you want, all with their own individual state
Juliandyce
JuliandyceOP17mo ago
so u can compare the first random with string, int, ...
phaseshift
phaseshift17mo ago
? You can't compare apples to oranges
Jimmacle
Jimmacle17mo ago
you mean like why you don't need new to assign those types of variables?
Juliandyce
JuliandyceOP17mo ago
it defines the variable
Jimmacle
Jimmacle17mo ago
because they have literal representations and are generally treated as values instead of instances of objects yes, variable definitions are all the same [type] [identifier] = [value]
Juliandyce
JuliandyceOP17mo ago
So I define the varialbe as random and then I create it with new
Jimmacle
Jimmacle17mo ago
correct
Juliandyce
JuliandyceOP17mo ago
ty looks like i can?
Jimmacle
Jimmacle17mo ago
what you're asking isn't very clear he intepreted your question as asking if you can literally compare a Random and a string or int with each other (in which case the answer would be no)
Juliandyce
JuliandyceOP17mo ago
ok just to be clear what i "creat" with new ist called instance like when i write int Num = 1 is 1 the instance of num
Jimmacle
Jimmacle17mo ago
no, value types work differently
phaseshift
phaseshift17mo ago
Creating something with 'new' is an instance, yes. 1, or "1" are literals, not instances
Juliandyce
JuliandyceOP17mo ago
whats the differences srry if i ask Stupid questions instances are objects created from a class literals are direct representations of constant values got it
artya
artya17mo ago
The new Random() gives you an instance of the Random class. The random class has methods you can call
Juliandyce
JuliandyceOP17mo ago
Other than that, they are comparable to I think
artya
artya17mo ago
Why do you want to compare the Random?
Juliandyce
JuliandyceOP17mo ago
to understand now i know what new does but tanks for ur version
artya
artya17mo ago
Are you trying to compare like: 1 and a random number?
Juliandyce
JuliandyceOP17mo ago
yes as value
artya
artya17mo ago
Then you have to first generate a random number Which is done by calling the .Next public method of the Random class.
Juliandyce
JuliandyceOP17mo ago
already did public class Program11 { static void Main(string[] args) { Random rnd = new Random(); int secretNum = rnd.Next(1, 2); int numGuessed = 0; do { Console.WriteLine("Enter a number between 1 and 10:"); numGuessed = Convert.ToInt32(Console.ReadLine()); } while (secretNum != numGuessed); Console.WriteLine("You guessed it! It was {0}", secretNum); } }
artya
artya17mo ago
Ah okay, nice!
Juliandyce
JuliandyceOP17mo ago
but ty
artya
artya17mo ago
I thought that you thought that the new Random() was giving you a random number 😛 But it seems you've figured it out!
Juliandyce
JuliandyceOP17mo ago
then u got me i thought next just gives an interval
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server