Monthly Archives: May 2021

THE STRUCTS IN C SHARP

using System; Persona p = new Persona("Mario","Rossi"); Console.WriteLine(p.Denominazione); struct Persona { private string nome; private string cognome; public Persona(string nome, string cognome) { this.nome = nome; this.cognome = cognome; } public string Denominazione => this.nome + " " + this.cognome; } THE STRUCTS IN C SHARP Structs in c sharp are structures similar to classes, but there are also profound differences that we will explore in a bit. A struct can contain variables, methods, properties. The first difference that jumps out at you when looking at the code below is the struct keyword instead [...]

By |2024-07-31T21:21:04+02:0010 May 2021|0 Comments

THE ENUMERATIONS IN C SHARP

ENUMERATIONS IN C# The enumerations in C Sharp are also types introduced by us such as classes and structs and like the latter they are value types. Enums are data structures that the language makes available to us to improve the readability and security of our programs. They allow us to choose from a series of well-predefined values. Suppose our code handles the days of the week, let's see how this enum is declared. using System; namespace Enumerazioni { enum GiorniSettimana{Lunedi,martedi,mercoledi,giovedi,venerdi,sabato,domenica}; class Program { static void Main(string[] args) { } } } As you can see, [...]

By |2021-08-27T02:34:47+02:0010 May 2021|0 Comments

INHERITANCE IN C SHARP

BASIC CLASSES AND DERIVED CLASSES Inheritance in c sharp is the second pillar of Object Oriented programming. Let us try to clarify the mechanism with a figure. In C#, classes are the data structures that allow us to manage superclasses and subclasses, and therefore to implement the inheritance mechanism. Only they support inheritance, struct and enum that we have already seen do not implement this mechanism. Let's see programmatically how an inheritance hierarchy is defined: EXAMPLE OF INHERITANCE CHAIN using System; namespace Ereditarieta { class Program { static void Main(string[] args) { } } public class [...]

By |2024-08-03T00:14:52+02:0010 May 2021|0 Comments

POLYMORPHISM IN C SHARP

THE POLYMORPHISM IN C# Polymorphism in c sharp is the third pillar of Object Oriented programming, literally it means “having many forms” it means in practice that starting from a base class that has a certain behavior, we can redefine this behavior in its derived classes. Let's see the theory behind polymorphism with some images. POLYMORPHISM IN C SHARP THE OVERRIDE METHODS VIRTUAL AND OVERRIDE Let's move from theory to practice by looking at what are the mechanisms that the C# language makes available to us to implement polymorphism in C Sharp. [...]

By |2024-08-03T14:48:06+02:0010 May 2021|0 Comments

THE INTERFACES IN C SHARP

INTERFACE C sharp interfaces are like reference types classes. Interfaces are similar to abstract classes that is, they contain the definition of methods, properties and other members that are not implemented. C# does not support multiple inheritance like other languages such as Pyton, C++ but only one class can be derived. However, the language allows us to implement multiple interfaces. IMPLEMENT AN INTERFACE But what does it mean to implement an interface? A class that claims to implement an interface is required to implement all the members, properties, methods and events that have been defined in it. It is [...]

By |2024-08-04T14:59:22+02:0010 May 2021|0 Comments

THE EXCEPTIONS IN C SHARP

EXCEPTIONS IN C SHARP Until now, in our programs we have assumed that the execution was carried out without errors. However, regardless of the skill of the code writer, error situations can occur simply because the reasons for this are not always predictable or manageable: a method can raise an error due to a developer oversight, but also in response. a hardware problem or attempting to access an unavailable resource. Now let's see the mechanism that .NET 8 makes available to limit the errors that can occur. To analyze the exceptions in c sharp we start with a small [...]

By |2024-08-05T00:35:28+02:0010 May 2021|0 Comments

THE GENERICS IN C SHARP

INTRODUCTION TO GENERICS In C # there are two categories in which we can apply the Generics mechanism. Generic types and methods. Generic types (class, interface, struct, delegate) Generic Methods With generics in C Sharp we can define type parameters in Generic Types or Generic Methods. Conventionally they are indicated with the letter T, which is simply a placeholder that allows us to postpone the creation of real objects or the invocation of a method with a type that will be introduced when we use generic types or generic methods. OVERLOAD METHODS using System; namespace Generics [...]

By |2024-08-05T08:56:55+02:0012 May 2021|0 Comments

THE DELEGATES IN C SHARP

THE DELEGATE C Sharp delegates are a new type after classes, structs and interfaces. It is a reference type whose instances allow us to treat methods as if they were objects. A delegate has no implementation, it can be defined at the namespace level but also at the class level. Let's see how a delegate declares. namespace Delegate {      public delegate int MyDelegate(int a, int b); } As you can see a delegate declares itself with the keyword delegate, otherwise it looks like the definition of a method, we have declared an int return type and two [...]

By |2024-08-05T17:59:39+02:0011 May 2021|0 Comments

GENERIC COLLECTION IN C #

GENERIC COLLECTION LIST<T> List<T> is a set of strongly typed objects that can be accessed by index and by methods for sorting, searching and editing the list. using System; using System.Collections.Generic; namespace CollectionGeneriche { class Program { static void Main(string[] args) { Console.Write("***************************************************************"); Console.Write("\n"); Console.WriteLine("LIST<T>"); //LIST<T> //Aggiungo elementi usando metodo Add List<string> city = new List<string>(); city.Add("Milano"); city.Add("Torino"); city.Add("Roma"); string[] bigCity = new []{"New York","London","Chicago"}; //Aggiungo elementi usando metodo AddRange city.AddRange(bigCity); //Aggiungo elementi usando la collection-initializer syntax List<string> city1 = new List<string>(){"Napoli,Firenze,Perugia"}; //Accedo agli elementi con gli indici, si può usare la struttura Index string prima [...]

By |2024-08-06T00:32:10+02:0013 May 2021|0 Comments

ASYNCHRONOUS PROGRAMMING IN C SHARP

ASYNCHRONOUS PROGRAMMING In asynchronous programming in c sharp there are synchronous methods and asynchronous methods. Let's try to understand first of all when to perform operations asynchronously and when not. In general we can say that we must resort to asynchronous operations when we have to access I/O peripherals such as reading from the disk or working with the network. Connecting and sending queries to a database is an asynchronous operation, reading a large text file, sending a request to a web service, sending an email are all asynchronous operations. Conversely, there is no benefit when a method is [...]

By |2024-08-06T07:52:44+02:0013 May 2021|0 Comments
Go to Top