SlideShare uma empresa Scribd logo
1 de 28
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

Mais conteúdo relacionado

Mais procurados

OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1İbrahim Kürce
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objectsİbrahim Kürce
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6BlackRabbitCoder
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statementsİbrahim Kürce
 
Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegantalenttransform
 
Comparable/ Comparator
Comparable/ ComparatorComparable/ Comparator
Comparable/ ComparatorSean McElrath
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1Vahid Farahmandian
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13Vince Vo
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++Nimrita Koul
 
C#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language EnhancmentsC#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language Enhancmentstechfreak
 

Mais procurados (20)

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

Destaque (8)

Sets in python
Sets in pythonSets in python
Sets in python
 
Posting using ajax
Posting using ajaxPosting using ajax
Posting using ajax
 
Html5
Html5 Html5
Html5
 
Web services in java
Web services in javaWeb services in java
Web services in java
 
conditional statements
conditional statementsconditional statements
conditional statements
 
Adding multiple object in java
Adding multiple object in javaAdding multiple object in java
Adding multiple object in java
 
Usage of bean class in java
Usage of bean class in javaUsage of bean class in java
Usage of bean class in java
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 

Semelhante a Essential language features

C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access PrinciplePhilip Schwarz
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )ayesha420248
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patternsGomathiNayagam S
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersKrishnaov
 

Semelhante a Essential language features (20)

C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Classes2
Classes2Classes2
Classes2
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patterns
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
My c++
My c++My c++
My c++
 
csharp.docx
csharp.docxcsharp.docx
csharp.docx
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 

Último (20)

WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

Essential language features

  • 1.
  • 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