HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
var / constint.Parse() / ToString()
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›REFERENCE›Variables & Types
📦

Variables & Types

var, const, and type conversion.

CommandDescription
var / constVariables and immutable constants.
int.Parse() / ToString()Text ↔ number conversion.

var / const

Try it yourself →

Variables and immutable constants.

Syntax

var name = value;   //   const Type NAME = value;

Description

With var, the type is inferred from the value, and the value can change later. const is a compile-time constant and cannot be reassigned. You can also explicitly write the type (int, double, string, bool).

Example

using System;
var name = "C#";
int score = 10;
score = 20;
Console.WriteLine(name);
Console.WriteLine(score);

Expected output

C#
20

When to use?

General variables; use const for values that shouldn't change.

Common mistakes

  • ⚠️ Trying to reassign a const constant causes a compilation error.

int.Parse() / ToString()

Try it yourself →

Text ↔ number conversion.

Syntax

int.Parse(text)   //   number.ToString()

Description

int.Parse converts text to an integer, and double.Parse converts it to a decimal. ToString converts a number to text. Unconvertible text throws an error.

Example

using System;
int n = int.Parse("25");
Console.WriteLine(n + 5);
string s = (42).ToString();
Console.WriteLine(s + "!");

Expected output

30
42!

When to use?

Accounting for text coming from users/files.

Common mistakes

  • ⚠️ int.Parse("abc") throws an error; TryParse is safer.
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code