In C#, use Any(x => ...) instead of Contains when you need to check a condition, not an exact value match.
// Contains: works for exact values
var ids = new List<int> { 10, 20, 30 };
bool has20 = ids.Contains(20);
// Any: works for conditions
bool hasOver25 = ids.Any(x => x > 25);
Console.WriteLine(has20); // True
Console.WriteLine(hasOver25); // True
In C#, use a for loop instead of foreach when you need the index or when you need to modify the collection safely.
// foreach: index bilgisi yok
foreach (var item in items)
{
Console.WriteLine(item);
}
// for: index gerektiğinde
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine($"{i}: {items[i]}");
}
// for: eleman silme gibi durumlarda daha güvenli
for (int i = items.Count - 1; i >= 0; i--)
{
items.RemoveAt(i);
}
In C#, var should be used when the type is clearly understood from the right side. It improves readability without hiding information.
// Clear and readable: type is obvious
var numbers = new List<int>();
var name = "Alice";
var total = numbers.Count;
// Not recommended: type is not obvious
var result = GetData();
// Recommended alternative
List<User> users = GetUsers();
In C#, const values are fixed at compile time, while readonly values can be assigned at runtime, usually in the constructor.
// const: value must be known at compile time
public const int MaxItems = 10;
// readonly: value can be set at runtime
public readonly int Timeout;
public Settings()
{
Timeout = GetTimeoutFromConfig();
}
In C#, use class when objects are identified by who they are. Use record when objects are identified by their values.
// class: identity-based
class User
{
public string Name { get; set; }
public int Age { get; set; }
}
var u1 = new User { Name = "Alice", Age = 30 };
var u2 = new User { Name = "Alice", Age = 30 };
Console.WriteLine(u1 == u2); // False
// record: value-based
record UserRecord(string Name, int Age);
var r1 = new UserRecord("Alice", 30);
var r2 = new UserRecord("Alice", 30);
Console.WriteLine(r1 == r2); // True
In C#, nameof() returns the name of a variable, property, or method as text. This helps prevent errors caused by hard-coded strings.
int age = 25;
// Old approach
if (age < 0)
{
throw new ArgumentException("age");
}
// Recommended approach
if (age < 0)
{
throw new ArgumentException(nameof(age));
}
Console.WriteLine(nameof(age)); // age
In C#, use Guid.Empty instead of new Guid() when you want an empty GUID. It makes the code clearer.
// Less clear
Guid id1 = new Guid();
// Clear and recommended
Guid id2 = Guid.Empty;
Console.WriteLine(id1 == id2); // True
In C#, use IEnumerable<T> when you only need to read and iterate over data. Use List<T> when you need to add, remove, or access items by index.
// Read-only usage: just iterate
IEnumerable<int> numbers = new[] { 10, 20, 30 };
foreach (var n in numbers)
{
Console.WriteLine(n);
}
// List usage: modify or access by index
List<int> list = new List<int> { 10, 20, 30 };
list.Add(40);
Console.WriteLine(list[0]); // 10
In C#, modifying a collection inside a foreach loop causes runtime errors. If you need to change the collection, iterate over a copy or use a different loop.
// This will cause an error
foreach (var item in items)
{
items.Remove(item);
}
// Safe approach: iterate over a copy
foreach (var item in items.ToList())
{
items.Remove(item);
}
// Alternative: use for loop
for (int i = items.Count - 1; i >= 0; i--)
{
items.RemoveAt(i);
}
In C#, string interpolation ($"...") is more readable and safer than using String.Format.
int a = 18, b = 22;
string text;
// Old approach
text = string.Format("number1: {0}, number2: {1}", a, b);
// Recommended approach
text = $"number1: {a}, number2: {b}";