SlideShare a Scribd company logo
1 of 10
Lesson 4
Learn C#. Series of C# lessons
http://csharp.honcharuk.me/lesson-4
Agenda
• Classes
• Functions/Methods
• Properties
Class example
• Constructors
• Static constructors
• Access modifiers
• static
Constructor
• Constructors are used to create and initialize any object member
variables when you use the 'new' expression to create an object of
a class.
Functions/Methods
• A function allows you to encapsulate a piece of code and call it from
other parts of your code.
<visibility> <return type> <Name>(<parameters>)
{
<function code>
return <returned object>;
}
Preperties
• A property is a member that provides a flexible mechanism to read,
write, or compute the value of a private field.
class User
{
private string _country;//backed field
public string FirstName { get; set; }//backed field will be created automatically
public string LastName { get; set; }
public string FullName => FirstName + " " + LastName; //Read only property
public bool IsInitialized // ReadOnly property
{
get { return !string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName); }
}
public string Country//property using backed field
{
get { return string.Format("I live in {0}", _country); }
set { _country = value; }
}
}
Access modifiers
• public: access is not restricted
• private: access is limited to the containing type
• protected: access is limited to the containing class or types derived
from the containing class
• internal: access is limited to the current assembly
• protected internal: access is limited to the current assembly or types
derived from the containing class
static
• static function
• static property
• static class
class Animal
{
public static int AnimalCount { get; private set; }
private string Name;
public Animal()
{
Name = "No name defined.";
AnimalCount++;
}
public Animal(string name)
{
Name = name;
AnimalCount++;
}
public void PrintName()
{
Console.WriteLine(Name);
}
public static void GetInfo()
{
Console.WriteLine("My mission is to be an animal");
}
}
Animal.GetInfo(); //prints message
Animal a1 = new Animal();
Animal a2 = new Animal("Cow");
Animal a3 = new Animal();
a2.PrintName();//prints Cow
a3.PrintName();// pring 'no name defined' message
Console.WriteLine(Animal.AnimalCount); // outputs 3
Static constructor
• A static constructor is used to initialize any static data, or to perform a
particular action that needs to be performed once only. It is called
automatically before the first instance is created or any static
members are referenced.
Thank you!
Questions?

More Related Content

What's hot

2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
pedro.carvalho
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
Tran Khoa
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part  1JavaScript: Patterns, Part  1
JavaScript: Patterns, Part 1
Chris Farrell
 

What's hot (20)

Pharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntaxPharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntax
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
 
Python Metaclasses
Python MetaclassesPython Metaclasses
Python Metaclasses
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Core Java
Core JavaCore Java
Core Java
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryLearn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
 
Introduction to Type Script by Sam Goldman, SmartLogic
Introduction to Type Script by Sam Goldman, SmartLogicIntroduction to Type Script by Sam Goldman, SmartLogic
Introduction to Type Script by Sam Goldman, SmartLogic
 
Best Guide for Javascript Objects
Best Guide for Javascript ObjectsBest Guide for Javascript Objects
Best Guide for Javascript Objects
 
Java
JavaJava
Java
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
A glimpse into protocols with associated type and type erasure
A glimpse into protocols with associated type and type erasureA glimpse into protocols with associated type and type erasure
A glimpse into protocols with associated type and type erasure
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part  1JavaScript: Patterns, Part  1
JavaScript: Patterns, Part 1
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
 
Java Advanced Features
Java Advanced FeaturesJava Advanced Features
Java Advanced Features
 
Prototype
PrototypePrototype
Prototype
 
Session 15 - Collections - Array List
Session 15 - Collections - Array ListSession 15 - Collections - Array List
Session 15 - Collections - Array List
 

Viewers also liked

Viewers also liked (13)

Portfólio serviços psrv
Portfólio serviços psrvPortfólio serviços psrv
Portfólio serviços psrv
 
Alejandro serrato
Alejandro serratoAlejandro serrato
Alejandro serrato
 
Biotesty
BiotestyBiotesty
Biotesty
 
Skrillex
SkrillexSkrillex
Skrillex
 
Lesson11
Lesson11Lesson11
Lesson11
 
Lesson8
Lesson8Lesson8
Lesson8
 
La théorie des rôles en équipe de Belbin.
La théorie des rôles en équipe de Belbin.La théorie des rôles en équipe de Belbin.
La théorie des rôles en équipe de Belbin.
 
Tool Wear.Rep
Tool Wear.RepTool Wear.Rep
Tool Wear.Rep
 
Living in the future
Living in the futureLiving in the future
Living in the future
 
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...
 
Stress management techniques suggested by custom soft
Stress management techniques  suggested by custom softStress management techniques  suggested by custom soft
Stress management techniques suggested by custom soft
 
Cloud Computing for Startups
Cloud Computing for StartupsCloud Computing for Startups
Cloud Computing for Startups
 
Puzzles sh pandillas vertical
Puzzles sh pandillas verticalPuzzles sh pandillas vertical
Puzzles sh pandillas vertical
 

Similar to Lesson 4

Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
zand3rs
 
Defining classes-part-i-constructors-properties
Defining classes-part-i-constructors-propertiesDefining classes-part-i-constructors-properties
Defining classes-part-i-constructors-properties
CtOlaf
 

Similar to Lesson 4 (20)

14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Oops
OopsOops
Oops
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
VB.net&OOP.pptx
VB.net&OOP.pptxVB.net&OOP.pptx
VB.net&OOP.pptx
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Defining classes-part-i-constructors-properties
Defining classes-part-i-constructors-propertiesDefining classes-part-i-constructors-properties
Defining classes-part-i-constructors-properties
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 

Recently uploaded

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Recently uploaded (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

Lesson 4

  • 1. Lesson 4 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-4
  • 3. Class example • Constructors • Static constructors • Access modifiers • static
  • 4. Constructor • Constructors are used to create and initialize any object member variables when you use the 'new' expression to create an object of a class.
  • 5. Functions/Methods • A function allows you to encapsulate a piece of code and call it from other parts of your code. <visibility> <return type> <Name>(<parameters>) { <function code> return <returned object>; }
  • 6. Preperties • A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. class User { private string _country;//backed field public string FirstName { get; set; }//backed field will be created automatically public string LastName { get; set; } public string FullName => FirstName + " " + LastName; //Read only property public bool IsInitialized // ReadOnly property { get { return !string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName); } } public string Country//property using backed field { get { return string.Format("I live in {0}", _country); } set { _country = value; } } }
  • 7. Access modifiers • public: access is not restricted • private: access is limited to the containing type • protected: access is limited to the containing class or types derived from the containing class • internal: access is limited to the current assembly • protected internal: access is limited to the current assembly or types derived from the containing class
  • 8. static • static function • static property • static class class Animal { public static int AnimalCount { get; private set; } private string Name; public Animal() { Name = "No name defined."; AnimalCount++; } public Animal(string name) { Name = name; AnimalCount++; } public void PrintName() { Console.WriteLine(Name); } public static void GetInfo() { Console.WriteLine("My mission is to be an animal"); } } Animal.GetInfo(); //prints message Animal a1 = new Animal(); Animal a2 = new Animal("Cow"); Animal a3 = new Animal(); a2.PrintName();//prints Cow a3.PrintName();// pring 'no name defined' message Console.WriteLine(Animal.AnimalCount); // outputs 3
  • 9. Static constructor • A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Editor's Notes

  1. https://msdn.microsoft.com/en-us/library/k6sa6h87.aspx
  2. https://msdn.microsoft.com/en-us/library/ms173114.aspx http://csharp.net-tutorials.com/basics/functions/
  3. https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
  4. https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx
  5. https://msdn.microsoft.com/en-us/library/98f28cdx.aspx https://msdn.microsoft.com/en-us/library/79b3xss3.aspx
  6. https://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx