Learn C# Programming
Master C# programming from basics to advanced concepts with our comprehensive tutorial series. Perfect for beginners and experienced developers.
C# Data Types
C# is a strongly-typed language, which means every variable and expression has a type. Data types specify the type of data that a variable can store.
Value Types
Value types store data directly and are stored on the stack. They include:
1. Integral Types
// Signed integers
sbyte myByte = -128; // 8-bit signed (-128 to 127)
short myShort = -32768; // 16-bit signed (-32,768 to 32,767)
int myInt = -2147483648; // 32-bit signed (-2,147,483,648 to 2,147,483,647)
long myLong = -9223372036854775808L; // 64-bit signed
// Unsigned integers
byte myUByte = 255; // 8-bit unsigned (0 to 255)
ushort myUShort = 65535; // 16-bit unsigned (0 to 65,535)
uint myUInt = 4294967295U; // 32-bit unsigned (0 to 4,294,967,295)
ulong myULong = 18446744073709551615UL; // 64-bit unsigned
2. Floating-Point Types
float myFloat = 3.14f; // 32-bit floating point (7 digits precision)
double myDouble = 3.14159; // 64-bit floating point (15-17 digits precision)
decimal myDecimal = 3.14159m; // 128-bit decimal (28-29 digits precision)
3. Character and Boolean Types
char myChar = 'A'; // 16-bit Unicode character
bool myBool = true; // Boolean (true or false)
Reference Types
Reference types store references to data and are stored on the heap:
1. String Type
string myString = "Hello, World!";
string multiLine = @"This is a
multi-line string";
string interpolated = $"The value is {myInt}";
2. Object Type
object myObject = 42; // Can hold any type
object myStringObject = "Hello";
object myBoolObject = true;
Nullable Types
Value types can be made nullable by adding a ? suffix:
int? nullableInt = null;
bool? nullableBool = null;
double? nullableDouble = 3.14;
// Check for null
if (nullableInt.HasValue)
{
Console.WriteLine($"Value: {nullableInt.Value}");
}
else
{
Console.WriteLine("Value is null");
}
Type Conversion
Implicit Conversion
int myInt = 42;
long myLong = myInt; // Implicit conversion (safe)
double myDouble = myInt; // Implicit conversion (safe)
Explicit Conversion (Casting)
double myDouble = 3.14;
int myInt = (int)myDouble; // Explicit casting (data loss possible)
// Safe conversion with TryParse
string numberString = "123";
if (int.TryParse(numberString, out int result))
{
Console.WriteLine($"Converted: {result}");
}
var Keyword
The var keyword allows implicit typing (compiler determines the type):
var myNumber = 42; // Inferred as int
var myText = "Hello"; // Inferred as string
var myDecimal = 3.14m; // Inferred as decimal
Constants
const int MAX_SIZE = 100;
const string APP_NAME = "MyApp";
const double PI = 3.14159;
💡 Best Practices
- Use
intfor most integer operations - Use
doublefor floating-point calculations - Use
decimalfor financial calculations - Use
stringfor text data - Use nullable types when a value might not be present
- Use
varwhen the type is obvious from the right side
Frequently Asked Questions
Master C# Programming with Our Comprehensive Tutorial
Our C# programming tutorial is designed to take you from a complete beginner to an advanced developer. Whether you're looking to build web applications, desktop software, or mobile apps, C# provides the foundation you need for modern software development.
Start with the basics like variables, data types, and control structures, then progress to advanced topics including object-oriented programming, LINQ, async programming, and more. Each lesson includes practical examples and exercises to reinforce your learning.
Join thousands of developers who have learned C# programming through our step-by-step tutorials. Begin your journey today and unlock the power of .NET development.