.Net Framework 2 fundamentals

32
CHAPTER -1 FRAMEWORK FUNDAMENTALS Harshana Weerasinghe http://www.harshana.i nfo http://Blog.harshana. info

description

.Net Framework 2 fundamentals

Transcript of .Net Framework 2 fundamentals

Page 1: .Net Framework 2 fundamentals

CHAPTER -1FRAMEWORK FUNDAMENTALS

Harshana Weerasinghehttp://www.harshana.infohttp://Blog.harshana.info

Page 2: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.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.

Page 3: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 3

Types

Mainly 2 types in Microsoft .Net Value Type Reference Type

Page 4: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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.

Page 5: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 5

Built-in Value Types

Page 6: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 6

Other Value Types

Page 7: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 7

Declare Value Types

<type> <VariableName> [= <Default Value>];

bool bolTestVariable = false;Nullable<bool> b = null; Orbool? b = null;

Page 8: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 8

Create User-Defined Typesstruct Cycle{ // Private fields int _val, _min, _max;

// Constructor public Cycle(int min, int max) { _val = min; _min = min; _max = max; }

public int Value { 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 int ToInteger() { return Value; }}

Page 9: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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, int arg2){ arg1.Value -= arg2; return arg1;}

Page 10: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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);}

Page 11: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 11

Enumerations

enum Titles : int { Mr, Ms, Mrs, Dr };

Titles t = Titles.Dr;Console.WriteLine("{0}.", t); //

Displays "Dr."

Page 12: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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 garbage collection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced.

Page 13: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 13

Built-in Reference Types

Page 14: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 14

Strings and String Buildersstring 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);

Page 15: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 15

String Builder

using System.Text;

StringBuilder sb = 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);

Page 16: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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]);

Page 17: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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.Stream type is the base type for all task-specific stream types.

Page 18: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 18

Streams Cont…

using System.IO;

// Create and write to a text fileStreamWriter sw = new StreamWriter("text.txt");sw.WriteLine("Hello, World!");sw.Close();

// Read and display a text fileStreamReader sr = new

StreamReader("text.txt");Console.WriteLine(sr.ReadToEnd());sr.Close();

Page 19: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 19

Throw and Catch ExceptionsStreamReader sr = 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();}

Page 20: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 20

Constructing Classes

public class Cycle{ // Private fields int _val, _min, _max; // Constructor public Cycle(int min, int

max) { _val = min; _min = min; _max = max; }

//Property public int Value { get { return _val; } set { _val = value; } }

//Method public string MyValue() { return _val.ToString(); } }

Page 21: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 21

Inheritance – Animal Classclass Animal{ string m_Name; int m_Age;

public Animal(string _Name)

{ this.Name = _Name;

}

public string Name { get { return m_Name; }

set { m_Name = value; } }

public int Age { get { return m_Age; } set { m_Age = value; } } public string Speak() { return "Hello Animal"; }

}

Page 22: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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..";

}}

Page 23: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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.

Page 24: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 24

Page 25: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 25

Sampleinterface IMessage{ // Send the message. Returns True is

success, False otherwise. bool Send(); // The message to send. string Message { get; set; } // The Address to send to. string Address { get; set; }}

class EmailMessage : IMessage{ public bool Send() { 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."); } }}

Page 26: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 26

Commonly used interfaces

Page 27: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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.

Page 28: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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.

Page 29: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 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; }}

Page 30: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 30

Generics Cont..

// Add two strings using the Obj classObj oa = new Obj("Hello, ", "World!");Console.WriteLine((string)oa.t + (string)oa.u);

// Add two strings using the Gen classGen<string, string> ga = new Gen<string, string>("Hello, ",

"World!");Console.WriteLine(ga.t + ga.u);

// Add a double and an int using the Obj classObj ob = new Obj(10.125, 2005);Console.WriteLine((double)ob.t + (int)ob.u);

// Add a double and an int using the Gen classGen<double, int> gb = new Gen<double, int>(10.125, 2005);Console.WriteLine(gb.t + gb.u);

Page 31: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 31

Generics Cont..

// Add a double and an int using the Gen classGen<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 classObj oc = new Obj(10.125, 2005);Console.WriteLine((int)oc.t + (int)oc.u);

last line contains an error — the oc.t value 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.

Page 32: .Net Framework 2 fundamentals

By Harshana Weerasinghe (http://www.harshana.info) 32

Events