Mark As Completed Discussion

Working with Variables and Data Types

In .NET, variables are used to store and manipulate data. Each variable has a type, which determines the kind of data it can hold. .NET provides various data types to work with, including:

  • Numeric types, such as int, double, and decimal, for storing numbers.
  • Boolean type, bool, for storing true/false values.
  • Character type, char, for storing single characters.
  • String type, string, for storing sequences of characters.

Let's look at an example that demonstrates the use of different data types in .NET:

TEXT/X-CSHARP
1using System;
2
3namespace LearningCSharp
4{
5    class Program
6    {
7        static void Main(string[] args)
8        {
9            // Example of different data types
10            int age = 25;
11            double weight = 65.5;
12            bool isStudent = true;
13            char grade = 'A';
14            string name = "John Doe";
15
16            // Printing the values
17            Console.WriteLine("Age: " + age);
18            Console.WriteLine("Weight: " + weight);
19            Console.WriteLine("Is Student: " + isStudent);
20            Console.WriteLine("Grade: " + grade);
21            Console.WriteLine("Name: " + name);
22        }
23    }
24}

In this example, we define variables of different data types, such as int, double, bool, char, and string. We assign values to these variables and then print their values to the console using the Console.WriteLine method.

By using the appropriate data types, we can ensure that our variables store the correct type of data and perform operations on them accordingly.

CSHARP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment