16.02.2023 C#

Моя цель — изучить C# и поделиться с людьми тем, что я узнал. Я упомянул примеры с кодами комментариев.

Мы продолжим о C#

  • Снято
  • Сорт

16.02.2023 C#

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

Примечания=Методы, полезные для кодирования, поскольку они воспроизводимы.

Примечания= Имя класса должно быть PascalCase. Это правило соглашения в C#.

Snipped= мы можем использовать короткое слово для поиска кода, в этом случае мы можем легко найти блоки кода, которые помогут нам. Например, когда мы пишем «prop» и нажимаем + нажимаем на клавиатуру, мы видим получить и установить конструкцию, чтобы помочь нам определить что-то в классе или т.д..

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++;
            
            }

Примечание= когда мы видим Менеджер, Сервис, Контроллер, Доступ к данным. Это означает, что они содержат Операции.

Note=(.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
    }