16.02.2023 C#

Целта ми е да науча C# и да споделя това, което съм научил с хората. Споменах с примери, което е с кодове за коментари.

Ще продължим за C#

  • Нарязани
  • Клас

16.02.2023 C#

Класове с методи

Бележки=Методи, които са полезни за кодиране, тъй като са повторими.

Бележки=Името на класа трябва да бъде PascalCase. това е конвенционално правило в C#

Snipped=можем да използваме кратка дума, за да намерим код, в този случай можем да намерим лесни кодови блокове, които да ни помогнат. Например, когато напишем „prop“ и докоснете+докоснете, натиснете клавиатурата, отколкото можем да видим get и set конструкция, която да ни помогне да дефинираме нещо в класа или т.н.

public int MyProperty { get; set; }   // When you write the prob and tap+tap press

Защо използваме The Class

Когато трябва да използваме различен тип променливи, трябва да използваме класа. Да видим кода.

Пример=когато използваме различен тип променлива, тогава тя не може да бъде прочетена, защото масивите не могат да четат или използват много типове променлива като под тях.

Не използвайте този код само за показване.

String productName = "Apple";
            double price = 15;
            String description = "Apple of Amasya";

            String productName = "Strawberry";      
            double price = 20;
            String description = "High Quality Fruit";
// we can not write the int variables that's why we need to use classes
            String fruits = new string[] { };

Вече изпълнихме класа като продукт

internal class Product //Properties
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Price { get; set; }
        public string Description { get; set; }

    }
Product product1 = new Product();
product1.Name= "Apple";
product1.Price= 15;        //this is class example and we have to execute it
product1.Description = "Apple of Amasya"; 
// we can see clean code at the class type and we can use the array to write screen or do different someting
Product product2 = new Product();
product2.Name = "Strawberry";
product2.Price = 20;
product2.Description = "High Quality Fruit";

Product[] product= new Product[] { product1, product2 };
//Now we can create the Product Array.

Бележки = Масивите съдържат много променливи. Като конструкция.+

int count = 1;
            foreach (Product products in product)  //Product =which class we are working
            {// products=alias , in product we are looping the this array                                     
   Console.WriteLine(count+".)Product Name = " +products.Name);
   Console.WriteLine(count + ".)Product Price = " + products.Price);
   Console.WriteLine(count + ".)Product Description= " + products.Description);
   Console.WriteLine("-------------------------------------------------------");
                count++;
            
            }

Забележка=когато видим мениджър, услуга, контролер, достъп до данни. Това означава, че те държат операциите.

Забележка=(.cs ) файл, който е C# файл.

BasketManager basketManager = new BasketManager(); //this one is instance
basketManager.Add();
basketManager.Add();
basketManager.Add();
//we can use it at the different page.It means reusablity .and when we change we can reach the basketManager Class and you can  change then it will be directly changed.

Забележка=Когато напишем (cw) +Tap+Tap можем да видим Console.WriteLine();

public void Add(Product product) //Basket manager class
{
 Console.WriteLine(" Congralations We added the Basket!");

        }  
BasketManager basketManager = new BasketManager() //main class also this one is instance
basketManager.Add(product1);
basketManager.Add(product1);
//we added function production1 and production2’s informations basketMagnager Class
//we can use different page.It means reusablity
internal class FourTransaction //we constructed the class 
    {
        public void Topla(int num1,int num2)
        {
            int sum=num1+ num2;

            Console.WriteLine("Result = "+ sum);

        }
    }

FourTransaction fourTransaction= new FourTransaction();
            fourTransaction.Topla(5,6);
            fourTransaction.Topla(7,8);
//we called the class for the defined the numbers to sum them and we can change numbers everywhere.
public void Add2(String productName,String information,double price,int stockNum) {
        
        }

            basketManager.Add2("Pear","Green Pear",12,10);
            basketManager.Add2("Apple", "Green Aplle", 12,9);
            basketManager.Add2("Watermelon", "Watermelon of Adna", 12,8);
// we can write like this but this one is not useful because
// when we add new option then we have change and add something and will take many errors.
internal class Product
    {

        public int Id { get; set; }
        public string Name { get; set; }    
        public int Price { get; set; }
        public string Description { get; set; }
        public int StockNum { get; set; }
//but We can use class them and we can add new options .how many options we need,we can add infite options and useful 
// it is name Encapsulation
    }