SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Lesson 11
Learn C#. Series of C# lessons
http://csharp.honcharuk.me/lesson-11
Agenda
• LINQ (Language-Integrated Query)
• Generics
• System.Collections.Generic
• Attributes
• Reflection
• Exporter demo
LINQ (Language-Integrated Query)
• A query is an expression that retrieves data from a data source.
• All LINQ query operations consist of three distinct actions:
1. Obtain the data source
2. Create the query
3. Execute the query
var numbers = new[] {12, 3, 7, 4, 3, 1, 8, 22,
65, 33, 7, 9, 3, 3};
var evenNumbers = numbers.Where(x => x%2 == 0);
foreach (var evenNumber in evenNumbers)
{
Console.WriteLine(evenNumber);
}
var numbers = new[] {12, 3, 7, 4, 3, 1, 8, 22, 65, 33,
7, 9, 3, 3};
var evenNumbers = numbers.Where(x => x%2 == 0)
.Select(x=> $"Number {x} is even.");
foreach (var evenNumber in evenNumbers)
{
Console.WriteLine(evenNumber);
}
Example of Generic class
class Swapper<T>
{
public void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
}
Some type that can be passed into the class
Generic class usage
class Program
{
class SuperValue
{
public string Value { get; set; }
}
static void Main(string[] args)
{
int x = 12;
int y = 20;
SuperValue value1 = new SuperValue { Value = "Hello” };
SuperValue value2 = new SuperValue { Value = "World" };
Swapper<int> swapperOne = new Swapper<int>();
var swapperTwo = new Swapper<SuperValue>();
swapperOne.Swap(ref x, ref y);
swapperTwo.Swap(ref value1, ref value2);
Console.WriteLine($"x = {x}, y = {y}");
Console.WriteLine($"v1 = {value1.Value}, v2 = {value2.Value}");
Console.ReadLine();
}
}
Generic Constraints
By default, a type parameter can be substituted with any type whatsoever. Constraints can be
applied to a type parameter to require more specific type arguments:
 where T : base-class // Base-class constraint
 where T : interface // Interface constraint
 where T : class // Reference-type constraint
 where T : struct // Value-type constraint (excludes Nullable types)
 where T : new() // Parameterless constructor constraint
 where U : T // Naked type constraint
class Swapper<T> where T:class
{
public void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
}
Swapper<int> swapperOne = new Swapper<int>();
int is not a reference type!!!
System.Collections.Generic
List<string> names = new List<string>();
names.Add("John");
names.Add("Mike");
names.Add("Alan");
foreach (var name in names)
{
Console.WriteLine(name);
}
Dictionary<string,string> bouquet = new Dictionary<string, string>();
bouquet.Add("rose", "red");
bouquet.Add("tulip", "yellow");
bouquet.Add("chamomile", "white");
bouquet.Add("aster", "white");
Console.WriteLine($"Rose's color is {bouquet["rose"]}.");
Console.WriteLine("All flowers in bouquet.");
Console.WriteLine($"Total number is {bouquet.Count}");
foreach (var flower in bouquet)
{
Console.WriteLine($"{flower.Key} is {flower.Value}");
}
Console.WriteLine("White flowers are:");
foreach (var flower in bouquet.Where(x=>x.Value== "white"))
{
Console.WriteLine($"{flower.Key}");
}v
Attributes
• Attributes extend classes and types. This C# feature allows you to
attach declarative information to any type
class DisplayValueAttribute : Attribute
{
public string Text { get; set; }
}
class Record
{
[DisplayValue(Text="Name of the record")]
public string Name { get; set; }
public string Value { get; set; }
[DisplayValue(Text = "Created at")]
public DateTime Created { get; set; }
[Hide]
public DateTime Changed { get; set; }
}
public class HideAttribute : Attribute
{
}
Reflection
• Reflection objects are used for obtaining type information at runtime.
The classes that give access to the metadata of a running program are
in the System.Reflection namespace.
int i = 42;
Type type = i.GetType();
Console.WriteLine(type);
System.Reflection.Assembly info = typeof(int).Assembly;
Console.WriteLine(info);
Exporter demo
Thank you!
Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-convertedMicheal Ogundero
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a BossBob Tiernay
 
Introduction to MySQL in PHP
Introduction to MySQL in PHPIntroduction to MySQL in PHP
Introduction to MySQL in PHPhamsa nandhini
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.David Gómez García
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Array within a class
Array within a classArray within a class
Array within a classAAKASH KUMAR
 
Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny Nafan
 
Database design and error handling
Database design and error handlingDatabase design and error handling
Database design and error handlinghamsa nandhini
 
Useful JMeter functions for scripting
Useful JMeter functions for scriptingUseful JMeter functions for scripting
Useful JMeter functions for scriptingTharinda Liyanage
 

Was ist angesagt? (20)

Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-converted
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Introduction to MySQL in PHP
Introduction to MySQL in PHPIntroduction to MySQL in PHP
Introduction to MySQL in PHP
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Array within a class
Array within a classArray within a class
Array within a class
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
Stacks
StacksStacks
Stacks
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Database design and error handling
Database design and error handlingDatabase design and error handling
Database design and error handling
 
Datastruct2
Datastruct2Datastruct2
Datastruct2
 
Stack queue
Stack queueStack queue
Stack queue
 
Useful JMeter functions for scripting
Useful JMeter functions for scriptingUseful JMeter functions for scripting
Useful JMeter functions for scripting
 
DIG1108 Lesson 6
DIG1108 Lesson 6DIG1108 Lesson 6
DIG1108 Lesson 6
 

Andere mochten auch

Portfólio serviços psrv
Portfólio serviços psrvPortfólio serviços psrv
Portfólio serviços psrvRomulo Bokorni
 
La théorie des rôles en équipe de Belbin.
La théorie des rôles en équipe de Belbin.La théorie des rôles en équipe de Belbin.
La théorie des rôles en équipe de Belbin.Daf Ossouala
 
Living in the future
Living in the futureLiving in the future
Living in the futureMar Jurado
 
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...
Απάντηση σε ερώτηση Ν. Μηταράκη σχετικά με την επιτάχυνση της νέας διαδικασία...Notis Mitarachi
 
Stress management techniques suggested by custom soft
Stress management techniques  suggested by custom softStress management techniques  suggested by custom soft
Stress management techniques suggested by custom softCustom Soft
 
Cloud Computing for Startups
Cloud Computing for StartupsCloud Computing for Startups
Cloud Computing for StartupsYong Li
 

Andere mochten auch (13)

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

Ähnlich wie Lesson11

Ähnlich wie Lesson11 (20)

Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
C# / Java Language Comparison
C# / Java Language ComparisonC# / Java Language Comparison
C# / Java Language Comparison
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
C# programming
C# programming C# programming
C# programming
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
PostThis
PostThisPostThis
PostThis
 
Linq intro
Linq introLinq intro
Linq intro
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 

Mehr von Alex Honcharuk (6)

Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Lesson9
Lesson9Lesson9
Lesson9
 
Lesson6
Lesson6Lesson6
Lesson6
 
Lesson5
Lesson5Lesson5
Lesson5
 
Lesson2
Lesson2Lesson2
Lesson2
 
Lesson1
Lesson1Lesson1
Lesson1
 

Kürzlich hochgeladen

Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsapna80328
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSsandhya757531
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdfsahilsajad201
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfNainaShrivastava14
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 

Kürzlich hochgeladen (20)

Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveying
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdf
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 

Lesson11

  • 1. Lesson 11 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-11
  • 2. Agenda • LINQ (Language-Integrated Query) • Generics • System.Collections.Generic • Attributes • Reflection • Exporter demo
  • 3. LINQ (Language-Integrated Query) • A query is an expression that retrieves data from a data source. • All LINQ query operations consist of three distinct actions: 1. Obtain the data source 2. Create the query 3. Execute the query var numbers = new[] {12, 3, 7, 4, 3, 1, 8, 22, 65, 33, 7, 9, 3, 3}; var evenNumbers = numbers.Where(x => x%2 == 0); foreach (var evenNumber in evenNumbers) { Console.WriteLine(evenNumber); } var numbers = new[] {12, 3, 7, 4, 3, 1, 8, 22, 65, 33, 7, 9, 3, 3}; var evenNumbers = numbers.Where(x => x%2 == 0) .Select(x=> $"Number {x} is even."); foreach (var evenNumber in evenNumbers) { Console.WriteLine(evenNumber); }
  • 4. Example of Generic class class Swapper<T> { public void Swap(ref T a, ref T b) { T temp = a; a = b; b = temp; } } Some type that can be passed into the class
  • 5. Generic class usage class Program { class SuperValue { public string Value { get; set; } } static void Main(string[] args) { int x = 12; int y = 20; SuperValue value1 = new SuperValue { Value = "Hello” }; SuperValue value2 = new SuperValue { Value = "World" }; Swapper<int> swapperOne = new Swapper<int>(); var swapperTwo = new Swapper<SuperValue>(); swapperOne.Swap(ref x, ref y); swapperTwo.Swap(ref value1, ref value2); Console.WriteLine($"x = {x}, y = {y}"); Console.WriteLine($"v1 = {value1.Value}, v2 = {value2.Value}"); Console.ReadLine(); } }
  • 6. Generic Constraints By default, a type parameter can be substituted with any type whatsoever. Constraints can be applied to a type parameter to require more specific type arguments:  where T : base-class // Base-class constraint  where T : interface // Interface constraint  where T : class // Reference-type constraint  where T : struct // Value-type constraint (excludes Nullable types)  where T : new() // Parameterless constructor constraint  where U : T // Naked type constraint class Swapper<T> where T:class { public void Swap(ref T a, ref T b) { T temp = a; a = b; b = temp; } } Swapper<int> swapperOne = new Swapper<int>(); int is not a reference type!!!
  • 7. System.Collections.Generic List<string> names = new List<string>(); names.Add("John"); names.Add("Mike"); names.Add("Alan"); foreach (var name in names) { Console.WriteLine(name); } Dictionary<string,string> bouquet = new Dictionary<string, string>(); bouquet.Add("rose", "red"); bouquet.Add("tulip", "yellow"); bouquet.Add("chamomile", "white"); bouquet.Add("aster", "white"); Console.WriteLine($"Rose's color is {bouquet["rose"]}."); Console.WriteLine("All flowers in bouquet."); Console.WriteLine($"Total number is {bouquet.Count}"); foreach (var flower in bouquet) { Console.WriteLine($"{flower.Key} is {flower.Value}"); } Console.WriteLine("White flowers are:"); foreach (var flower in bouquet.Where(x=>x.Value== "white")) { Console.WriteLine($"{flower.Key}"); }v
  • 8. Attributes • Attributes extend classes and types. This C# feature allows you to attach declarative information to any type class DisplayValueAttribute : Attribute { public string Text { get; set; } } class Record { [DisplayValue(Text="Name of the record")] public string Name { get; set; } public string Value { get; set; } [DisplayValue(Text = "Created at")] public DateTime Created { get; set; } [Hide] public DateTime Changed { get; set; } } public class HideAttribute : Attribute { }
  • 9. Reflection • Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace. int i = 42; Type type = i.GetType(); Console.WriteLine(type); System.Reflection.Assembly info = typeof(int).Assembly; Console.WriteLine(info);

Hinweis der Redaktion

  1. https://msdn.microsoft.com/en-us/library/system.collections.generic(v=vs.110).aspx 101 - https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
  2. https://msdn.microsoft.com/en-us/library/system.collections.generic(v=vs.110).aspx
  3. https://msdn.microsoft.com/en-us/library/mt656691.aspx http://www.tutorialspoint.com/csharp/csharp_reflection.htm