C# Basics
Types, variables, control flow and how to write a function (method) in C#, mapped from PHP.
#csharp #dotnet #basics #functions
The everyday syntax you'll reach for, framed against PHP.
Variables & types
Unlike PHP's $var, C# variables have types and no $. You either name the
type or let the compiler infer it with var.
int age = 25; // whole number
double price = 19.99; // decimal number
string name = "Ada"; // text — double quotes only
bool isActive = true; // boolean
var city = "Manila"; // compiler infers `string` — same as writing `string city`
var is inference, not PHP's dynamic typing. var city = "Manila" is
permanently a string — you can't later assign a number to it. It's a shortcut
for the type, not an escape from types.A type followed by ? means the value is allowed to be null:
string? middleName = null; // nullable — this is fine
string lastName = null; // warning: not marked nullable
Output & input (console apps)
Console.WriteLine("Hello"); // print + newline (like echo + "\n")
Console.Write("No newline"); // print, no newline
string? input = Console.ReadLine(); // read a line of input from the user
String interpolation uses $"..." with {} — same idea as PHP's "{$var}":
Console.WriteLine($"Welcome, {name}! You are {age}.");
Control flow
Almost identical to PHP, minus the $:
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
foreach (var item in items) // like foreach ($items as $item)
{
Console.WriteLine(item);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
switch has a modern expression form worth knowing:
string label = status switch
{
"A" => "Active",
"I" => "Inactive",
_ => "Unknown" // _ is the default case
};
Functions (methods)
In C# a function is called a method, and it always lives inside a class. The
big difference from PHP: you declare the return type before the name (or
void if it returns nothing), and each parameter has a type.
// void = returns nothing (like a PHP function with no return)
void Greet(string name)
{
Console.WriteLine($"Hi, {name}");
}
// returns an int
int Add(int a, int b)
{
return a + b;
}
// call them
Greet("Ada");
int total = Add(2, 3); // 5
Comparison at a glance:
| PHP | C# |
|---|---|
function add($a, $b) { return $a + $b; } | int Add(int a, int b) { return a + b; } |
$x = "text"; | string x = "text"; or var x = "text"; |
echo "hi"; | Console.WriteLine("hi"); |
| no return type | return type is required before the name |
GetUser, Login), not
camelCase like PHP methods. Local variables and parameters stay camelCase.Collections quick look
PHP arrays split into a few typed things in C#. The one you'll use most is List<T>:
var names = new List<string> { "Ada", "Alan" };
names.Add("Grace");
Console.WriteLine(names.Count); // 3
Console.WriteLine(names[0]); // "Ada"
Filtering and mapping is done with LINQ — this is your Eloquent Collections equivalent, and it's worth its own note once you're comfortable with the basics.
using System.Linq;
var adults = users.Where(u => u.Age >= 18).ToList(); // like ->filter()
Next
Now that you can write methods, put them on your own types → Classes & models.
C#
Learning C# and .NET from a Laravel/PHP background — the fundamentals, mapped to what you already know.
Classes & Models
Create a class, give it properties and a constructor, and use it — the C# version of a Laravel model.