عطري وجودك
Well-Known Member
- إنضم
- 5 أغسطس 2019
- المشاركات
- 83,338
- مستوى التفاعل
- 3,631
- النقاط
- 213
اكواد جاهزة بلغة سي شارب: 7 منصات لـ ” احتراف هذه اللغة “

اكواد جاهزة بلغة سي شارب
مجموعة من أكواد C# لغة السي شارب متنوعة:csharpCopy code
كود:
// ====================================
// 1. الأساسيات والمتغيرات والأنواع
// ====================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.Text.Json;
namespace CSharpComprehensiveExamples
{
// مثال على الأنواع المختلفة والمتغيرات
class BasicTypes
{
public static void DemonstrateTypes()
{
// أنواع البيانات الأساسية
int number = 42;
double price = 19.99;
char grade = ‘A’;
bool isActive = true;
string name = “محمد”;
// المصفوفات
int[] numbers = { 1, 2, 3, 4, 5 };
string[] cities = new string[] { “الرياض”, “جدة”, “الدمام” };
// المتغيرات الثابتة
const double PI = 3.14159;
readonly DateTime createdDate = DateTime.Now;
Console.WriteLine($”الاسم: {name}, الرقم: {number}, السعر: {price:C}“);
}
}
// ====================================
// 2. الكلاسات والوراثة
// ====================================
// كلاس أساسي
public abstract class Vehicle
{
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; protected set; }
protected Vehicle(string brand, string model, int year)
{
Brand = brand;
Model = model;
Year = year;
}
public virtual void Start()
{
Console.WriteLine($”تشغيل {Brand} {Model}“);
}
public abstract void DisplayInfo();
}
// كلاس مشتق
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
public string FuelType { get; set; }
public Car(string brand, string model, int year, int doors, string fuel)
: base(brand, model, year)
{
NumberOfDoors = doors;
FuelType = fuel;
}
public override void Start()
{
Console.WriteLine($”تشغيل السيارة {Brand} {Model} بالمفتاح”);
}
public override void DisplayInfo()
{
Console.WriteLine($”سيارة: {Brand} {Model} ({Year}) – {NumberOfDoors} أبواب – {FuelType}“);
}
}
// كلاس آخر مشتق
public class Motorcycle : Vehicle
{
public bool HasSidecar { get; set; }
public Motorcycle(string brand, string model, int year, bool sidecar)
: base(brand, model, year)
{
HasSidecar = sidecar;
}
public override void DisplayInfo()
{
string sidecarInfo = HasSidecar ? “مع عربة جانبية” : “بدون عربة جانبية”;
Console.WriteLine($”دراجة نارية: {Brand} {Model} ({Year}) – {sidecarInfo}“);
}
}
// ====================================
// 3. الواجهات (Interfaces)
// ====================================
public interface IRepository<T>
{
Task<T> GetByIdAsync(int id);
Task<IEnumerable<T>> GetAllAsync();
Task AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(int id);
}
public interface INotificationService
{
void SendNotification(string message);
Task SendNotificationAsync(string message);
}
// ====================================
// 4. النماذج والخصائص
// ====================================
public class Product
{
private decimal _price;
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price
{
get => _price;
set
{
if (value < 0)
throw new ArgumentException(“السعر لا يمكن أن يكون سالباً”);
_price = value;
}
}
public Category Category { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.Now;
public bool IsActive { get; set; } = true;
// خاصية محسوبة
public decimal PriceWithTax => Price * 1.15m;
// Constructor
public Product(string name, decimal price)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Price = price;
}
// Override ToString
public override string ToString()
{
return $”{Name} – {Price:C} ({Category?.Name})”;
}
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Product> Products { get; set; } = new List<Product>();
}
// ====================================
// 5. الاستثناءات (Exceptions)
// ====================================
public class ProductNotFoundException : Exception
{
public int ProductId { get; }
public ProductNotFoundException(int productId)
: base($”المنتج برقم {productId} غير موجود”)
{
ProductId = productId;
}
public ProductNotFoundException(int productId, Exception innerException)
: base($”المنتج برقم {productId} غير موجود”, innerException)
{
ProductId = productId;
}
}
public class ValidationException : Exception
{
public Dictionary<string, string[]> Errors { get; }
public ValidationException(Dictionary<string, string[]> errors)
: base(“فشل في التحقق من صحة البيانات”)
{
Errors = errors;
}
}
// ====================================
// 6. LINQ والعمليات على المجموعات
// ====================================
public class ProductService
{
private readonly List<Product> _products = new List<Product>();
public void LinqExamples()
{
// إضافة بيانات تجريبية
_products.AddRange(new[]
{
new Product(“لاب توب”, 3500) { Category = new Category { Name = “الكترونيات” } },
new Product(“هاتف ذكي”, 1200) { Category = new Category { Name = “الكترونيات” } },
new Product(“كتاب برمجة”, 85) { Category = new Category { Name = “كتب” } },
new Product(“قميص”, 120) { Category = new Category { Name = “ملابس” } }
});
// البحث والتصفية
var expensiveProducts = _products
.Where(p => p.Price > 1000)
.OrderByDescending(p => p.Price)
.ToList();
// التجميع
var productsByCategory = _products
.GroupBy(p => p.Category.Name)
.ToDictionary(g => g.Key, g => g.ToList());
// الإحصائيات
var stats = new
{
TotalProducts = _products.Count,
AveragePrice = _products.Average(p => p.Price),
MaxPrice = _products.Max(p => p.Price),
MinPrice = _products.Min(p => p.Price),
TotalValue = _products.Sum(p => p.Price)
};
// البحث المتقدم
var searchResult = _products
.Where(p => p.Name.Contains(“لاب”) || p.Description?.Contains(“كمبيوتر”) == true)
.Select(p => new { p.Name, p.Price, CategoryName = p.Category.Name })
.ToList();
Console.WriteLine($”إجمالي المنتجات: {stats.TotalProducts}“);
Console.WriteLine($”متوسط السعر: {stats.AveragePrice:C}“);
}
}
7 منصات تعطيك اكواد جاهزة بلغة سي شارب:
1. Microsoft .NET Documentation
https://dotnet.microsoft.com/en-us/learn/csharp- المصدر الرسمي من Microsoft مع دروس مجانية ومقاطع فيديو
2. C# Corner
https://www.c-sharpcorner.com- من أفضل المواقع المتخصصة في C# مع مقالات وأمثلة عملية
3. GitHub – .NET Samples
https://github.com/dotnet/samples- مجموعة ضخمة من أمثلة الكود الرسمية من Microsoft
4. Stack Overflow
https://stackoverflow.com/questions/tagged/c#- الموقع الأشهر لحلول البرمجة مع آلاف الأمثلة العملية
5. W3Schools C#
https://www.w3schools.com/cs/index.php
- دروس مجانية شاملة مع أمثلة تفاعلية
6. GeeksforGeeks
https://www.geeksforgeeks.org/c-sharp-tutorial/- منصة تعليمية شاملة مع أمثلة وشروحات متقدمة