SlideShare une entreprise Scribd logo
1  sur  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?

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette

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
 

En vedette (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
 

Similaire à Lesson11

Similaire à 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 Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
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
 

Plus de 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
 

Dernier

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
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
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESNarmatha D
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
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
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 

Dernier (20)

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
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
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIES
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
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
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 

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);

Notes de l'éditeur

  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