using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace CSharpExamples
{
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
Console.WriteLine("Sum: " + sum);
int number = 4;
if (number % 2 == 0)
{
Console.WriteLine("Even");
}
else
{
Console.WriteLine("Odd");
}
int factorialNumber = 5;
int factorial = 1;
for (int i = 1; i <= factorialNumber; i++)
{
factorial *= i;
}
Console.WriteLine("Factorial: " + factorial);
for (int i = 2; i <= 100; i++)
{
bool isPrime = true;
for (int j = 2; j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine(i);
}
}
string str = "Hello";
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
string reversedStr = new string(charArray);
Console.WriteLine(reversedStr);
int[] numbers = { 1, 2, 3, 4, 5 };
int max = numbers[0];
foreach (int num in numbers)
{
if (num > max)
{
max = num;
}
}
Console.WriteLine("Max: " + max);
List<string> names = new List<string> { "Ali", "Sara", "Reza" };
names.Add("Mina");
names.Remove("Sara");
foreach (string name in names)
{
Console.WriteLine(name);
}
Dictionary<int, string> students = new Dictionary<int, string>
{
{ 1, "Ali" },
{ 2, "Sara" },
};
students.Add(3, "Reza");
foreach (var student in students)
{
Console.WriteLine("ID: " + student.Key + ", Name: " + student.Value);
}
int n = 10;
for (int i = 0; i < n; i++)
{
Console.WriteLine(Fibonacci(i));
}
Console.Write("Enter your name: ");
string nameInput = Console.ReadLine();
Console.WriteLine("Hello, " + nameInput);
DateTime now = DateTime.Now;
Console.WriteLine("Current Date and Time: " + now);
string writePath = @"C:\example.txt";
using (StreamWriter sw = File.CreateText(writePath))
{
sw.WriteLine("Hello, World!");
}
Console.WriteLine("File written successfully.");
string readPath = @"C:\example.txt";
using (StreamReader sr = File.OpenText(readPath))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
MessageBox.Show("Hello, World!");
Program program = new Program();
program.ProcessCompleted += Program_ProcessCompleted;
program.StartProcess();
string json = File.ReadAllText(@"C:\example.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
Console.WriteLine(jsonObj.name);
List<int> nums = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = nums.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
double average = list.Average();
Console.WriteLine("Average: " + average);
Task<int> task = FetchDataAsync();
task.Wait();
Console.WriteLine("Fetched Data: " + task.Result);
}
static int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
public delegate void Notify();
public event Notify ProcessCompleted;
public void StartProcess()
{
Console.WriteLine("Process Started!");
OnProcessCompleted();
}
protected virtual void OnProcessCompleted()
{
ProcessCompleted?.Invoke();
}
private static void Program_ProcessCompleted()
{
Console.WriteLine("Process Completed!");
}
public static async Task<int> FetchDataAsync()
{
await Task.Delay(1000);
return 42;
}
}
}