Core C# Concepts: Arrays, Multithreading, Internal Access, LINQ, and HttpClient
C# Arrays
Arrays in C# are fixed-size data structures that store elements of the same type. They are declared with a specific size and type.
int[] numbers = new int[] { 10, 20, 30, 40, 50 };
int first = numbers[0];
int count = numbers.Length;
C# Multithreading
Multithreading enables concurrent execution in C#. The Thread class and the Task class are commonly used.
System.Threading.Thread thread = new System.Threading.Thread(() =>
{
Console.WriteLine("Thread running");
});
thread.Start();
thread.Join();
Task task = Task.Run(() => Console.WriteLine("Task running"));
task.Wait();
C# internal Keyword
The internal access modifier restricts visbiility to within the same assembly.
internal class RestrictedClass
{
public void DoWork() { }
}
C# LINQ
Language Integrated Query (LINQ) provides a SQL-like syntax for querying collections.
int[] data = { 1, 2, 3, 4, 5 };
var evenValues = from d in data
where d % 2 == 0
select d;
foreach (int v in evenValues)
{
Console.WriteLine(v);
}
C# HttpClient Usage
HttpClient is used to send HTTP requests and receive responses asynchronously.
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/data");
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
}