Introduction to STM32 Microcontroller

Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at Introduction to STM32 Microcontroller. This microcontroller is a new category of a microcontroller family it is a thirty-two-bit microcontroller also compatible with the ARM and Cortex M processor. Due to thirty-two bits compatibility, this module provides good performance, processing of digital signal and consumes less power and voltage. The modules used in most of the industries due to their compatibility with the cortex M0, M0+, M3, M4.

In today's post, we will have a look at its working, features, pinouts, applications and some other related parameters. So lets with the Introduction to STM32 Microcontroller.

Introduction to STM32 Microcontroller

  • The STM32 is a type of microcontroller that offers the ARM Cortex-M thirty-two bits processor.
  • This type of microcontroller is mostly used in different engineering projects.
  •  This module has numerous series and parallel combinations for the connections so many other electronic devices can be linked with this module such as LCD display, a sensing module, electrical motors, etc.
  • All modules of the STM32 are mounted with interior memory storage and random access memory (RAM).
  • The price of some other STM family modules is higher. And STM series like STM32F0 and STM32F1 is consists of the twenty-four megahertz and sixteen pinouts.
  • The STM32H7 modules use four hundred megahertz and have two forty pinouts at its casing.
  • The sophisticated modules of STM series are used for FPU floating-point units applications where there is need of some special arrangements.

Pinout of STM32 Microcontroller

  • Now we discuss the pinouts of STM32 with the detailed, that is described here with the detail.
Pin names Pin Types:                                         Parameters
IOREF It is power pinout. At his pinout reference, 3,3 volts are applied.
RESET It is power pinout. This pinout is used to reset the controller.
+3.3 volts It is power pinout. At this pinout, 3.3 volts are applied as output that can also be used to give input to the microcontroller.
+5 volts It is power pinout. This pinout is only for five volts outputs.
GND It is power pinout. It is ground pinout.
A0-A1 Analog Pins and I2C At this pinout, analogue voltage is found.
A4 and A5 Analog Pins and I2C At this pinouts, I2C transmission of data is done A4 is SDA (Serial Data) pinout and A5 is SCL (Serial Clock) pinout.
D8-D15 Digital Pins and SPI These are digital GPIO (General Purpose Input/Output) pins.
AVDD Digital Pins and SPI At this pinout, the analogue reference voltage is applied.
GND Digital Pins and SPI It is ground pinout.
D13, D12. D11 and D10 Digital Pins and SPI These pinouts work as SCK (serial clock), MISO (master in slave out), MOSI (master out slave in) and CS pins respectively for SPI communication.
D0 to D7 Digital Pins and USART These pinouts are Digital GPIO (General Purpose Input/Output) pins
D0 and D1 Digital Pins and USART These pins act as Rx (receiver) and Tx (transmission) pins respectively for USART communication.
PC0, PC1, PC2, PC3, PC10, PC11, PC12, PC13, PC14, PC15 These are Port pinouts All these are digital pinouts of port c of the module.
PD2 It is Port pinouts It is the input and output pinout of Port D.
PA0, PA1, PA4, PA13, PA14, PA15 These are Port pinouts All of these are input/output pinouts of Port A.
PB7, PB8 and PB9 These are Port pinouts These are input/output pinouts of PORT B.
PH0 and PH1 These are Port pinouts These are input/output pinouts of port H.
VBAT It is power pinout. This pinout used to provide power to the module form the battery.
+3.3 Volts It is power pinout This pinout provides 3.3 volts as output that can be used to power up the module.
+5V It is power pinout It is five volts output supply pinout.
VIN It is power pinout It is unregulated input power pinout.
RESET It is power pinout It used to Resets the microcontroller.
IOREF It is power pinout This is reference voltage pinout.
PC4, PC5, PC6, PC7, PC8, PC9 These are Port Pinouts These are the input and output pinout of Port C.
PA2, PA3, PA4, PA6, PA7, PA10, PA11 and PA12 These are Port Pinouts These are the Port A inputs and outputs pinouts.
PB1, PB2, PB3, PB4, PB5, PB6, PB8, PB9, PB10, PB12, PB14, PB15 These are Port Pinouts These are the Port B inputs and output Pinouts.
U5V It is power pinout It is five volts power pinout.
GND It is power pinout It used to ground the controller.
U5V It is power pinout It is analog ground pinout.
  • Now we see the pinout diagram of STM32.

Applications of STM32

  • These are some practical applications of STM32 that are described in detail.
  • This module is used in less power consuming hand-held electronic devices.
  • This microcontroller is also used in Robotics and different electronic projects.
  • It is also used in system automation.

It is a detailed tutorial on the STM32 I have explained each and everything related to this microcontroller. I tried my level best to describe this module in the simplest way, but still, if you have any question and query about this module please as in comments. Thanks for reading.  see you in the next tutorial. Have a nice day.

Industries impacted by Virtual Reality

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at Industries impacted by Virtual Reality. Since its introduction to the mainstream a few years ago, VR technology has been raising excitement in anyone interested in fresh ideas. This concept might not be developing on the level we expected from the beginning. However, the potential of Virtual Reality shows great promise for some fantastic things we could be doing in the future, but even now there are many fields and industries where VR is becoming a real game-changer.

Architecture and engineering

There is a lot that VR-based tools can do for engineers, architects, planners, designers, constructors, and so on. It helps them deliver value and make their work easier and better. Just imagine a project of your own house coming to life in front of your eyes in a way the best computer programs won’t be able to match. VR can greatly improve all the stages of the design cycle like the initial concept, modeling, prototyping, reviewing structural weaknesses and similar, as well as serving as an invaluable training tool.

Entertainment

VR in the gaming industry is one of the most promising trends for video game development and a significant stream of revenue for this sector. It is believed that the annual worldwide proceeds from VR will reach 28 billion dollars in 2020, with $4.5 B coming from actual VR games. The technology is getting more recognition in other entertainment branches as well. Some music artists use VR gear to provide a more memorable experience from music concerts. It is also used similarly in theaters, museums, galleries, and amusement parks.

Education

The concept of learning has changed a lot because people can acquire knowledge in different ways. Many of them through the sense of sight. Applying solutions coming from Virtual Reality makes sense here, and it can transform learning processes for students and teachers. It allows them to have a fuller, more immersive experience and come into contact with excellent interactive content.

Tourism and marketing

Virtual Reality is a great way to showcase a product to potential customers to make them want it more. No wonder then that marketing was one of the first industries to embrace the new technology. Travel companies, especially, can find it useful to deliver realistic views of exotic locations, architectural monuments, and exciting places.

Introduction to PT2272

Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at Introduction to PT2272. It is a CMOS (complementary metal-oxide-semiconductor) that use to design simple wireless controlling of instruments installed at some distance. This module is normally used for controlling the circuits of garage door, speed control of the fan, robots and for monitoring the alarm connected at different places for security purposes. This integrated circuitry uses non-variable code address and no built-in encryption due to these parameters it not used for such circuits where high security is need but uses only for small or less cost security circuits such as used in home security alarms etc. In today's post, we will have a look at its working, pinout,  applications and some other related parameters. So let's get started with the Introduction to PT2272.

Introduction to PT2272

  • The PT2272 is a decoder coupled with the PT2262 use the complementary metal-oxide-semiconductor technique.
  • The use of PT2262 with this module to work as a transmitter for the PT2272 that work as a receiver and decodes the data sent by the transmitter.
  • The output pinouts of this module are transistor-transistor logic that can be connected with the other circuitry and microcontroller.
  • The operating voltage of the transmitter (PT2262) is five volts to twelve volts dc and the operation voltage of PT2272 are also five volts dc.
  • This module consists of twelve bits of tri-state address pinout that offers three hundred twelve address codes.
  • This module has features like large no of output data pinouts, latching and temporary output.
  • The complementary metal-oxide-semiconductor technique (CMOS) technique used in this module offered the use of less power, less sound to noise ratio, twelve tri-state address pinouts, six data pinouts many ranges of voltages operation.
  • This integrated circuitry is the finest option for security circuits used in different vehicles like cars, door alarm, robotic controls and some other home automation circuits.

Pinouts of PT2272

  • Now we discuss the pinouts of PT2272 with the detailed, that is described here with the detail.
Pin No: Pin Name:                                         Parameters
A0 to A5 Input pinout These pinouts are address pinouts. All these six pinouts are used to get input coming from the transmitter and then encode the data.
A6/D5 ~ A11/D0 Input and output pinouts These 6 pinouts are used for such data that has a large number of bits and also on the type of PT2272 module used.
DIN Input pinout When these pinouts are operating as inputs, these pins behave as tri-state input pinouts and every pin can set at the three different levels one, zero and floating. If we use these six pinouts as When used as output pins, these pins are driven to VCC if (1) the address decoded from the waveform that was received matches the address setting at the address input pins, and (2) the corresponding data bits received is a “1” bit. Otherwise, they are driven to VSS.
OSC 1 input pinout It is oscillator pinout no one.
OSC 2 Output pinout It is oscillator pinout number two. A specific value of resistance connected among them finds the value of the fundamental frequency of PT2272.
VT Output This pinout is used for the output gain
VCC - At this terminal positive input is provided.
VSS - This terminal is used to provide a negative supply.
NC - This is open pinout.

Features of PT2272

  • These are some features of PT2272 that are described here with the detailed.
  • This module used complementary metal-oxide-semiconductor (CMOS) technique.
  • This module used a small amount of energy for its operation.
  • Its provides larger immunisation from the noise.
  • This module has twelve tri-state code address pinouts.
  • This module has six data pinouts.
  • Its operating volt re from four to fifteen volts.
  • This module also consists of a single resistance oscillator.
  • It is also available in dual in-line package and in small outline package.
  • Its operating span is from five volts to twelve volts.
  • Its input voltage is five volts dc.
  • This module has already assembled light-emitting diode on its assembly.
  • This module also has four outputs of transistor-transistor logic.
  • Its transmitter is dimensions are thirty-six milli-meter by twenty-six milli-meters.
  • Its receiver dimensions are forty-five to twenty-six millimetres.

Absolute Maximum Ratings of PT2272

  • Now we discuss the ratings of PT2272 with the detailed.
Parameter Symbol                                       Rating
Input Voltage Input voltage denoted as VI. The value of these voltages is -0.3 VCC+0.3 volts.
Supply Voltage Denoted as VCC The value of voltage is -0.3 to 16 volts.
Storage Temperature This temperature denoted as Tstg. The temperature value is -65 to 150 Celsius.
Operating Temperature It denoted as Topr. Temperature value from -40  to +85 celsius.
Output Voltage This voltage denoted as VO. This temperature is from -0.3 VCC+0.3 volts.

DC Electrical Features of PT2272

  • Now we discuss the pinouts of PT2272 with the detailed, that is described here with the detail.
Parameter Symbol                    Conditions
Supply Voltage This voltage denoted as VCC.                       -
Stand-by Current This current is denoted as ISB. The conditions for this voltage is VCC is twelve volts. DIN is zero volts. OSC1 is zero volts.
DOUT Output Driving Current It denoted as IOH. The voltage VCC is eight volts. The voltage VOH is four volts.
DOUT Output Sinking Current It is denoted as IOL. The value of VCC is five volts. The value of VOH three volts.
“H” Input Voltage This input voltage is denoted as VIH. VCC
“L” Input Voltage VIL VCC

Applications of PT2272

  • These are some applications of PT2272 described with the detailed.
  • It uses in circuits of a different security system.
  • It is used in circuits of automatic on and off of garage door circuits.
  • It used for the automatic control fan speed circuits.
  • Security circuits of home.
  • It used in different robotics circuits.
  • It used in industries for the control of different machines.
It is the detailed tutorial on the PT2272 if you have any question about it ask in comments thanks for reading.

Introduction to LCD 20x4

Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at the Introduction to 20x4 LCD Module. The LCD stands for liquid crystal display, which works on the light modulation features of liquid crystals. It is available in electronic visible display, video display and flat panel display. There are numerous categories and features are exist in markets of LCD and you can see it on your mobile, laptop, computer and television screen.

The invention of LCD gives new life to electronic industries and replaces lED and gas plasma techniques. It also replaces the CTR (cathode ray) tube that is used for visual display. The input power consumed by the liquid crystal display is less than the light-emitting diode and plasma display. In today's post, we will have a look at 20 x 4 LCD, its features, working, applications, and practical implementation in different electronic devices. So let's get started with the Introduction to 20x4 LCD Module.

Introduction to 20 x 4 LCD Module

  • In a 20x4 LCD module, there are four rows in display and in one row twenty characters can be displayed and in one display eighty characters can be shown.
  • This liquid crystal module uses HDD44780 (It is a controller used to display monochrome text displays) parallel interfacing.
  • The liquid crystal display interfacing code is easily accessible. We just required eleven input and output pinouts for the interfacing of the LCD screen.
  • The input supply for this module is three volts or five volts, with that module other components like PIC, Raspberry PI, Arduino.
  • Thie electronic device can be used in different embedded systems, industries, medical devices, and portable devices like mobile, watches, laptops.
  • Liquid crystal display works on two types of the signal first one is data and the second one is for control.
  • The existence of these signals can be identified through the on and off condition of RS pinout. Data can be read by pushing the Read/write pinout.

20x4 LCD Pinout

  • These are some pinouts of 20x4 LCD modules that are described here in detail.
Pin No: Pin Name:                                         Parameters
Pin#1 It is denoted as Vss It is ground pinout potential at this pinout is zero.
Pin#2 It is denoted as Vdd At this pinout, five volts are provided.
Pin#3 This pinout denoted as Vo This pinout is used to set the contrast of the screen.
Pin#4 This pin denoted as RS It is used to H/L register select signal.
Pin#5 It is denoted as R/W It is used for H/L read/write signal.
Pin#6 This pinout denoted as E It is used for H/L enable signal.
Pin#7-14 The pinouts from seven to fourteen are denoted as DB0 – DB7. It is used for H/L data bus for 4-bit or 8-bit mode.
Pin#15 It identified as A (LED+) It is used to set the backlight anode.
Pin#16 It is recognized as K (LED-). It is used to set the backlight cathode.

Features of 20 x 4 LCD

  • These are some features of 20 x 4 LCD modules that are described here in detail.
  • The most important feature of this module is that it can display 80 characters at a time.
  • The cursor of this module has 5x8 (40) dots.
  • This module already assembled the controller of RW1063.
  • This module operates on the plus five volts input supply and can also work on the plus three volts.
  • The plus 3-volt pinout can also be used for the negative supply.
  • The duty cycle of this module is one by sixteen (1/16).
  • The light-emitting diode of this module can get supply from the pinout one, pinout two, pinout fifteen, pinout sixteen, or pinout A and K.

Electrical Characteristics of 20 x 4 LCD

  • These are some pinouts of 20 x 4 LCD modules that are described here in detail.
Parameters  Symbol                    Conditions
Input Voltage It is denoted as VDD The value of VDD is plus five volts.
Supply Current It denoted as IDD Its value is ten milliamperes.
LC Driving Voltage for Normal Temperature Version Module Its symbol is VDD to V0. Its value is 5.2 volts
LED Forward Voltage It is denoted as VF. Its value is 4.3V
LED Forward Current It is denoted as IF. Its value is 4.6V.
EL Supply Current This pinout denoted as EL VEL = 110 VAC, and four hundred frequency.

Absolute Maximum Ratings

  • Now we discuss the maximum ratings of 20 x 4 LCD.
Parameters  Symbol                    Conditions
Working temperature It is denoted as Top Its value is zero to a plus fifty-celsius degree.
Storage Temperature It is denoted as Tst. Its value is minus twenty Celsius to plus seventy Celsius.
Supply Voltage for Logic It is denoted as Vi. Its minimum value is Vss and the maximum value is equal to Vdd volts.
Supply Voltage for liquid crystal display It is denoted as Vdd or Vss. Its value is three volts to thirteen volts.

Advantages of 20 x 4 LCD

  • These are some advantages of this module that are described in detail.
  • It is less expensive, and lightweight as compared to the cathode ray tube display.
  • It uses less power according to the brightness resolution.
  • It produces less amount of heat due to less use of power.
  • In this module, there is no geometric distortion.
  • It can be constructed in any shape and size according to user requirements.
  • The LCD used in the computer monitor uses twelve volts.

Disadvantages of 20 x 4 LCD

  • Despite the advantages of this module, there are some problems created by this module that are described here.
  • In some older LCD modules, there are some issues due to view angle and brightness.
  • It loses brightness and operates at less response time with the increment of temperature.
  • With the increment of the surrounding temperature, its contrast also disturbs.

It is a detailed tutorial on the 20x4 LCD module I have mentioned everything related to this Liquid crystal display. If you have any questions about it ask in the comments. Thanks for reading.

Introduction to Enums in C#

Hello friends, I hope you all are having fun. In today's tutorial, we will have a look at detailed Introduction to Enums in C#. It's our 23rd tutorial in C# series and this C# concept is quite easy one. It's normally used to give better meanings/understanding to your project. If you haven't studied lectures on C# Classes & C# Methods, then do read them first, as we are going to use them today. So, let's get started with Introduction to Enums in C#:

Introduction to Enums in C#

  • Enums in C# ( short for Enumerations ) is a value type datatype for constants, created by using keyword enum and can be controlled by the class Enum. ( keyword with small e, while class with capital E )
  • We can declare & implement Enums directly in C# Namespace, class or struct and the underlying type of an enum is integer.
  • Enums are strongly typed so we can't implicitly convert them into any other Data Type. Although we can use explicit conversion to convert enums into integers etc. ( We studied it in Data Types Conversions and we will also look at it shortly )
  • Let's understand this enum concept with an example, suppose we are working on some scheduling project and we need to print names of all days.
  • As we know these names are constants, and are also occurring in a sequence, so we can create enums for them, as shown in figure on right side.
  • There are 3 ways to create Enums as you can see in the figure.
  • In the first enum declaration, I have simply added the data in the enum WeekDays. Although, I haven't added any index but still it got the index starting from 0 and then incremented automatically by 1, thorough out the enum, as its underlying type is integer.
  • In the second enum declaration, I have added the index in front of the data, so we can do that as well. Although, there's no difference between first & second implementation, its just second one displays the hidden values.
  • In the third enum implementation, I have assigned random values to constants, so we can do that as well. Instead of values in a sequence, we can assign random values to these Constants.

Why use Enums in C# ?

  • You must be thinking, why we need to use Enums, we can remember such things and can easily replace them with integer checks.
  • Yeah we can do that in simple project but in complex projects and especially in those projects where you don't have a sequence, enums play an important role.
  • Let's say you need to use Elements of Periodic Table in your project, there are too many elements, so in such cases, enum comes quite handy, you can place these elements in right order in your enum and then can use them easily though out your project.

How to use Enums in C# ???

  • Now let's have a look at How to use Enums in C#, so I am creating a simple code where we will check whether it's holiday or working day using enums.
  • As shown in below figure, I have created an Enum named WeekDays and this enum is public so we can use it in other classes.
  • Moreover, enums are value type so, we can't instantiate them in other classes.
  • You can see in above figure that in Main Function, I have created a new variable CurrentDay of Data Type WeekDays, as enum is a Data Type.
  • We can't assign any other value to this variable, we can only assign members of its enum to this variable.
  • In the IF Loop, I have used the members of enum in my conditions to verify.
  • So, now you can see the code has become more Readable & we can easily maintain it without remembering anything.
  • We have studied enum keyword so far, now let's have a look at class Enum:

How to use class Enum in C# ??

  • In Visual studio, you will also find Enum class along with enum keyword. (enum keyword starts with small e while class Enum starts with capital E )
  • We can use this Enum class to get different properties of our enums i.e. we can get their Constants or can also get their underlying integer values.
  • We can also search for any keyword in the Enum, whether it exists or not.
  • I have performed all these three operations in the code, shown in below figure:
  • So, you can see in above figure that we can get Names & Indexes of our enums using this Enum class.
  • I have first used the class name Enum, then dot operator and finally methods from Enum Class.
  • We can also change the underlying type of our enum using ( : ) colon operator.
  • In the right figure,I have changed the underlying type of my enum from integer to short.
  So, that was all about Enums in C#. I hope you understood enums & their importance. In next lecture, we will have  look at Attributes in C#. Till then take care & have fun !!! :)

Introduction to Access Modifiers in C#

Hello friends, I hope you all are having fun. In today's tutorial, we will have a look at Introduction to Access Modifiers in C#. It's our 22nd tutorial in C# series and now it's time to have a look at access modifiers, which we have to use a lot in C#. I hope that you have already studied previous lectures i.e. C# Methods, C# Structs, C# Classes etc. as these access modifiers are used with them. So, let's get started with Introduction to Access Modifiers in C#:

Introduction to Access Modifiers in C#

  • Access Modifiers in C# are used to apply restrictions on the accessibility of C# Objects within or outside the project, and are defined in the declaration.
  • There are 5 Access Modifiers available in C#, whcih are:
    • Private
    • Public
    • Protected
    • Internal
    • Protected Internal
  • We will have a look at all of them, in detail but before going any further, we need to understand the difference between Types & Type Members.
  • All those C# objects, which can contain members in them as called Types i.e. C# Classes, Structs, Interface, Enums etc.
  • While the members of these types are called Type Members i.e. C# Fields, C# Properties and C# Methods.
  • All the C# Types can only be either Public or Internal i.e. we can't make a class private. ( We will discuss it in detail shortly )

1. Private - Access Modifier in C#

  • Private Access Modifier restricts the C# Type Members to be used in the containing class only. ( Containing Class means, the class in which they are created )
  • We can't use Private Members in external classes, as shown in the figure on right side.
  • I have created a Private variable in a new class and now when I am trying to access this variable in Main function, I am unable to do that.
  • In the IntelliSense, there's no property/option to use this private variable.
  • Although, we can use this variable Number1 in HelloWorld class quite easily.

2. Public - Access Modifier in C#

  • Public Access Modifier removes all restrictions from C# Objects and these objects become available in any assembly or project.
  • In order to use Public Objects, we need to follow the proper way i.e. we need to instantiate in order to get the Public Property.
  • In the figure, you can see I have created a public variable Number1 in a new class and then I have assigned a value to it in my Main Method.

3. Protected - Access Modifier in C#

  • Protected Access Modifier restricts the C# Type Members to use only in containing classes or inherited classes.
  • We can't use Protected Members in any independent Class or Struct, it has to be inherited from the Containing Class.
  • Here's an example of Protected Access Modifier in action, and you can see that I can assign value to Protected variable Number1 in the inherited class HelloWorld2.
  • But the independent class Program can't access this Number1 variable.

4. Internal - Access Modifier in C#

  • Internal Access Modifier restricts the C# Objects to be used in the current assembly only.
  • In complex projects, we need to add multiple assemblies i.e. different add-ons and plugins etc.
  • So, Internal variables can be used in the current assembly / project only, we can't access it in external assemblies.
  • We will look at them in detail, in our coming lectures.

5. Protected Internal - Access Modifier in C#

  • Protected Internal Modifier allows the C# Type Members to be accessed in the containing assembly as well as in those classes ( of external assemblies ), which are inherited from the containing class.
  • When we will be done with these basic concepts in C#, then we will design few complex projects and then you will get better understanding of these access Modifiers.
So, that was all about Access Modifiers. In the coming lecture, we will have a look at Enumerations in C#. Till then take care & have fun !!! :)

Introduction to Delegates in C#

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at detailed Introduction to Delegates in C#. It's our 21st tutorial in C# series and now we are entering in the complex field. C# concepts, we have studied so far, were quite simple, but now we are slowly moving towards complex topics. Delegates are not that difficult as normally considered, you just need to understand its basic idea and rest is quite simple. So, let's get started with Introduction to Delegates in C#:

Introduction to Delegates in C#

  • Delegates in C#, created by using delegate keyword, are used as a type safe pointer, normally pointing towards C# Methods & Events.
  • The signature of the Delegate & the function ( its pointing to ) must be same.
  • As Delegate is a reference type Data type, so we can create its new instance.
  • If you want to use any Method as a variable, then Delegates are literally the answer. ( We will have a look at it shortly )
  • Delegates belong to builtin C# class named System.Delegate.
  • Let's have a look at it's syntax:
  • You can see in above figure that I have first declared the delegate, outside the class and I have used delegate keyword in its definition.
  • Now in the Main function, I have created a new instance of this Delegate and in its constructor, I have pointed out the Method Name, to which I want to assign this delegate.
  • After that I have invoked the Method using Delegate, instead of the Method's name itself.
  • You need to make sure that signature of this delegate & the function must be same, when I use the word signature then that means return type and parameters.
  • My Function & delegate, both have void return type, so if I change return type of any one of them, then compiler will generate an error and that's why we call it type safe.
  • Similarly, the number & type of Function Parameters must have to be same for both Delegate & its pointed Method.
  • Let's have a look at MultiCast Delegate in C#:

MultiCast Delegate in C#

  • We can assign / point a single delegate to multiple Methods / Functions, such delegates are called MultiCast Delegates in C#.
  • In the below figure, I have assigned a single delegate to 3 methods and when I invoked the delegate then it has executed all these 3 methods.
  • In the above figure, I have used two ways to chain delegates together, in the first way I have used plus sign ( + ), while in second one I have used append sign ( += ).
  • You must be wondering why to use these function pointers when we can directly invoke the Method using its own name.
  • So, in order to understand the importance of delegates, let's design a simple example:

Example of Delegate in C#

  • Let's create simple code, where we create a new class named StudentsData and it will have 3 C# Properties and 1 C# Method.
  • You can see this code in below figure and I have placed an if loop in my method which has a Boolean expression in it i.e. score < 40.
  • This condition Score < 40 is hard coded in my method, which makes my code quite rigid, whereas its always good practice to add flexibility in your code so that you could reuse it.
  • So, now let's create a new method and add a delegate instead of this Boolean expression and as its a Boolean expression, so my delegate also has to be of Boolean return type.
  • You can see in above figure that I have created a delegate named StudentsDelegate, which is of Boolean return type.
  • After that, I have created a method in Main Function named CheckStatus, which is also of Boolean Type and taking Score as a parameter.
  • If you look closely, the signature of this delegate & method CheckStatus are same.
  • I have used the delegate to point to this method CheckStatus and have placed our failed condition in it.
  • Now this function will return either TRUE or FALSE i.e. it's kind of a Boolean expression.
  • Finally, I have added a delegate as a second parameter in FailedStudents Function, and then used this parameter as a condition in IF Loop.
  • So, now my class is completely flexible, if I wanna change the condition, then I don't need to change the class, I can change score condition in my Main class.
  • Now, you must be thinking, we have done so much coding just to shift a condition from one class to another, we have done it for a purpose.
  • So, let's have a look at the below image, where I have used Lambda Expression in C#:
  • In C#, we can use lambda expressions, which we will study in coming lectures, but for now you can see in above figure that I have removed the delegate instantiation and have also removed the pointed method.
  • Instead I have just placed a Lambda expression and visual studio has created the delegate & method in the background, which we don't need to worry about.
  • We just need to understand this lambda expression where we have placed our condition i.e. Score < 40.
  • Delegates are the basis of this lambda expression, which is quite handy, it has not only made the class flexible but have also reduced the code to just single line.
So, that was all about Delegates in C# and I hope you have understood their importance. In the coming lecture, we will have a look at Exception Handling in C#. Till then take care & have fun !!! :)

Introduction to Properties in C#

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at Properties in C#. It's our 20th tutorial in C# Series and a quite important one as we are gonna use properties a lot in our coming lectures. If you haven't studied C# Methods & C# Classes then I would suggest you to read them first before starting today's tutorial, as we are gonna use methods today. So, let's get started with Properties in C#:

Introduction to Properties in C#

  • Today I am not gonna start with the definition as I did in previous tutorial, instead we will first understand the concept and then will define it.
  • So, let's create a new C# Class named StudentsClass shown in figure on right side.
  • this class has 3 fields in it, first one is int and other two are string and all these three fields are public.
  • Moreover, it has a C# Method in it, which is also public and printing the Full Name.
  • In our Main Function, I have created new instance of this Class and then invoked the Method.
  • So what if, we want to add some restrictions on these fields i.e. we can't have a negative roll number & the name fields can't be null.
  • We don't want garbage values as well, to pass on to our project so its always wise to use private fields instead of public and place some controls on the coming values.
  • We can do this by using C# Methods, as I did in this right image. ( you may need to click the image to look at the zoomed version)
  • So, here I have created two C# Methods, which are receiving values from external classes and then I have placed the check on the coming value and if it satisfies the condition then I have passed it on to my field or variable.
  • Now the field is also private, instead of public.
  • But you must be wondering that its a lot of work to add two functions for every field, so here comes the properties in C#.
  • In the below figure, I have created a C# Property named RollNo for the private field _RollNo and then I have used accessor in C#, which are:
    • set: To save the value of Property.
    • get: To read the value of Property.
  • In the above figure, you can see that I have created a property named RollNo and in that property, I have used set accessor & get accessor.
  • Moreover, in the Main function, now we are treating RollNo as a Property and using dot operator to set or get the value.
  • Instead of using the setRollNo function, we are simply using SC.Roll = 20; to set the Roll no. and compiler will automatically move to set accessor of this property and the value we will assign it i.e. 21 it will be used by the value keyword.
  • You can see in set accessor that, I have used value keyword and this keyword will have the value coming from invoking request i.e. 21 in our case.
  • Similarly, when we want to read the Roll No, we are using SC.RollNo and the compiler will automatically know that it need to move into RollNo Property and then check the get accessor.
  • So, we can say that our property RollNo is a read / write property, if it just has the get accessor then it will be read only property.

Auto Implemented Properties in C#

  • In the above case, we have seen tha we have to place some logic in our set & get accessor, but in most of the cases, we don't need to add any additional logic in our accessors.
  • For example, I am creating fields for City, Phone Number, Email Address etc. then in such cases, we can make use of C# Auto-Implemented Properties which were introduced in C# 3.0.
  • In the below figure, I have created  Properties with get & set accessors:
  • You can see that now I have created 3 new C# Properties and haven't even created any field for them, that's created & implemented automatically by C#.
  • So, when I create a C# Property then its auto-Implemented its private field, which we can control by using set & get accessors, if we want to.
  • So, our whole field vanished & protected and our new Property code is also lies in just one line, so kind of brilliant idea, introduced in C# 3.0.

Object Initializer Syntax

  • Now let's have a look at a new Object Initializer Syntax, which was also introduced in C# 3.0 and I think it's best among all.
  • Let's instantiate our StudentsClass using this new Object Initializer Syntax, as shown in below figure:
  • You can see in above figure that it's now quite simple to assign & get data to & from Class Properties.
  • I am updating all Members of C# Class in just single line, although you can add single property per line as well, but I like it that way, simple and clear.
So, that was all about Properties in C# and I hope now you can set & get them quite easily. In our coming tutorial, we will have a look at Delegates in C#. Till then take care & have fun !!! :)

Introduction to Abstract Classes in C#

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at detailed Introduction to Abstract Classes in C#. It's our 19th tutorial in C# series. Today, we are gonna discuss another new concept in C# and I would recommend you to follow these tutorials at least 3 to 4 times, so that these concepts find the right place in your brain and stick there. C# Abstract Classes are quite similar to C# Interface in many respects, so if you have studied previous tutorial in C# Series, then do read it first. So, let's get started with Introduction to Abstract Classes in C#:

Introduction to Abstract Classes in C#

  • Abstract Classes in C# are created by using Abstract Keyword, and are used to create abstract members, which have their declaration but no implementation (as in C# Interfaces) but unlike Interfaces, Abstract Classes can also have complete methods, properties etc.
  • Abstract Methods in C# are also created by using Abstract Keyword, these methods are only declared in Abstract classes and are then implemented in one of its derived classes & the implementation of the Method must have override Keyword in it.
  • You must be finding these definitions a bit difficult but no need to worry as we will discuss them one by one in detail.
  • The Abstract Modifier / Keyword is used to indicate an incomplete implementation that's why we can't instantiate such objects. We can use it with classes, methods, properties, events and Indexers.
  • Moreover, the class inherited from Abstract Class must have the complete implementation of all the members of Abstract Class, along with override keyword in their declaration. (We will discuss it shortly)
  • It's not compulsory for the Abstract Class to must have some Abstract members, instead we can add only normal members as well.
  • Let's have a look at the syntax of Abstract Class having 1 Abstract Method & 1 normal Method, shown in below figure:
  • In the above figure, we have created a public class with abstract keyword/modifier in it named StudentsAbstract, so it's an abstract class.
  • As StudentsAbstract is an Abstract Class, so we can create both Abstract (Incomplete) members and normal (complete) members in it.
  • So, I have created two methods, printMsg() is an abstract Method as it has Abstract Keyword in its declaration, that's why we haven't placed the implementation of this method.
  • While printMsg2() is a normal method so it's fully implemented in Abstract Class.
  • These Abstract Classes are normally used as a Parent Class in Inheritance and it's kind of their main role. So, let's have a look at Inheritance in Abstract Classes:

Inheritance in C# Abstract Classes

  • As I mentioned earlier, Inheritance is the main role of C# Abstract Classes and they normally play the role of Parent Class and we inherit different classes & structs from them.
  • We can't use the sealed keyword with abstract class, as then we can't inherit any other class. ( We have studied sealed keyword in C# Classes Lecture )
  • Abstract Class can inherit from other Abstract Classes & C# Interfaces. ( Interfaces can't inherit from Abstract Classes )
  • The class inherited from Abstract Class, must have the implementation of all abstract members of its Parent Class, otherwise, we will get a compiler error.
  • In the above figure, we can see that I have added a new class StudentsClass derived from our Abstract Class StudentsAbstract.
  • Now, this new derived class must have the implementation of its Parent's Abstract Members, as I did for printMsg() Method in above figure.
  • Moreover, we need to use override keyword in our derived class method's declaration.
  • So, now we have created an Abstract Class and have also added an Abstract Method in it, after that we have derived a new class and provided implementation of our Abstract Method.
  • Let's now invoke this Abstract Method, declared in Abstract Class & Implemented in Derived Class, shown in below figure:
  • In the Main function, I have simply created a reference variable of Child Class and then invoked both of these methods.
  • You must have noticed that Abstract Classes are quite similar to Interfaces but they have few differences as well, let's have a quick look at them:

Abstract Classes Vs. Interfaces

  • Abstract Classes & Interfaces have a lot in common as both are used for creating incomplete members i.e. declaration only, yet they have some differences as well.
  • We can't create implementation of any member in Interfaces but that's not the case in Abstract Classes, we have the option to create Full members in Abstract Classes.
  • Members in Interfaces can't use access modifier and they are all public by default, but in Abstract Classes we can provide access modifier and can make them private etc.
  • We can't declare C# Fields in Interfaces but that's possible in Abstract Classes.
So, that was all about C# Abstract Classes and I hope you can now differentiate them from Interfaces. In the next lecture, we will have a look at Introduction to Delegates in C#. Till then take care & have fun !!! :)

Introduction to Interface in C#

Hello friends, I hope you all are doing great. In today's tutorial, we will discuss another important concept named Interface in C#. It's our 18th tutorial in C# series. It's one of my favorite concepts in C# as its come quite handy in complex projects. Interfaces are quite similar to C# Classes & C# Structs in design so if you haven't studied those lectures then do read them first. So, let's get started with the Introduction to Interface in C#:

Introduction to Interface in C#

  • Interface in C# is created using Interface Keyword and is used for the declaration of Methods, Properties, Events etc. but we can't add any implementation in it. ( we can't declare Fields in Interface )
  • You can think of Interface as a signature, you look at the interface and you will know all Methods, Properties etc. used in your project.
  • Interface members don't use access modifier in their definitions, by default they all are public.
  • It's a naming convention to use capital ( I ) as the first character of interface name, so that you know, just by looking at the name, that it's an Interface.
  • Let's have a look at the syntax of Interface , before exploring it any further:
  • In the above figure, I have created an interface named IStudentsInterface and it contains declaration of two C# Methods.
  • We can't implement these methods in Interface, as it will generate a compiler error.
  • Now you must be wondering, what's the benefit of using Interfaces, when we can't add the Implementation, and here comes Inheritance:

Interface Inheritance in C#

  • Interfaces are normally used as Parent Node in Inheritance, while C# Classes and C# Structs are inherited from Interfaces.
  • We can inherit a class or struct from multiple Inheritances, separated by commas. (We can't inherit a class from multiple classes)
  • When we inherit a C# Class or Struct from an Interface, then it must provide the implementation for all the Members of that Interface.
  • If we miss implementation of any single member, then compiler will generate an error.
  • We can't create a new instance of Interface as we can do for Classes & Structs, although we can create an Interface Reference variable that will point to its derived class object.
  • So, let's inherit a C# Class from our interface IStudentsInterface and see How it works:
  • In the above figure, I have created an Interface named IStudentsInterface and it has declaration of two Methods i.e. PrintMsg() & StudentsNames().
  • After that, I have inherited a class named StudentsClass from this interface IStudentsInterface .
  • This class must have the implementation of those two methods we declared in our Interface IStudentsInterface.
  • So, I have just created two simple methods with Console ouput, so we remove any one of them, the compiler will generate an error.
  • Let's inherit our class from multiple interfaces, shown in below figure:
  • In the above figure, I have created two C# Interfaces having 1 Method each.
  • Moreover, the class is inherited from both of these interfaces, separated by commas, so it must have the implementation of both of these methods.
  • Similarly, if an interface is inherited from another interface and you are inheriting your class from Child Interface, then still you must have the implementation of all Members of these two Interfaces in your class.
  • Let's have a look at this multiple level inheritance of Interfaces:
  • You can see in above figure, that IStudentInterface2 is inherited from IStudentInterface and our class is inherited from IStudentInterface2. (Like a chain)
  • So, in this case as well, our class must have the implementation of both these methods, otherwise we will get compiler error.
  • This type of Interface Members' Implementation in C# Classes or Structs is called Implicit Interfaces Implementation.
  • So, now let's have a look at Explicit Interfaces Implementation:

Explicit Interfaces Implementation

  • In Explicit Interfaces Implementation, we have to use the Interface Name along with dot operator and then name of the Method in its implementation.
  • An example of Explicit Interfaces Implementation is shown in below figure:
  • As you can see in above figure that our class is inherited from two interfaces.
  • Moreover, in this class I have implemented C# Methods explicitly i.e. now compiler won't need to find which method belongs to which interface.
  • Instead the interface name is present in Methods' definition along with dot operator ( . ).
  • You must have noticed that now we have removed the access modifier and its public by default as of Interface members.
  • When Interface members are implemented explicitly, then we can't access them using  reference variable, instead we need to use cast operator:

( ( IStudentsInterface) SC ) . PrintMsg();

  • So, we need to use above method in order to invoke the Explicitly Implemented Interface Method.
  • There's another way as well, which I have told you at the very top of today's tutorial i.e. we can't create a new instance of Interface yet we can create its reference variable, as shown in below figure:
  • You can see in above figure that in Main function, I have created Interface variables both pointing to its derived class object.
  • Now, as they are interface variables so we can use them to invoke respective explicitly implemented method.
So, that was all about Interfaces in C#. I hope you have completely understood its operation and working. In next tutorial, we will have a look at Abstract Classes in C#. Till then take care & have fun !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir