বিস্তৃত C# চিট শীটটি বিকাশকারীদের কী সিনট্যাক্স এবং C# প্রোগ্রামিং সম্পর্কিত ধারণাগুলি আয়ত্ত করতে সহায়তা করার জন্য ডিজাইন করা হয়েছে।
বিষয়বস্তু
- মৌলিক কাঠামো
- তথ্যের ধরণ
- ভেরিয়েবল
- ধ্রুবক
- শর্তসাপেক্ষ বিবৃতি
- লুপস
- অ্যারে
- তালিকা
- অভিধান
- পদ্ধতি
- ক্লাস এবং অবজেক্ট
- ব্যতিক্রম হ্যান্ডলিং
- প্রতিনিধি, ঘটনা এবং Lambdas
- LINQ (ভাষা-সমন্বিত প্রশ্ন)
- গুণাবলী
- অ্যাসিঙ্ক/অপেক্ষা করুন
- বিবিধ
- স্ট্রিং ম্যানিপুলেশন
- ফাইল I/O
- তারিখ সময়
- জেনেরিক
- শূন্য
- গুণাবলী এবং প্রতিফলন
- এক্সটেনশন পদ্ধতি
- ইনজেকশন নির্ভরতা
- আংশিক ক্লাস
- ইন্টারঅপারেবিলিটি
- বেনামী প্রকার
- টিপলস
- প্যাটার্ন ম্যাচিং
- স্থানীয় ফাংশন
- রেকর্ডস
- এক্সপ্রেশন সহ
- ইনডেক্সার এবং রেঞ্জ
- ঘোষণা ব্যবহার করে
- বাতিলযোগ্য রেফারেন্স টাইপস (NRTs)
- প্যাটার্ন-ভিত্তিক ব্যবহার
- সম্পত্তি নিদর্শন
- ডিফল্ট ইন্টারফেস বাস্তবায়ন
- ডাইনামিক বাইন্ডিং
1. মৌলিক কাঠামো
সমস্ত C# প্রোগ্রাম একটি মৌলিক কাঠামো অনুসরণ করে, নীচে বর্ণিত:
using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
.NET 5 দিয়ে শুরু করে, শীর্ষ-স্তরের বিবৃতি Program.cs বিষয়বস্তুকে সহজ করে:
Console.WriteLine("Hello, World");
2. ডেটা প্রকার
C# বিভিন্ন ধরনের ডেটা সমর্থন করে যেমন:
- মান প্রকার: int, char, এবং float
- রেফারেন্স প্রকার: স্ট্রিং, ক্লাস এবং অ্যারে
3. ভেরিয়েবল
ভেরিয়েবল হল মানগুলির প্রতীকী নাম:
int age = 30; // integer variable string name = "John"; // string variable double PI = 3.14159; // double for floating-point numbers bool isLoggedIn = true; // boolean variable
প্রকার অনুমানের জন্য 'var' ব্যবহার করুন:
var number = 5; // compiler infers type as int var message = "This is a message"; // compiler infers type as string
4. ধ্রুবক
ধ্রুবক অপরিবর্তনীয় মান ধারণ করে:
const double GRAVITY = 9.81; // constant for gravitational acceleration const string COMPANY_NAME = "MyCompany"; // constant company name
5. শর্তসাপেক্ষ বিবৃতি
শর্তের উপর ভিত্তি করে প্রোগ্রাম প্রবাহ নিয়ন্ত্রণ করুন:
int age = 20; if (age >= 18) { Console.WriteLine("You are eligible to vote."); } else { Console.WriteLine("You are not eligible to vote."); } switch (variable) { /*...*/ } // Switch statement
6. লুপ
বারবার কোড চালান:
for (int i = 1; i <= 5; i++) { Console.WriteLine(i); } foreach (var item in collection) { /*...*/ } // Foreach loop while (condition) { /*...*/ } // While loop do { /*...*/ } while (condition); // Do-while loop
7. অ্যারে
উপাদানগুলির নির্দিষ্ট আকারের সংগ্রহ:
string[] names = new string[3] { "Alice", "Bob", "Charlie" }; Console.WriteLine(names[1]); // Output: Bob (accessing element at index 1)
8. তালিকা
অ্যারের অনুরূপ গতিশীল সংগ্রহ:
List<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); foreach (var number in numbers) { Console.WriteLine(number); }
9. অভিধান
ডেটা অ্যাসোসিয়েশনের জন্য কী-মান জোড়া:
Dictionary<string, string> phonebook = new Dictionary<string, string>(); phonebook.Add("John Doe", "123-456-7890"); phonebook.Add("Jane Doe", "987-654-3210"); Console.WriteLine(phonebook["John Doe"]); // Output: 123-456-7890
10. পদ্ধতি
পুনঃব্যবহারযোগ্য যুক্তি এনক্যাপসুলেট করুন:
public class Rectangle { public double Width { get; set; } public double Height { get; set; } public double GetArea() { return Width * Height; } } public class Program { public static void Main(string[] args) { Rectangle rect = new Rectangle(); rect.Width = 5; rect.Height = 10; double area = rect.GetArea(); Console.WriteLine($"Area of rectangle: {area}"); } }
11. ক্লাস এবং অবজেক্ট
ক্লাস অবজেক্টের জন্য ব্লুপ্রিন্ট সংজ্ঞায়িত করে:
public class MyClass // Class definition { public string PropertyName { get; set; } // Properties store data public void MethodName() { /*...*/ } // Methods define actions } MyClass obj = new MyClass(); // Object creation
12. ব্যতিক্রম হ্যান্ডলিং
রানটাইম ত্রুটিগুলি সুন্দরভাবে পরিচালনা করুন:
public static int GetNumberInput() { while (true) { try { Console.WriteLine("Enter a number: "); string input = Console.ReadLine(); return int.Parse(input); } catch (FormatException) { Console.WriteLine("Invalid input. Please enter a number."); } } } public static void Main(string[] args) { int number = GetNumberInput(); Console.WriteLine($"You entered: {number}"); }
13. প্রতিনিধি, ঘটনা এবং Lambda
ইভেন্ট-চালিত প্রোগ্রামিং এবং পদ্ধতি পরিচালনার জন্য:
public delegate void MyDelegate(); // Delegate declaration event MyDelegate MyEvent; // Event declaration public class Person { public string Name { get; set; } public int Age { get; set; } } public static void Main(string[] args) { List<Person> people = new List<Person>() { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 }, new Person { Name = "Charlie", Age = 40 }, }; people.Sort((p1, p2) => p1.Name.CompareTo(p2.Name)); foreach (var person in people) { Console.WriteLine(person.Name); // Output: Alice, Bob, Charlie (sorted by name) } }
14. LINQ (ভাষা-সমন্বিত প্রশ্ন)
ডেটা ম্যানিপুলেশনের জন্য অনুসন্ধান ক্ষমতা:
using System.Linq; public static void Main(string[] args) { List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6 }; var evenNumbers = numbers.Where(x => x % 2 == 0); foreach (var number in evenNumbers) { Console.WriteLine(number); // Output: 2, 4, 6 } }
15. গুণাবলী
কোড উপাদানে মেটাডেটা যোগ করুন:
[Obsolete("Use the new DoSomethingV2 method instead.")] public void DoSomething() { // Implementation here } public void DoSomethingV2() { // New and improved implementation }
16. অ্যাসিঙ্ক/অপেক্ষা করুন৷
নন-ব্লকিং কোড এক্সিকিউশনের জন্য:
using System.Threading.Tasks; public static async Task DownloadFileAsync(string url, string filePath) { // Simulate downloading data asynchronously await Task.Delay(2000); // Simulate a 2-second download // Write downloaded data to the file File.WriteAllText(filePath, "Downloaded content"); Console.WriteLine($"File downloaded to: {filePath}"); } public static void Main(string[] args) { string url = "https://example.com/data.txt"; string filePath = "downloaded_data.txt"; DownloadFileAsync(url, filePath); // Continue program execution while download happens in the background Console.WriteLine("Downloading file..."); Console.WriteLine("Meanwhile, you can do other things..."); }
17. বিবিধ
অতিরিক্ত ভাষার বৈশিষ্ট্য:
- enum, ইন্টারফেস, ক্লাস, রেকর্ড, এবং struct
- গতিশীল, is, as, var, এবং nameof
18. স্ট্রিং ম্যানিপুলেশন
শক্তিশালী স্ট্রিং হ্যান্ডলিং পদ্ধতি:
string.Concat(); // Combine strings string.Join(); // Join elements str.Split(); // Split string str.ToUpper(); // Convert to uppercase str.ToLower(); // Convert to lowercase
19. ফাইল I/O
ফাইলের সাথে অপারেশন:
using System.IO; // Required for File I/O File.ReadAllText(path); // Read file content File.WriteAllText(path, content); // Write to file File.Exists(path); // Check file existence
20. তারিখ ও সময়
তারিখ এবং সময় হেরফের:
using System; public static void Main(string[] args) { DateTime startDate = DateTime.Parse("2024-03-10"); DateTime endDate = DateTime.Now; TimeSpan difference = endDate - startDate; Console.WriteLine($"Time difference: {difference.Days} days, {difference.Hours} hours"); }
21. জেনেরিক
টাইপ-সেফ ডেটা স্ট্রাকচার:
public class Stack<T> { private List<T> items = new List<T>(); public void Push(T item) { items.Add(item); } public T Pop() { T item = items[items.Count - 1]; items.RemoveAt(items.Count - 1); return item; } } public static void Main(string[] args) { Stack<string> messages = new Stack<string>(); messages.Push("Hello"); messages.Push("World"); string message = messages.Pop(); Console.WriteLine(message); // Output: World }
22. শূন্য
মান প্রকারগুলিকে শূন্য করার অনুমতি দিন:
int? nullableInt = null; // Nullable integer
23. বৈশিষ্ট্য এবং প্রতিফলন
মেটাডেটা এবং টাইপ আত্মদর্শন:
public class Person { public string Name { get; set; } public int Age { get; set; } } public static void Main(string[] args) { Type personType = typeof(Person); PropertyInfo[] properties = personType.GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine(property.Name); // Output: Name, Age } }
24. এক্সটেনশন পদ্ধতি
বিদ্যমান ধরনের পদ্ধতি যোগ করুন:
public static class StringExtensions { public static string ToUppercase(this string str) { return str.ToUpper(); } } public static void Main(string[] args) { string message = "Hello, world!"; string uppercased = message.ToUppercase(); // Using the extension method Console.WriteLine(uppercased); // Output: HELLO, WORLD! }
25. নির্ভরতা ইনজেকশন
শিথিলভাবে সংযুক্ত কোড ডিজাইন:
public interface ILogger { void LogMessage(string message); } public class MyService { private readonly ILogger _logger; public MyService(ILogger logger) { _logger = logger; } public void DoSomething() { _logger.LogMessage("Doing something..."); } } // Implementing the ILogger interface (example) public class ConsoleLogger : ILogger { public void LogMessage(string message) { Console.WriteLine(message); } } public static void Main(string[] args) { ILogger logger = new ConsoleLogger(); MyService service = new MyService(logger); service.DoSomething(); }
26. আংশিক ক্লাস
একটি একক শ্রেণীর সংজ্ঞা বিভক্ত করা:
public partial class MyClass { /*...*/ } // Partial class definition
27. ইন্টারঅপারেবিলিটি
অন্যান্য ভাষার সাথে ইন্টারপ করুন:
using System; using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType); public static void Main(string[] args) { MessageBox(IntPtr.Zero, "Hello from C#!", "Interop Example", 0); }
28. বেনামী প্রকার
নামহীন প্রকার তৈরি করা হচ্ছে: csharpCopy কোড
var person = new { Name = "John", Age = 30 }; Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
29. টুপল
একটি নির্দিষ্ট সংখ্যক উপাদান সহ ডেটা কাঠামো:
(string Name, int Age) person = ("Alice", 30); Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); // Accessing elements using Item1 and Item2
30. প্যাটার্ন ম্যাচিং
কিছু প্রোগ্রামিং কাজ সহজ করে:
object obj = new Person { Name = "Bob", Age = 25 }; if (obj is Person { Name: "Bob", Age >= 18 }) { Console.WriteLine("Bob is an adult."); }
31. স্থানীয় কার্যাবলী
পদ্ধতির মধ্যে যুক্তি এনক্যাপসুলেট করুন:
public static int Calculate(int number) { int Factorial(int n) { if (n == 0) return 1; return n * Factorial(n - 1); } return Factorial(number); } public static void Main(string[] args) { int result = Calculate(5); Console.WriteLine($"5! = {result}"); }
32. রেকর্ড
রেফারেন্স প্রকারের জন্য সংক্ষিপ্ত বাক্য গঠন:
public record Person(string Name, int Age); public static void Main(string[] args) { Person person1 = new Person("Alice", 30); Person person2 = new Person("Alice", 30); // Records provide default equality comparison if (person1 == person2) { Console.WriteLine("People are equal"); } }
33. এক্সপ্রেশন সহ
রেকর্ডের জন্য অ-ধ্বংসাত্মক মিউটেশন:
var john = new Person("John", 30); var jane = john with { Name = "Jane" }; // Non-destructive mutation
34. সূচক এবং রেঞ্জ
নমনীয় ডেটা অ্যাক্সেস:
int[] arr = {0, 1, 2, 3, 4, 5}; var subset = arr[1..^1]; // Indexer and range usage
35. ঘোষণা ব্যবহার করে
আইডিপোজেবল বস্তুর নিষ্পত্তি:
using var reader = new StreamReader("file.txt"); // using declaration
36. বাতিলযোগ্য রেফারেন্স টাইপস (NRTs)
শূন্য রেফারেন্স ব্যতিক্রম এড়িয়ে চলুন:
public class Person { public string Name { get; set; } public int Age { get; set; } } public static void Main(string[] args) { Person person = new Person() { Age = 30 }; // NRTs require null checks before accessing properties if (person?.Name != null) { Console.WriteLine(person.Name); } else { Console.WriteLine("Name is null"); } }
37. প্যাটার্ন-ভিত্তিক ব্যবহার
ব্যবহারের বিবৃতিতে আরও নিদর্শন:
public ref struct ResourceWrapper { /*...*/ } // Resource wrapper using var resource = new ResourceWrapper(); // Pattern-based using
38. সম্পত্তি নিদর্শন
প্যাটার্ন মিলে অবজেক্ট ডিকনস্ট্রাক্ট করুন:
if (obj is Person { Name: "John", Age: var age }) { /*...*/ } // Property pattern matching
39. ডিফল্ট ইন্টারফেস বাস্তবায়ন
ডিফল্ট পদ্ধতি বাস্তবায়নের সাথে ইন্টারফেস:
public interface IPerson { /*...*/ } // Interface with default method public class MyClass : IPerson { /*...*/ } // Class implementing interface
40. ডাইনামিক বাইন্ডিং
রানটাইম টাইপ রেজোলিউশন:
dynamic d = 5; // Dynamic binding d = "Hello"; // No compile-time type checking
উপসংহার
এই কাঠামোবদ্ধ C# চিট শীটটি উন্নত বিষয় এবং কৌশলগুলির সাথে সমাপ্ত হয়, যা তাদের C# প্রোগ্রামিং দক্ষতা বাড়ানোর লক্ষ্যে বিকাশকারীদের জন্য একটি ব্যাপক রেফারেন্স প্রদান করে। বিস্তারিত উদাহরণ এবং আরও অন্বেষণের জন্য, এই নির্দেশিকায় বর্ণিত নির্দিষ্ট বিভাগগুলি পড়ুন। শুভ কোডিং!
C# প্রোগ্রামিং🚀
C# সম্প্রদায়ের অংশ হওয়ার জন্য আপনাকে ধন্যবাদ! তুমি ত্যাগ করার পূর্বে:
আপনি যদি এটি এতদূর তৈরি করে থাকেন, তাহলে অনুগ্রহ করে একটি হাততালি দিয়ে আপনার প্রশংসা দেখান এবং লেখককে অনুসরণ করুন! 👏️️
আমাদের অনুসরণ করুন: এক্স | লিঙ্কডইন | Dev.to | হ্যাশনোড | নিউজলেটার | টাম্বলার
আমাদের অন্যান্য প্ল্যাটফর্মগুলি দেখুন: GitHub | ইনস্টাগ্রাম | টিকটক | Quora | দৈনিক.দেব
দ্বারা অনুপ্রাণিত: https://zerotomastery.io/cheatsheets/csharp-cheat-sheet/#constants
এছাড়াও এখানে প্রকাশিত