C# Basics – Random Numbers, Chars and Strings – Part 7

We introduce data types like strings and chars in C# programming language

Andraz Krzisnik
C# Basics – Random Numbers, Chars and...

Random Numbers

To generate a random number, we will use a constructor called Random. Then to generate a random number we will use either .Next() or .NextDouble() function.

Random rnd = new Random();
int r1 = rnd.Next();    //Generates a random positive number
int r2 = rnd.Next(5);     // Generates a random number between 0 and 4
int r3 = rnd.Next(10, 20);    //Generates a random number between 10 and 19
double r4 = rnd.NextDouble();    //Random number between 0.0 and 1.0

Char Data Type

We can store a value to a variable that is just a single symbol by itself. These type of variables are called char – short for character. We can store symbols such as lowercase letters, uppercase letters, all numerals, special symbols like dot or comma and many more. When declaring a char it is important to write with single quotes (Example: ‘a’).

char c = 'a';


Strings are very useful data type, because we almost always work with strings when we work with data that is not numerical. For example names and addresses. But they are useful for more than just that.

How do we declare a string?

It’s important that you put any kind of string value between double quotes.

We can also access each letter in a string like we would access values in an array. So string is an array of each individual characters.

string s = "This is a sentence.";
Download Examples Next Chapter
Show Comments (2)


  • furtdsolinopv

    We’re a group of volunteers and starting a new scheme in our community. Your website provided us with valuable information to work on. You’ve done a formidable job and our entire community will be thankful to you.

    • Article Author
    • Reply
  • Cuckold Cams

    This piece of writing gives clear idea for the new visitors of blogging, that genuinely how to do blogging and site-building.

    • Article Author
    • Reply

Related Articles

C# Tutorial

C# Tutorial: How To Scale Images Without Deforming Them

Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Variables and Data Types – Part 4

Variables and Data Types Software works with data that’s stored in the memory. All this data, to be usable, needs to be stored in some defined space. We name these spaces...

Posted on by Andraz Krzisnik