Reputation: 98
Consider the following program:
using System;
class BooleanType
{
static void Main()
{
bool male = false;
Random random = new Random();
male = Convert.ToBoolean(random.Next(0, 2));
if (male)
{
Console.WriteLine("We will use name John");
} else
{
Console.WriteLine("We will use name Victoria");
}
}
}
Question 1. why is the line bool male = false;
initialised to false?
this seems irrelevant to me as the program later decides if it will be true or false.
Question 2. Random random = new Random();
Why do you have to initialise this to new Random()
, doesn't Random random
create the variable random of type random?
Upvotes: 2
Views: 90
Reputation: 171
The Answer to your first Question is in the code it is initially set as false, so that it doesn't create ambiguity. We know we set what is the default value, the response occurs according to our settings of variables.
And in the 2nd Question you asked why we used the word "new" , instead of a simple variable declaration. So we used the word "new" because Random is a class, and for creating Class instances we must have to use the word new. (These are not simple variables) Hope You got.
Upvotes: -1
Reputation: 27039
Question 1. why is the line bool male = false; initialised to false? this seems irrelevant to me as the program later decides if it will be true or false.
In this case, you are right it is irrelevant. But in some cases variables are initialized to a value because that will be the default. For example, one might do this:
bool overtime = false;
if (hours > 40)
{
overtime = true;
}
In the above there is no else. If hours
are not more than 40 then it will not be overtime.
Question 2. Random random = new Random(); Why do you have to initialise this to new Random(), doesn't Random random create the variable random of type random?
Actually Random random
does not create the variable. It declares a variable named random
of type Random
. The variable is null
by default. Once you do this:
Random random = new Random();
Now the variable is holding a reference to an instance of Random
. In some case people do this if one class derives from another class:
public class ClassA
{
public virtual void DoSomething()
{
Console.WriteLine("Hello");
}
}
public class ClassB : ClassA
{
public override void DoSomething()
{
base.DoSomething();
Console.WriteLine(" World!");
}
}
ClassA a = null;
Later on base on some decision, they may do this:
if (somecondition)
{
a = new ClassB();
}
else
{
a = new ClassA();
}
This gives you polymorphism because the reference is of type ClassA
but the object instance is of type ClassB
or ClassA
depending on the condition. Now you can do this regardless of whether it is an instance of ClassA
or ClassB
:
a.DoSomething();
Depending on the instance, a.DoSomething()
will either write Hello or Hello World!
The benefits are even more obvious when you do something like this:
List <ClassA> list = new List<ClassA>();
list.Add(new ClassA());
list.Add(new ClassB());
foreach (var thisClass in list)
{
thisClass.DoSomething();
}
In the above I am creating a List
of ClassA
and then adding instances of ClassA
and ClassB
and then looping them and calling DoSomething()
on them.
Try it and you will see how flexible this is.
Upvotes: 0
Reputation: 119046
Question 1: You are right that this is a redundant initialiser as the value is almost immediately set a few lines later. In fact, you could simplify the code slightly by combining the declaration and initialisation:
bool male = Convert.ToBoolean(random.Next(0, 2));
The one reason why the code might look the way it does is because C# mandates that all variables must be initialised before they are used and whoever wrote the code is in the habit of initialising everything. From the official docs:
Local variables in C# must be initialized before they are used.
Question 2: The Random
type is not a vlue type, it is a reference type. This means that the default value for a variable of a reference type is always null
. In other words, it is nothing, it doesn't exist and you will get an exception if you try to use it. So you must initialise it by creating an instance of the class with the new
operator.
Upvotes: 3
Reputation: 52280
why is the line bool male = false; initialised to false? this seems irrelevant to me as the program later decides if it will be true or false.
It doesn't have to be. You could declare it as
bool male;
...which means it won't be initialized. Although technically its memory location will be set to false, the compiler won't let you use this variable until you assign it a value; this can be helpful if you want to make sure you actually assign it something later.
Random random = new Random();
Why do you have to initialise this to new Random(), doesn't Random random create the variable random of type random?
If it bothers you a lot, you can also declare it as
var random = new Random();
This may seem less redundant to you.
To the left of the symbol random
you generally will declare a type (in this case, the Random class) and to the right hand side ("RHS") of the equal sign you will generally provide a method for creating an instance of that type (in this case a new
instance, although you could assign it an existing instance). When you use the var
keyword, the compiler will infer the type from the RHS when possible.
Upvotes: 0