🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME 01POSTS 02COURSES 03ELECTRİCAL_ENERGY 04AUTOMOTİVE_ENGİNEERİNG 05TOOLS 06LAB 07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

Variables and Data Types

Learn the int, double, decimal, bool, char and string types, plus var, const and nullable variables, with an Ohm's law example.

What is a variable?

A variable is a named box that stores a value in memory. C# is a type-safe language: every box has a type, and you can only put a value of that type into it. This lets you catch mistakes before the program even runs.

int age = 24;
double voltage = 12.5;
string name = "Fatih";
bool isConnected = true;
char grade = 'A';

A declaration follows this pattern: type name = value;. The assignment sign = does not mean "equals"; it means "put the value on the right into the box on the left".

Commonly used types

TypeWhat it storesExample
intWhole number (about ±2.1 billion)42
longLarger whole number9000000000L
doubleDecimal number (about 15 digits of precision)3.14
floatSmaller decimal number (7 digits)3.14f
decimalDecimal number that needs exact precision, such as money19.99m
boolTrue/falsetrue
charA single character'A'
stringText"Hello"

In engineering calculations you will usually use double. For money calculations, decimal is preferred, because double works in binary and cannot store some decimals exactly (for example 0.1).

Your first calculation with Ohm's law

double voltage = 12.0;   // volts
double resistance = 470; // ohms
double current = voltage / resistance;

Console.WriteLine($"Current: {current:F4} A");
Current: 0.0255 A

{current:F4} prints the number with 4 digits after the decimal point. The decimal separator depends on your computer's regional settings: with English (US) settings the output is 0.0255, while on a machine set to Turkish you would see 0,0255.

var: let the compiler find the type

Where the type is obvious from the right-hand side, you can use var. The type is still fixed; you just leave the typing to the compiler.

var number = 10;          // int
var name = "Ege";         // string
// number = "ten";        // ERROR: number is now an int, text cannot be assigned

const: a value that never changes

const double Pi = 3.14159265;
const double ElectronCharge = 1.602e-19; // coulomb

double radius = 2;
Console.WriteLine(Pi * radius * radius);

You cannot change the value of a constant later. Scientific notation works too: 1.602e-19 means 1.602 times 10 to the power of -19.

Nullable types

Sometimes a value is not known yet: for example, a sensor did not respond. If you add ? to the end of the type, the variable can also hold null (no value at all):

double? temperature = null;          // not measured yet
Console.WriteLine(temperature.HasValue);   // False

temperature = 23.5;
Console.WriteLine(temperature.HasValue);   // True
Console.WriteLine(temperature.Value);      // 23.5

Naming: Write variable names starting with a lowercase letter and capitalize the first letter of each following word (measurementCount). Choose meaningful names: voltage instead of x. Non-English letters are allowed in names, but many people prefer plain ASCII.

About the text (string) type

string is immutable: operations that seem to change a piece of text actually produce a new one. You can use ready-made members such as name.ToUpper() or name.Length. Interpolation ($"...") is the most readable way to join text. To compare two strings you can use ==; in that case C# compares their contents.

Try it yourself

Calculate the series resistor for an LED: supply voltage 5 V, LED voltage 2 V, current 0.02 A. The formula is R = (Vsupply - Vled) / I. Declare the variables as double and print the result in the form "Resistance: 150 Ω". Then change the current to 0.01 A and see how the result changes.

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. Which type is preferred for calculations that need exact precision, such as money?
2. What happens if you write number = "ten"; after the line "var number = 10;"?
3. What does the ? mean in the expression double? temperature = null;?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.