SlideShare a Scribd company logo
Essential language features
Essential Language Features
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopb
aabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaa
bte/
+91 9746854752
Automatically Implemented Properties
Consider a class “baabtraStudent” with the 3 properties name, marks and
isPromotable . Consider the following code to implement the class.
If we have a closer look at the implementation of properties, properties name and
marks have get and set methods implemented in the usual way whereas the
boolean property isPromotable is implemented as a read-only property and the
get accessor of isPromotable is implemented with some logic.
The logic is “the student is promotable (isPromotable returns true) only when his
marks are greater than 60”
Leaving the isPromotable property and if we look at the other two, there is no
logic in the get and set accessors. So if we have 10 or 20 or more such properties
we will have to write a lot of code to implement all these which will actually take
away a lot from the developers’ time.
The concept of auto-implemented properties removes this hassle by making the
property declaration in a single line of code which will look like
Public string name { get; set; }
With such a declaration the compiler will automatically implement the get and set
accessors of the property. So that our earlier implementation of the
baabtraStudent class can be re-written as
This enhances the code maintainability by a greater degree and removes the need
of writing a lot of code to implement the properties.
Summary
auto implemented properties can only be used when the implementation of
property (get and set accessors) need no additional logic
Auto implemented properties reduce the amount of code that needs to be
written
We don’t have to explicitly declare a field for an auto implemented property.
when we use auto-implemented properties, the compiler creates a
private, anonymous field that can only be only be accessed through the property’s
get and set accessors.
Object and Collection Initializers
Object initializers let us assign values to any accessible fields or properties of an
object at creation time without having to invoke a constructor followed by lines of
assignment statements.
Consider the class “baabtraStudent” again
Suppose if we want to create 3 object instances of this class, we will have to write
the following code,
This needs a lot of code to be written to initialize objects.
C# offers another method of object initializing as shown in the following code
Which saves a lot of coding and time for the developers.
A collection object of the “baabtrStudent” class (List<baabtraStudent>) can also
be initialized in the same way as in the following code. This eliminates creating
and adding new objects to the collection one by one.
Extension Methods
Extension methods help us to add new methods to existing types without
modifying the original code, inheriting or aggregating .
Consider a simple class called “mathOperations” which is having only an addition
method, which takes two numbers as arguments and returns the added value as
result.
Suppose this class is added as a compiled dll to our application and the source
code is no longer available to edit and add other methods.
We can create an instance of this class in our application and perform an add
function and display the result as in the following code
When we put a dot to the right of the mathOperations variable the “add” method
is populated in the intelliscence
And the program runs
smoothly
Now if we want to find the difference between two numbers, there is no subtract
method available in the “mathOperations” class. So to add a subtract method we
can go for 3 options
add a subtract method in the “mathOperations” class – this is not possible as
mathOperations is a third party dll and we don’t have the source code for it.
Through aggregation - Create a wrapper class around the math operations class
and add the subtract method as shown in the following example
And access the method as shown in the following code
But now as you can see the add method can only be accessed through a
mathOperations field of wrapperMathOperations class and is not directly
accessible from the wrapperMathOperationsClass.
The third method is to inherit the mathOperations class and add the subtract
method
In any of the three methods the complexity of the class structure is increased.
The correct solution to such a scenario is an extnesion method “subtract” for the
mathOperations type which can be created in the following way,
Now the method “add” and “subtract” (as an extention method, see the blue
arrow right to the pink block) appears in the intelliscense block for an object
instance of the mathOperations class as shown in the following fig.
Summary
extension methods enable us to add methods to a datatype without changing
the code for the datatype or creating a derived type.
An extension method must be a static method inside a static class
they can be called on the object instance in the same way as we call any other
defined methods in the object class
Anonymous Methods & Lambda
Expressions
Anonymous methods provide a technique to pass a code block as a delegate
parameter.
Anonymous methods are basically methods without a name, just the body.
We need not specify the return type in an anonymous method; it is inferred from
the return statement inside the method body.
Anonymous methods are declared with the creation of the delegate instance, with
a delegate keyword. For example
The code block Console.WriteLine("Anonymous Method: {0}", x); is the body of the
anonymous method.
The delegate could be called both with anonymous methods as well as named
methods in the same way, i.e., by passing the method parameters to the delegate
object.
By using anonymous methods, we reduce the coding overhead in instantiating
delegates because you do not have to create a separate named method.
The scope of the parameters of an anonymous method is the anonymous-
method-block.
It is an error to have a jump statement, such as goto, break, or continue, inside
the anonymous method block if the target is outside the block. It is also an error
to have a jump statement, such as goto, break, or continue, outside the
anonymous method block if the target is inside the block.
Lambda expressions
Lambda expressions are similar to anonymous methods with the difference that in
lambda expressions we don’t have to use the “delegate” keyword and the input
parameter type explicitly.
Lamda expressions are more easy to write than anonymous methods because
they need much lesser amount of coding and they are syntactically simpler.
Lambda expressions are particularly helpful in writing LINQ query expressions.
In an anonymous method the inout parameters canbe completely omitted if
they are not used inside the method block but in case of a lambda expression this
is not possible.
Most of the time lambda expressions supersede anonymous methods unless the
input parameters to the method has to be omitted which is not possible in lambda
expressions
Examples
The examples below demonstrates the same functionality implemented with
anonymous method and lambda expression
Anonymous method
Examples
Lambda expression
Automatic type inference
The var keyword in C# allows the compiler to infer the type of a variable implicitly.
A var can be set to any type like, string, int, double etc.
The var can be used to access a variable based on an anonymous class.
var is only allowed for local variables
var is also strongly typed, but by the compiler. For example, The following two
declarations of i are functionally equivalent:
A variable of type var cannot be initialized to null
After the initial assignment the type of the variable is fixed
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PPT
Ap Power Point Chpt3
PPSX
Comparable and comparator – a detailed discussion
PDF
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
PPT
Chap02
PPT
Ap Power Point Chpt2
PPTX
C# interview
PPT
Ap Power Point Chpt3 B
PPT
Chap04
Ap Power Point Chpt3
Comparable and comparator – a detailed discussion
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Chap02
Ap Power Point Chpt2
C# interview
Ap Power Point Chpt3 B
Chap04

What's hot (20)

PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
PPTX
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
PPTX
The Go Programing Language 1
PPTX
Effective Java - Chapter 3: Methods Common to All Objects
PPTX
The Little Wonders of C# 6
PPTX
OCA Java SE 8 Exam Chapter 2 Operators & Statements
PPTX
Evolution of c# - by K.Jegan
PPTX
Comparable/ Comparator
DOC
DOCX
C# interview quesions
PPT
Programming In C++
PPTX
Core C# Programming Constructs, Part 1
PDF
Solutions manual for c++ programming from problem analysis to program design ...
PPT
Java căn bản - Chapter13
PPT
Chapter 4 5
PDF
Code Smells and Its type (With Example)
PPTX
Templates and Exception Handling in C++
PPT
PPT
C#3.0 & Vb 9.0 Language Enhancments
DOC
C# interview questions
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
The Go Programing Language 1
Effective Java - Chapter 3: Methods Common to All Objects
The Little Wonders of C# 6
OCA Java SE 8 Exam Chapter 2 Operators & Statements
Evolution of c# - by K.Jegan
Comparable/ Comparator
C# interview quesions
Programming In C++
Core C# Programming Constructs, Part 1
Solutions manual for c++ programming from problem analysis to program design ...
Java căn bản - Chapter13
Chapter 4 5
Code Smells and Its type (With Example)
Templates and Exception Handling in C++
C#3.0 & Vb 9.0 Language Enhancments
C# interview questions
Ad
Ad

Similar to Essential language features (20)

PPTX
C++ Object Oriented Programming
PDF
The Uniform Access Principle
PPT
Chapter 13 - Inheritance and Polymorphism
PPT
C++ classes tutorials
PPTX
Presentation 3rd
PPT
Methods in C#
PPT
PPT
Classes2
PDF
CIS 1403 lab 3 functions and methods in Java
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PPT
Object Oriented Programming (Advanced )
PDF
Absolute C++ 6th Edition Savitch Solutions Manual
DOCX
I assignmnt(oops)
PDF
All chapter download Absolute C++ 6th Edition Savitch Solutions Manual
PDF
Absolute C++ 6th Edition Savitch Solutions Manual
PPT
Javascript design patterns
PDF
4 pillars of OOPS CONCEPT
PDF
Absolute C++ 6th Edition Savitch Solutions Manual
PPT
C#3.0 & Vb 9.0 New Features
PPT
C++ Interview Questions
C++ Object Oriented Programming
The Uniform Access Principle
Chapter 13 - Inheritance and Polymorphism
C++ classes tutorials
Presentation 3rd
Methods in C#
Classes2
CIS 1403 lab 3 functions and methods in Java
C++ tutorial assignment - 23MTS5730.pptx
Object Oriented Programming (Advanced )
Absolute C++ 6th Edition Savitch Solutions Manual
I assignmnt(oops)
All chapter download Absolute C++ 6th Edition Savitch Solutions Manual
Absolute C++ 6th Edition Savitch Solutions Manual
Javascript design patterns
4 pillars of OOPS CONCEPT
Absolute C++ 6th Edition Savitch Solutions Manual
C#3.0 & Vb 9.0 New Features
C++ Interview Questions

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
medical staffing services at VALiNTRY
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
assetexplorer- product-overview - presentation
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
Softaken Excel to vCard Converter Software.pdf
Operating system designcfffgfgggggggvggggggggg
Which alternative to Crystal Reports is best for small or large businesses.pdf
medical staffing services at VALiNTRY
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Reimagine Home Health with the Power of Agentic AI​
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
assetexplorer- product-overview - presentation
Designing Intelligence for the Shop Floor.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

Essential language features

  • 2. Essential Language Features Anoop K. C. anoop@baabte.com www.facebook.com/anoopb aabte twitter.com/anoop_baabte in.linkedin.com/in/anoopbaa bte/ +91 9746854752
  • 4. Consider a class “baabtraStudent” with the 3 properties name, marks and isPromotable . Consider the following code to implement the class.
  • 5. If we have a closer look at the implementation of properties, properties name and marks have get and set methods implemented in the usual way whereas the boolean property isPromotable is implemented as a read-only property and the get accessor of isPromotable is implemented with some logic. The logic is “the student is promotable (isPromotable returns true) only when his marks are greater than 60” Leaving the isPromotable property and if we look at the other two, there is no logic in the get and set accessors. So if we have 10 or 20 or more such properties we will have to write a lot of code to implement all these which will actually take away a lot from the developers’ time. The concept of auto-implemented properties removes this hassle by making the property declaration in a single line of code which will look like Public string name { get; set; }
  • 6. With such a declaration the compiler will automatically implement the get and set accessors of the property. So that our earlier implementation of the baabtraStudent class can be re-written as This enhances the code maintainability by a greater degree and removes the need of writing a lot of code to implement the properties.
  • 7. Summary auto implemented properties can only be used when the implementation of property (get and set accessors) need no additional logic Auto implemented properties reduce the amount of code that needs to be written We don’t have to explicitly declare a field for an auto implemented property. when we use auto-implemented properties, the compiler creates a private, anonymous field that can only be only be accessed through the property’s get and set accessors.
  • 8. Object and Collection Initializers
  • 9. Object initializers let us assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. Consider the class “baabtraStudent” again
  • 10. Suppose if we want to create 3 object instances of this class, we will have to write the following code, This needs a lot of code to be written to initialize objects.
  • 11. C# offers another method of object initializing as shown in the following code Which saves a lot of coding and time for the developers. A collection object of the “baabtrStudent” class (List<baabtraStudent>) can also be initialized in the same way as in the following code. This eliminates creating and adding new objects to the collection one by one.
  • 13. Extension methods help us to add new methods to existing types without modifying the original code, inheriting or aggregating . Consider a simple class called “mathOperations” which is having only an addition method, which takes two numbers as arguments and returns the added value as result. Suppose this class is added as a compiled dll to our application and the source code is no longer available to edit and add other methods.
  • 14. We can create an instance of this class in our application and perform an add function and display the result as in the following code When we put a dot to the right of the mathOperations variable the “add” method is populated in the intelliscence And the program runs smoothly
  • 15. Now if we want to find the difference between two numbers, there is no subtract method available in the “mathOperations” class. So to add a subtract method we can go for 3 options add a subtract method in the “mathOperations” class – this is not possible as mathOperations is a third party dll and we don’t have the source code for it. Through aggregation - Create a wrapper class around the math operations class and add the subtract method as shown in the following example
  • 16. And access the method as shown in the following code But now as you can see the add method can only be accessed through a mathOperations field of wrapperMathOperations class and is not directly accessible from the wrapperMathOperationsClass. The third method is to inherit the mathOperations class and add the subtract method In any of the three methods the complexity of the class structure is increased.
  • 17. The correct solution to such a scenario is an extnesion method “subtract” for the mathOperations type which can be created in the following way, Now the method “add” and “subtract” (as an extention method, see the blue arrow right to the pink block) appears in the intelliscense block for an object instance of the mathOperations class as shown in the following fig.
  • 18. Summary extension methods enable us to add methods to a datatype without changing the code for the datatype or creating a derived type. An extension method must be a static method inside a static class they can be called on the object instance in the same way as we call any other defined methods in the object class
  • 19. Anonymous Methods & Lambda Expressions
  • 20. Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are basically methods without a name, just the body. We need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body. Anonymous methods are declared with the creation of the delegate instance, with a delegate keyword. For example The code block Console.WriteLine("Anonymous Method: {0}", x); is the body of the anonymous method.
  • 21. The delegate could be called both with anonymous methods as well as named methods in the same way, i.e., by passing the method parameters to the delegate object. By using anonymous methods, we reduce the coding overhead in instantiating delegates because you do not have to create a separate named method. The scope of the parameters of an anonymous method is the anonymous- method-block. It is an error to have a jump statement, such as goto, break, or continue, inside the anonymous method block if the target is outside the block. It is also an error to have a jump statement, such as goto, break, or continue, outside the anonymous method block if the target is inside the block.
  • 22. Lambda expressions Lambda expressions are similar to anonymous methods with the difference that in lambda expressions we don’t have to use the “delegate” keyword and the input parameter type explicitly. Lamda expressions are more easy to write than anonymous methods because they need much lesser amount of coding and they are syntactically simpler. Lambda expressions are particularly helpful in writing LINQ query expressions. In an anonymous method the inout parameters canbe completely omitted if they are not used inside the method block but in case of a lambda expression this is not possible. Most of the time lambda expressions supersede anonymous methods unless the input parameters to the method has to be omitted which is not possible in lambda expressions
  • 23. Examples The examples below demonstrates the same functionality implemented with anonymous method and lambda expression Anonymous method
  • 26. The var keyword in C# allows the compiler to infer the type of a variable implicitly. A var can be set to any type like, string, int, double etc. The var can be used to access a variable based on an anonymous class. var is only allowed for local variables var is also strongly typed, but by the compiler. For example, The following two declarations of i are functionally equivalent: A variable of type var cannot be initialized to null After the initial assignment the type of the variable is fixed
  • 27. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 28. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com