Related Articles

Back to Latest Articles

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# Basics

C# Basics – Arrays – Part 8

Arrays It’s an often occurrence, that we are dealing with a bunch of data of the same data type. And in case we want to process each value in the same or similar way, arrays come...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Apply Contrast Stretch To An Image

Contrast stretch or otherwise known as normalization is a process where your image’s intensity is changed in such a way, that dark pixels become darker and light pixels...

Posted on by Andraz Krzisnik