SlideShare une entreprise Scribd logo
1  sur  32
Chapter -1Framework Fundamentals HarshanaWeerasinghe http://www.harshana.info http://Blog.harshana.info
Framework Fundamentals Create a console or Windows Forms application in Visual Studio. Add namespaces and references to system class libraries to a project. Run a project in Visual Studio, set breakpoints, step through code, and watch the values of variables. By Harshana Weerasinghe (http://www.harshana.info) 2
Types Mainly 2 types in Microsoft .Net Value Type Reference Type By Harshana Weerasinghe (http://www.harshana.info) 3
Value Types Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory. Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead. By Harshana Weerasinghe (http://www.harshana.info) 4
Built-in Value Types  By Harshana Weerasinghe (http://www.harshana.info) 5
Other Value Types By Harshana Weerasinghe (http://www.harshana.info) 6
Declare Value Types <type> <VariableName> [= <Default Value>]; boolbolTestVariable= false; Nullable<bool>b= null;         Or bool? b = null; By Harshana Weerasinghe (http://www.harshana.info) 7
Create User-Defined Types structCycle {     // Private fields int_val, _min, _max; // Constructor public Cycle(intmin, intmax)     {         _val = min;         _min = min;         _max = max;     }     public intValue     {         get { return _val; }         set         {               if (value > _max)                    _val = _min;             else             {                    if (value < _min)                         _val = _max;                     else                         _val = value;               }         }     }     public override string ToString()     {          return Value.ToString();     }     public intToInteger()     {         return Value;     } } By Harshana Weerasinghe (http://www.harshana.info) 8
Using Operators (new in .NET 2.0)* public static Cycle operator +(Cycle arg1, int arg2) {     arg1.Value += arg2;     return arg1; } public static Cycle operator -(Cycle arg1,intarg2) {     arg1.Value -= arg2;     return arg1; } By Harshana Weerasinghe (http://www.harshana.info) 9
Use User-Defined Types Cycle degrees =new Cycle(0, 359); Cycle quarters= new Cycle(1, 4); for (int i = 0; i <= 8; i++) {     degrees += 90; quarters += 1; Console.WriteLine("degrees = {0}, quarters = {1}", degrees, quarters); } By Harshana Weerasinghe (http://www.harshana.info) 10
Enumerations enumTitles : int{ Mr, Ms, Mrs, Dr }; Titles t = Titles.Dr; Console.WriteLine("{0}.", t);// Displays "Dr." By Harshana Weerasinghe (http://www.harshana.info) 11
Reference Types Reference types store the address of their data, also known as a pointer, on the stack. The actual data that address refers to is stored in an area of memory called the heap. The runtime manages the memory used by the heap through a process called garbagecollection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced. By Harshana Weerasinghe (http://www.harshana.info) 12
Built-in Reference Types By Harshana Weerasinghe (http://www.harshana.info) 13
Strings and String Builders string s="this is some text to search"; s = s.Replace("search", "replace"); Console.WriteLine(s); string s; s = "wombat"; // "wombat“ s += " kangaroo";// "wombat kangaroo“ s += " wallaby"; // "wombat kangaroo wallaby“ s += " koala";// "wombat kangaroo wallaby koala” Console.WriteLine(s); By Harshana Weerasinghe (http://www.harshana.info) 14
String Builder using System.Text; StringBuildersb =new StringBuilder(30); sb.Append("wombat");// Build string. sb.Append(" kangaroo"); sb.Append(" wallaby"); sb.Append(" koala"); string s = sb.ToString(); // Copy result to string. Console.WriteLine(s); By Harshana Weerasinghe (http://www.harshana.info) 15
Create and Sort Arrays // Declare and initialize an array. int[] ar = { 3, 1, 2 }; // Call a shared/static array method. Array.Sort(ar); // Display the result. Console.WriteLine("{0}, {1}, {2}", ar[0], ar[1], ar[2]); By Harshana Weerasinghe (http://www.harshana.info) 16
Streams Streams are another very common type because they are the means for reading from and writing to the disk and communicating across the network. The System.IO.Streamtype is the base type for all task-specific stream types.  By Harshana Weerasinghe (http://www.harshana.info) 17
Streams Cont… using System.IO; // Create and write to a text file StreamWritersw =new StreamWriter("text.txt"); sw.WriteLine("Hello, World!"); sw.Close(); // Read and display a text file StreamReadersr =new StreamReader("text.txt"); Console.WriteLine(sr.ReadToEnd()); sr.Close(); By Harshana Weerasinghe (http://www.harshana.info) 18
Throw and Catch Exceptions StreamReadersr = new StreamReader("text.txt"); try { Console.WriteLine(sr.ReadToEnd()); } catch (Exception ex) { // If there are any problems reading the file, display an error message Console.WriteLine("Error reading file: " + ex.Message); } finally { // Close the StreamReader, whether or not an exception occurred sr.Close(); } By Harshana Weerasinghe (http://www.harshana.info) 19
Constructing Classes public class Cycle { // Private fields int_val, _min, _max; // Constructor public Cycle(intmin,intmax)     {         _val = min;         _min = min;         _max = max;     } //Property public intValue     {         get { return _val; }         set { _val = value; }     } //Method public string MyValue()     {         return _val.ToString();     }         } By Harshana Weerasinghe (http://www.harshana.info) 20
Inheritance – Animal Class class Animal { string m_Name; intm_Age;     public Animal(string _Name)     { this.Name = _Name;             }     public string Name     {       get { return m_Name; }       set { m_Name = value; }  }     public intAge     {       get { return m_Age; }       set { m_Age = value; }     }     public string Speak()     {         return "Hello Animal";     } } By Harshana Weerasinghe (http://www.harshana.info) 21
Cat n Dog Classes class Cat : Animal { double m_Weight;     public double Weight     {         get { return m_Weight; }         set { m_Weight = value; }     }     public string Speak()     {         return "Meaw, Meaw";     } } class Dog : Animal { //Default Ctor public Dog()     {     } //Other Ctor public Dog(string _Name,int_Age)         : base(_Name)//Call Base Class Ctor     { this.Age = _Age;     }     public string Speak()     {         if (this.Age >3)                         return " Grr.. Grr.."; else             return " Buhh.. Buhh..";     } } By Harshana Weerasinghe (http://www.harshana.info) 22
Interfaces Interfaces, also known as contracts, define a common set of members that all classes that implement the interface must provide. For example, the IComparable interface defines the CompareTo method, which enables two instances of a class to be compared for equality. All classes that implement the IComparable interface, whether custom-created or built in the .NET Framework, can be compared for equality. By Harshana Weerasinghe (http://www.harshana.info) 23
By Harshana Weerasinghe (http://www.harshana.info) 24
Sample interface IMessage { // Send the message. Returns True is success, False otherwise. boolSend(); // The message to send. string Message { get; set; } // The Address to send to. string Address { get; set; } } class EmailMessage : IMessage { public boolSend()     {         throw new Exception("The method or operation is not implemented."); } public string Message {         get  {             throw new Exception("The method or operation is not implemented.");         } set         {             throw new Exception("The method or operation is not implemented.");         }     } public string Address     {         get {             throw new Exception("The method or operation is not implemented.");  } set  {             throw new Exception("The method or operation is not implemented."); }     } } By Harshana Weerasinghe (http://www.harshana.info) 25
Commonly used interfaces By Harshana Weerasinghe (http://www.harshana.info) 26
Partial Classes - new in .NET 2.0 Partial classes allow you to split a class definition across multiple source files. The benefit of this approach is that it hides details of the class definition so that derived classes can focus on more significant portions. By Harshana Weerasinghe (http://www.harshana.info) 27
Generics Generics are part of the .NET Framework's type system that allows you to define a type while leaving some details unspecified. Instead of specifying the types of parameters or member classes, you can allow code that uses your type to specify it. This allows consumer code to tailor your type to its own specific needs. By Harshana Weerasinghe (http://www.harshana.info) 28
Generics Cont.. class Obj { public Object t; public Object u; public Obj(Object _t,Object _u) {         t = _t;         u = _u;     } } class Gen<T, U> { public T t;     public U u;     public Gen(T _t, U _u) {         t = _t;         u = _u;     } } By Harshana Weerasinghe (http://www.harshana.info) 29
Generics Cont.. // Add two strings using the Obj class Objoa =new Obj("Hello, ", "World!"); Console.WriteLine((string)oa.t + (string)oa.u); // Add two strings using the Gen class Gen<string, string>ga = new Gen<string, string>("Hello, ", "World!"); Console.WriteLine(ga.t + ga.u); // Add a double and an int using the Obj class Objob =new Obj(10.125, 2005); Console.WriteLine((double)ob.t + (int)ob.u); // Add a double and an int using the Gen class Gen<double,int> gb = new Gen<double, int>(10.125, 2005); Console.WriteLine(gb.t + gb.u); By Harshana Weerasinghe (http://www.harshana.info) 30
Generics Cont.. // Add a double and an int using the Gen class Gen<double, int> gc = new Gen<double,int>(10.125, 2005); Console.WriteLine(gc.t + gc.u); // Add a double and an int using the Obj class Objoc =new Obj(10.125, 2005); Console.WriteLine((int)oc.t + (int)oc.u); last line contains an error— the oc.tvalue is cast to an Int instead of to a double. Unfortunately, the compiler won't catch the mistake. Instead, a run-time exception is thrown when the runtime attempts to cast a double to an Int value.  By Harshana Weerasinghe (http://www.harshana.info) 31
Events By Harshana Weerasinghe (http://www.harshana.info) 32

Contenu connexe

Tendances

Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaEduardo Bergavera
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
Hibernate
Hibernate Hibernate
Hibernate Sunil OS
 
66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manualLaura Popovici
 

Tendances (19)

CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
A Taste of Dotty
A Taste of DottyA Taste of Dotty
A Taste of Dotty
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Hibernate
Hibernate Hibernate
Hibernate
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 

Similaire à .Net Framework 2 fundamentals

Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancementRakesh Madugula
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdfAdilAijaz3
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2guest075fec
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.pptDeepVala5
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 

Similaire à .Net Framework 2 fundamentals (20)

srgoc
srgocsrgoc
srgoc
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Struts 2
Struts 2Struts 2
Struts 2
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 

Dernier

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

.Net Framework 2 fundamentals

  • 1. Chapter -1Framework Fundamentals HarshanaWeerasinghe http://www.harshana.info http://Blog.harshana.info
  • 2. Framework Fundamentals Create a console or Windows Forms application in Visual Studio. Add namespaces and references to system class libraries to a project. Run a project in Visual Studio, set breakpoints, step through code, and watch the values of variables. By Harshana Weerasinghe (http://www.harshana.info) 2
  • 3. Types Mainly 2 types in Microsoft .Net Value Type Reference Type By Harshana Weerasinghe (http://www.harshana.info) 3
  • 4. Value Types Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory. Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead. By Harshana Weerasinghe (http://www.harshana.info) 4
  • 5. Built-in Value Types By Harshana Weerasinghe (http://www.harshana.info) 5
  • 6. Other Value Types By Harshana Weerasinghe (http://www.harshana.info) 6
  • 7. Declare Value Types <type> <VariableName> [= <Default Value>]; boolbolTestVariable= false; Nullable<bool>b= null; Or bool? b = null; By Harshana Weerasinghe (http://www.harshana.info) 7
  • 8. Create User-Defined Types structCycle { // Private fields int_val, _min, _max; // Constructor public Cycle(intmin, intmax) { _val = min; _min = min; _max = max; } public intValue { get { return _val; } set { if (value > _max) _val = _min; else { if (value < _min) _val = _max; else _val = value; } } } public override string ToString() { return Value.ToString(); } public intToInteger() { return Value; } } By Harshana Weerasinghe (http://www.harshana.info) 8
  • 9. Using Operators (new in .NET 2.0)* public static Cycle operator +(Cycle arg1, int arg2) { arg1.Value += arg2; return arg1; } public static Cycle operator -(Cycle arg1,intarg2) { arg1.Value -= arg2; return arg1; } By Harshana Weerasinghe (http://www.harshana.info) 9
  • 10. Use User-Defined Types Cycle degrees =new Cycle(0, 359); Cycle quarters= new Cycle(1, 4); for (int i = 0; i <= 8; i++) { degrees += 90; quarters += 1; Console.WriteLine("degrees = {0}, quarters = {1}", degrees, quarters); } By Harshana Weerasinghe (http://www.harshana.info) 10
  • 11. Enumerations enumTitles : int{ Mr, Ms, Mrs, Dr }; Titles t = Titles.Dr; Console.WriteLine("{0}.", t);// Displays "Dr." By Harshana Weerasinghe (http://www.harshana.info) 11
  • 12. Reference Types Reference types store the address of their data, also known as a pointer, on the stack. The actual data that address refers to is stored in an area of memory called the heap. The runtime manages the memory used by the heap through a process called garbagecollection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced. By Harshana Weerasinghe (http://www.harshana.info) 12
  • 13. Built-in Reference Types By Harshana Weerasinghe (http://www.harshana.info) 13
  • 14. Strings and String Builders string s="this is some text to search"; s = s.Replace("search", "replace"); Console.WriteLine(s); string s; s = "wombat"; // "wombat“ s += " kangaroo";// "wombat kangaroo“ s += " wallaby"; // "wombat kangaroo wallaby“ s += " koala";// "wombat kangaroo wallaby koala” Console.WriteLine(s); By Harshana Weerasinghe (http://www.harshana.info) 14
  • 15. String Builder using System.Text; StringBuildersb =new StringBuilder(30); sb.Append("wombat");// Build string. sb.Append(" kangaroo"); sb.Append(" wallaby"); sb.Append(" koala"); string s = sb.ToString(); // Copy result to string. Console.WriteLine(s); By Harshana Weerasinghe (http://www.harshana.info) 15
  • 16. Create and Sort Arrays // Declare and initialize an array. int[] ar = { 3, 1, 2 }; // Call a shared/static array method. Array.Sort(ar); // Display the result. Console.WriteLine("{0}, {1}, {2}", ar[0], ar[1], ar[2]); By Harshana Weerasinghe (http://www.harshana.info) 16
  • 17. Streams Streams are another very common type because they are the means for reading from and writing to the disk and communicating across the network. The System.IO.Streamtype is the base type for all task-specific stream types. By Harshana Weerasinghe (http://www.harshana.info) 17
  • 18. Streams Cont… using System.IO; // Create and write to a text file StreamWritersw =new StreamWriter("text.txt"); sw.WriteLine("Hello, World!"); sw.Close(); // Read and display a text file StreamReadersr =new StreamReader("text.txt"); Console.WriteLine(sr.ReadToEnd()); sr.Close(); By Harshana Weerasinghe (http://www.harshana.info) 18
  • 19. Throw and Catch Exceptions StreamReadersr = new StreamReader("text.txt"); try { Console.WriteLine(sr.ReadToEnd()); } catch (Exception ex) { // If there are any problems reading the file, display an error message Console.WriteLine("Error reading file: " + ex.Message); } finally { // Close the StreamReader, whether or not an exception occurred sr.Close(); } By Harshana Weerasinghe (http://www.harshana.info) 19
  • 20. Constructing Classes public class Cycle { // Private fields int_val, _min, _max; // Constructor public Cycle(intmin,intmax) { _val = min; _min = min; _max = max; } //Property public intValue { get { return _val; } set { _val = value; } } //Method public string MyValue() { return _val.ToString(); } } By Harshana Weerasinghe (http://www.harshana.info) 20
  • 21. Inheritance – Animal Class class Animal { string m_Name; intm_Age; public Animal(string _Name) { this.Name = _Name; } public string Name { get { return m_Name; } set { m_Name = value; } } public intAge { get { return m_Age; } set { m_Age = value; } } public string Speak() { return "Hello Animal"; } } By Harshana Weerasinghe (http://www.harshana.info) 21
  • 22. Cat n Dog Classes class Cat : Animal { double m_Weight; public double Weight { get { return m_Weight; } set { m_Weight = value; } } public string Speak() { return "Meaw, Meaw"; } } class Dog : Animal { //Default Ctor public Dog() { } //Other Ctor public Dog(string _Name,int_Age) : base(_Name)//Call Base Class Ctor { this.Age = _Age; } public string Speak() { if (this.Age >3) return " Grr.. Grr.."; else return " Buhh.. Buhh.."; } } By Harshana Weerasinghe (http://www.harshana.info) 22
  • 23. Interfaces Interfaces, also known as contracts, define a common set of members that all classes that implement the interface must provide. For example, the IComparable interface defines the CompareTo method, which enables two instances of a class to be compared for equality. All classes that implement the IComparable interface, whether custom-created or built in the .NET Framework, can be compared for equality. By Harshana Weerasinghe (http://www.harshana.info) 23
  • 24. By Harshana Weerasinghe (http://www.harshana.info) 24
  • 25. Sample interface IMessage { // Send the message. Returns True is success, False otherwise. boolSend(); // The message to send. string Message { get; set; } // The Address to send to. string Address { get; set; } } class EmailMessage : IMessage { public boolSend() { throw new Exception("The method or operation is not implemented."); } public string Message { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } public string Address { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } } By Harshana Weerasinghe (http://www.harshana.info) 25
  • 26. Commonly used interfaces By Harshana Weerasinghe (http://www.harshana.info) 26
  • 27. Partial Classes - new in .NET 2.0 Partial classes allow you to split a class definition across multiple source files. The benefit of this approach is that it hides details of the class definition so that derived classes can focus on more significant portions. By Harshana Weerasinghe (http://www.harshana.info) 27
  • 28. Generics Generics are part of the .NET Framework's type system that allows you to define a type while leaving some details unspecified. Instead of specifying the types of parameters or member classes, you can allow code that uses your type to specify it. This allows consumer code to tailor your type to its own specific needs. By Harshana Weerasinghe (http://www.harshana.info) 28
  • 29. Generics Cont.. class Obj { public Object t; public Object u; public Obj(Object _t,Object _u) { t = _t; u = _u; } } class Gen<T, U> { public T t; public U u; public Gen(T _t, U _u) { t = _t; u = _u; } } By Harshana Weerasinghe (http://www.harshana.info) 29
  • 30. Generics Cont.. // Add two strings using the Obj class Objoa =new Obj("Hello, ", "World!"); Console.WriteLine((string)oa.t + (string)oa.u); // Add two strings using the Gen class Gen<string, string>ga = new Gen<string, string>("Hello, ", "World!"); Console.WriteLine(ga.t + ga.u); // Add a double and an int using the Obj class Objob =new Obj(10.125, 2005); Console.WriteLine((double)ob.t + (int)ob.u); // Add a double and an int using the Gen class Gen<double,int> gb = new Gen<double, int>(10.125, 2005); Console.WriteLine(gb.t + gb.u); By Harshana Weerasinghe (http://www.harshana.info) 30
  • 31. Generics Cont.. // Add a double and an int using the Gen class Gen<double, int> gc = new Gen<double,int>(10.125, 2005); Console.WriteLine(gc.t + gc.u); // Add a double and an int using the Obj class Objoc =new Obj(10.125, 2005); Console.WriteLine((int)oc.t + (int)oc.u); last line contains an error— the oc.tvalue is cast to an Int instead of to a double. Unfortunately, the compiler won't catch the mistake. Instead, a run-time exception is thrown when the runtime attempts to cast a double to an Int value. By Harshana Weerasinghe (http://www.harshana.info) 31
  • 32. Events By Harshana Weerasinghe (http://www.harshana.info) 32