SlideShare a Scribd company logo
1 of 20
Download to read offline
Objects and Types C#
   A class is a construct that enables you to
    create your own custom types by grouping
    together variables of other types, methods
    and events.
   A class is like a blueprint. It defines the data
    and behavior of a type.
   Syntax for creating a class
      [attributes] [modifiers] class identifier [:base-list]
       { class-body }
   Ex:
      public class Customer { //Fields, properties, methods and
       events go here... }

                                                                Contd…
   The data and functions within a class are
    known as the class’s members.
   Data Members
      Data members are those members that contain the data for
       class – fields, constants and events.
      Data members can be static.
      Fields are any variables associated with the class.
      Events are class members that allow an object to notify a
       caller whenever something of interest occurs.
   Function Members
      Function members are those members that provide
       some functionality for manipulating the data in the
       class.
      These include methods, properties, constructors,
       finalizers, operators, and indexers.
   A struct type is a value type that can contain
    constructors, constants, fields, methods, properties,
    indexers, operators, events, and nested types.
   The declaration of a struct takes the following form:
      [attributes] [modifiers] struct identifier [:interfaces] {//body }

   Structs share most of the same syntax as classes,
    although structs are more limited than classes:
   Within a struct declaration, fields cannot be initialized
    unless they are declared as const or static.
   A struct cannot declare a default constructor (a
    constructor without parameters) or a destructor.
   Structs are value types and classes are
    reference types.
   Unlike classes, structs can be instantiated
    without using a new operator.
   Structs can declare constructors that have
    parameters.
   A struct cannot inherit from another struct or
    class, and it cannot be the base of a class. All
    structs inherit directly from
    System.ValueType, which inherits from
    System.Object.
   A struct can implement interfaces.
   A struct can be used as a nullable type and
    can be assigned a null value.
   A method is a code block that contains a series of
    statements.
   In C#, every executed instruction is performed in
    the context of a method.
   Declaring Methods
    ◦ Syntax
       [modifiers] return-type MethodName ([parameters])
         {
         //method body
         }




                                                           Contd…
   Passing Parameters to Methods
    ◦ Parameters can be passed into methods by reference or by value.
    ◦ In C#, all parameters are passed by value unless you specifically
      say otherwise.
   ref Parameters
    ◦ The ref keyword causes arguments to be passed by reference.
      The effect is that any changes to the parameter in the method
      will be reflected in that variable when control passes back to the
      calling method.
   out Parameters
    ◦ The out keyword on a method parameter causes a method to
      refer to the same variable that was passed into the method.
      Any changes made to the parameter in the method will be
      reflected in that variable when control passes back to the
      calling method.


                                                               Contd…
   Passing by Reference vs. Passing by Value

      When a value type is passed to a method, a copy is
       passed instead of the object itself. Therefore, changes
       to the argument have no effect on the original copy in
       the calling method. You can pass a value-type by
       reference by using the ref keyword.

      Reference types are passed by reference. When an
       object of a reference type is passed to a method, the
       reference points to the original object, not a copy.
       Changes made through this reference will therefore be
       reflected in the calling method.



                                                        Contd…
   Named Arguments
      Named arguments allow you to pass in parameters in
       any order.
   Optional Arguments
      Parameters can also be optional
      You must supply a default value for parameters that
       are optional
      The optional parameter(s) must be the last ones
       defined as well.
   C# supports method overloading – several
    versions of the method that have different
    signatures.
    ◦ To overload methods, you simply declare the methods with
      the same name but different number or types of
      parameters.
   A property is a member that provides a flexible
    mechanism to read, write, or compute the value of
    a private field.
   Properties can be used as if they are public data
    members, but they are actually special methods
    called accessors.
    ◦ Ex:
       private int _age;
       public int Age
       {
         get{return age;}
         set{age=value;}
       }
    ◦ We can have access modifiers for set and get accessors
   Whenever a class or struct is created, its constructor is called.
   A class or struct may have multiple constructors that take
    different arguments.
   Constructors enable the programmer to set default values,
    limit instantiation, and write code that is flexible and easy to
    read.
   If you do not provide a constructor for your object, C# will
    create one by default that instantiates the object and sets
    member variables to the default values.
   Static classes and structs can also have constructors.
   We can have private constructors




                                                             Contd…
   Static Constructors
        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.
        A static constructor does not take access modifiers or have parameters.
        The user has no control on when the static constructor is executed in the
         program.

   Calling Constructors from Other Constructors
      A constructor can use the base keyword to call the constructor of
       a base class.
      Ex:
           public Manager() : base()
           { //Add further instructions here. }


                                                                       Contd…
   A constructor can invoke another constructor
    in the same object by using the this keyword.
   The readonly keyword is a modifier that you can
    use on fields.
   When a field declaration includes a readonly
    modifier, assignments to the fields introduced by
    the declaration can only occur as part of the
    declaration or in a constructor in the same class.
   Ex:
       class Age {
       readonly int _year; Age(int year)
       { _year = year; }
       void ChangeYear() { //_year = 1967; // Compile error if uncommented. }
       }
   Anonymous types provide a convenient way to
    encapsulate a set of read-only properties into a single
    object without having to explicitly define a type first.
   The type name is generated by the compiler and is not
    available at the source code level. The type of each
    property is inferred by the compiler.
   You create anonymous types by using the new operator
    together with an object initializer.
    ◦ Ex:
      var v = new { Amount = 108, Message = "Hello" };
   It is possible to split the definition of a class or a struct, an
    interface or a method over two or more source files. Each
    source file contains a section of the type or method
    definition, and all parts are combined when the application is
    compiled.
   To split a class definition, use the partial keyword modifier,
    as shown here:
        public partial class Employee
        {
        public void DoWork() { }
        }
        public partial class Employee
        {
        public void GoToLunch() { }
        }
   A static class is basically the same as a non-static
    class, but there is one difference: a static class
    cannot be instantiated.

   The following list provides the main features of a static class:
       Contains only static members.
       Cannot be instantiated.
       Is sealed.
       Cannot contain Instance Constructors.
   As indicated earlier, all .NET classes are ultimately
      derived from System.Object.
     System.Object Methods:
Equals(Object)      Determines whether the specified object is equal to the
                    current Object.

Finalize()          Allows an object to try to free resources and perform other
                    cleanup operations before it is reclaimed by garbage
                    collection.

GetHashCode()       Serves as a hash function for a particular type

MemberwiseClone()   Creates a shallow copy of the current Object.

ToString()          Returns a string that represents the current object.
   Extension methods enable you to "add" methods to
    existing types without creating a new derived type,
    recompiling, or otherwise modifying the original type.
   Extension methods are defined as static methods but
    are called by using instance method syntax.
   Their first parameter specifies which type the method
    operates on, and the parameter is preceded by the
    this modifier.
   Extension methods are only in scope when you
    explicitly import the namespace into your source
    code with a using directive.

More Related Content

What's hot

Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
Advanced c#
Advanced c#Advanced c#
Advanced c#saranuru
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 

What's hot (20)

Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Classes2
Classes2Classes2
Classes2
 
My c++
My c++My c++
My c++
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
C++ training
C++ training C++ training
C++ training
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Java
JavaJava
Java
 

Similar to Objects and Types C# (20)

Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegan
 
C# program structure
C# program structureC# program structure
C# program structure
 
C++
C++C++
C++
 
C++
C++C++
C++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
 
CSharp for Unity Day 3
CSharp for Unity Day 3CSharp for Unity Day 3
CSharp for Unity Day 3
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
C# concepts
C# conceptsC# concepts
C# concepts
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
VB.net
VB.netVB.net
VB.net
 
LEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMTLEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMT
 
C# interview
C# interviewC# interview
C# interview
 

More from Raghuveer Guthikonda (9)

C# String
C# StringC# String
C# String
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Generics C#
Generics C#Generics C#
Generics C#
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Regex in C#
Regex in C#Regex in C#
Regex in C#
 

Recently uploaded

COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 

Recently uploaded (20)

COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 

Objects and Types C#

  • 2. A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.  A class is like a blueprint. It defines the data and behavior of a type.  Syntax for creating a class  [attributes] [modifiers] class identifier [:base-list] { class-body }  Ex:  public class Customer { //Fields, properties, methods and events go here... } Contd…
  • 3. The data and functions within a class are known as the class’s members.  Data Members  Data members are those members that contain the data for class – fields, constants and events.  Data members can be static.  Fields are any variables associated with the class.  Events are class members that allow an object to notify a caller whenever something of interest occurs.  Function Members  Function members are those members that provide some functionality for manipulating the data in the class.  These include methods, properties, constructors, finalizers, operators, and indexers.
  • 4. A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types.  The declaration of a struct takes the following form:  [attributes] [modifiers] struct identifier [:interfaces] {//body }  Structs share most of the same syntax as classes, although structs are more limited than classes:  Within a struct declaration, fields cannot be initialized unless they are declared as const or static.  A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
  • 5. Structs are value types and classes are reference types.  Unlike classes, structs can be instantiated without using a new operator.  Structs can declare constructors that have parameters.  A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.  A struct can implement interfaces.  A struct can be used as a nullable type and can be assigned a null value.
  • 6. A method is a code block that contains a series of statements.  In C#, every executed instruction is performed in the context of a method.  Declaring Methods ◦ Syntax [modifiers] return-type MethodName ([parameters]) { //method body } Contd…
  • 7. Passing Parameters to Methods ◦ Parameters can be passed into methods by reference or by value. ◦ In C#, all parameters are passed by value unless you specifically say otherwise.  ref Parameters ◦ The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling method.  out Parameters ◦ The out keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. Contd…
  • 8. Passing by Reference vs. Passing by Value  When a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method. You can pass a value-type by reference by using the ref keyword.  Reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy. Changes made through this reference will therefore be reflected in the calling method. Contd…
  • 9. Named Arguments  Named arguments allow you to pass in parameters in any order.  Optional Arguments  Parameters can also be optional  You must supply a default value for parameters that are optional  The optional parameter(s) must be the last ones defined as well.
  • 10. C# supports method overloading – several versions of the method that have different signatures. ◦ To overload methods, you simply declare the methods with the same name but different number or types of parameters.
  • 11. A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.  Properties can be used as if they are public data members, but they are actually special methods called accessors. ◦ Ex: private int _age; public int Age { get{return age;} set{age=value;} } ◦ We can have access modifiers for set and get accessors
  • 12. Whenever a class or struct is created, its constructor is called.  A class or struct may have multiple constructors that take different arguments.  Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.  If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values.  Static classes and structs can also have constructors.  We can have private constructors Contd…
  • 13. Static Constructors  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.  A static constructor does not take access modifiers or have parameters.  The user has no control on when the static constructor is executed in the program.  Calling Constructors from Other Constructors  A constructor can use the base keyword to call the constructor of a base class.  Ex: public Manager() : base() { //Add further instructions here. } Contd…
  • 14. A constructor can invoke another constructor in the same object by using the this keyword.
  • 15. The readonly keyword is a modifier that you can use on fields.  When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.  Ex: class Age { readonly int _year; Age(int year) { _year = year; } void ChangeYear() { //_year = 1967; // Compile error if uncommented. } }
  • 16. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.  The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.  You create anonymous types by using the new operator together with an object initializer. ◦ Ex:  var v = new { Amount = 108, Message = "Hello" };
  • 17. It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.  To split a class definition, use the partial keyword modifier, as shown here: public partial class Employee { public void DoWork() { } } public partial class Employee { public void GoToLunch() { } }
  • 18. A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.  The following list provides the main features of a static class:  Contains only static members.  Cannot be instantiated.  Is sealed.  Cannot contain Instance Constructors.
  • 19. As indicated earlier, all .NET classes are ultimately derived from System.Object.  System.Object Methods: Equals(Object) Determines whether the specified object is equal to the current Object. Finalize() Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. GetHashCode() Serves as a hash function for a particular type MemberwiseClone() Creates a shallow copy of the current Object. ToString() Returns a string that represents the current object.
  • 20. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.  Extension methods are defined as static methods but are called by using instance method syntax.  Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier.  Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.