0% found this document useful (0 votes)
55 views53 pages

Coming Up in VS 2005

The document discusses new features in .NET Framework 2.0 including generics, iterators, partial types, anonymous methods, and other enhancements. Generics allow type-safe collections and eliminate boxing for value types. Iterators make enumerating sequences easier using yield return. Partial types allow splitting class definitions across files. Anonymous methods simplify event handling code. Static classes contain only static members.

Uploaded by

Elaiya Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views53 pages

Coming Up in VS 2005

The document discusses new features in .NET Framework 2.0 including generics, iterators, partial types, anonymous methods, and other enhancements. Generics allow type-safe collections and eliminate boxing for value types. Iterators make enumerating sequences easier using yield return. Partial types allow splitting class definitions across files. Anonymous methods simplify event handling code. Static classes contain only static members.

Uploaded by

Elaiya Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

The .NET Framework 2.

0 Series

What’s coming up in Microsoft


Visual Studio 2005…

Nauzad Kapadia [MVP]


[email protected]

Quartz Systems
Enhancements in the CLR,
Compilers
Before
Generics
Generics
public class List<T>
public class List
{{
object[]
private T
private elements;
[] elements;
private int
private int count;
count;

public void
public void Add(
Add(object element)
T element) { {
if (count
if (count ==
== elements.Length)
elements.Length) Resize(count
Resize(count *
* 2);
2);
elements[count++] =
elements[count++] = element;
element;
}
}
public
public object
T this[int
this[int index]
index] { {
get
get {
{ return elements[index];
return elements[index]; }}
set
set {
{ elements[index] =
elements[index] = value;
value; }}
}
}
public List<int>
List
int intList
intList
Count { new List<int>();
= new=List();
public int Count {
get
get { return
{ return count;
count; }}
} intList.Add(1); // No
Argument
boxingis boxed
} intList.Add(2); // No
Argument
boxingis boxed
}
} intList.Add("Three"); // Compile-time
Should be an error

int i = intList[0];
(int)intList[0]; Cast
// No required
cast required
Generics In VB
Public Class List(Of T)
     Private elements() As T
     Private elementcount As Integer
     Public Sub Add(ByVal element As T)
          If elementcount = elements.Length Then
Resize(elementcount * 2)
          elements(elementcount) = element
          count += 1
     End Sub
     Public Default Property Item(ByVal index As Integer) As T
          Get
              Return elements(index)
          End Get
          Set (ByVal Value As T)
              elements(index)
Dim intList = Value
As New List(Of Integer)
          End Set
intList.Add(1)           // No boxing
     End Property
intList.Add(2)           // No boxing
intList.Add("Three")     // Compile-time error
Dim i As Integer = intList(0)   // No cast required
Generics In C++
generic<typename T>
public ref class List {
   array<T>^ elements;
   int count;
public:
   void Add(T element) {
      if (count == elements->Length) Resize(count * 2);
      elements[count++] = element;
   }
   property T default [int index] {
      T get() { return elements[index]; }
      void set(T value) { elements[index] = value; }
   }
   property int Count {
      intList<int>^ intList
get() { return = gcnew
count; } List<int>();
   } intList->Add(1);           // No boxing
}; intList->Add(2);           // No boxing
intList->Add("Three");     // Compile-time error
 
int i = intList[0];        // No cast required
Generics
Why generics?
Type checking, no boxing, no downcasts
Reduced code bloat (typed collections)

How are C# generics implemented?


Instantiated at run-time, not compile-time
Checked at declaration, not instantiation
Work for both reference and value types
Complete run-time type information
Generics
Type parameters can be applied to
Class, struct, interface, and delegate types
Methods
Type parameters can have constraints
One base class, multiple interfaces, new()
class Dictionary<K,V>:
Dictionary<K,V> where
IDictionary<K,V>
K: IComparable
{ where K: IComparable<K>
public V:
where void
IKeyProvider<K>,
Add(K key, V value)
IPersistable,
{ new()
{ …
public
if (key.CompareTo(x)
((IComparable)key).CompareTo(x)
void Add(K key, V==value)
0) {…}
{ == 0) {…}

}
}
Generics
void Foo<T>() {
T.default T x = null; // Error
T y = T.default; // Ok
}

Null checks void Foo<T>(T x) {


if (x == null) {
throw new FooException();
}

Type casts }
void Foo<T>(T x) {
int i = (int)x; // Error
int j = (int)(object)x; // Ok
}
Iterators
foreach relies on “enumerator pattern”
GetEnumerator() method

foreach (object obj in list) {


DoSomething(obj);
} Enumerator e = list.GetEnumerator();
while (e.MoveNext()) {
object obj = e.Current;
DoSomething(obj);
}

foreach makes enumerating easy


But enumerators are hard to write!
Iterators public IEnumerator GetEnumerator() {
return new __Enumerator(this);
}

Method that incrementally computes and


private class __Enumerator: IEnumerator
{
returns a sequence of values
object current;
int state;

yield return and yield breakpublic bool MoveNext() {


switch (state) {
Must return IEnumerator or IEnumerable
case 0: …
case 1: …
case 2: …

}
public class List }
{
public IEnumerator GetEnumerator() { public object Current {
for (int i = 0; i < count; i++) { get { return current; }
}
yield return elements[i]; }
}
}
}
Iterators
public class List<T>
{
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < count; i++) yield return elements[i];
}

public IEnumerable<T> Descending() {


for (int i = count - 1; i >= 0; i--) yield return elements[i];
}

public IEnumerable<T> Subrange(int index, int n) {


for (int i = 0; i < n; i++) yield return elements[index + i];
}
} List<Item> items = GetItemList();
foreach (Item x in items) {…}
foreach (Item x in items.Descending()) {…}
foreach (Item x in Items.Subrange(10, 20)) {…}
Partial Types
public partial class Customer
{
private int id; public class Customer
private string name; {
private string address; private int id;
private string name;
private List<Orders> orders;
} private string address;
private List<Orders> orders;

public partial class Customerpublic void SubmitOrder(Order order) {


{ orders.Add(order);
public void SubmitOrder(Order
} order) {
orders.Add(order);
} public bool HasOutstandingOrders() {
return orders.Count > 0;
public bool HasOutstandingOrders()
} {
return orders.Count >} 0;
}
}
Anonymous Methods
class MyForm : Form
{
ListBox listBox;
TextBox textBox;
Button addButton;

public MyForm() {
listBox = new ListBox(...);
textBox = new TextBox(...);
addButton = new Button(...);
addButton.Click += delegate
new EventHandler(AddClick);
{
} listBox.Items.Add(textBox.Text);
};
}void AddClick(object sender, EventArgs e) {
} listBox.Items.Add(textBox.Text);
}
}
Anonymous Methods
Allows code block in place of delegate
Delegate type automatically inferred
Code block can be parameterless
Or code block can have parameters
In either case, return types must match

button.Click += delegate { MessageBox.Show("Hello"); };

button.Click += delegate(object sender, EventArgs e) {


MessageBox.Show(((Button)sender).Text);
};
Anonymous Methods
Method group conversions
Delegate type inferred when possible

using System;
using System.Threading;

class Program
{
static void Work() {…}

static void Main() {


Thread t = new Thread(Work
new ThreadStart(Work)
); );
t.Start();
}
}
Other Enhancements
Static classes
Can contain only static members
Cannot be type of variable, parameter, etc.
System.Console, System.Environment, etc.

public static class Math


{
public static double Sin(double x) {…}
public static double Cos(double x) {…}

}
Other Enhancements
Property accessor accessibility
Allows one accessor to be restricted further
Typically set {…} more restricted than get {…}

public class Customer


{
private string id;

public string CustomerId {


get { return id; }
internal set { id = value; }
}
}
Enhancements in the VB.NET
Grammar
Unsigned Types
Full support in the language
Full platform parity
Easier Win32 Api calls and translation
Memory and performance win
Dim sb As SByte = -4 ‘Error:negative
Dim us As UShort
Dim ui As UInteger
Dim ul As ULong

‘Full support in VisualBasic modules


If IsNumeric(uInt) Then
‘ Will now return true
End If
Operator Overloading
Create your own base types
Class Addr
Private mString As String

Property Value() As String


Get
Return mString
End Get
Set (value As String)
If Valid(value) Then
mString = value
End If
End Set

Shared Operator &(ad1 As Addr, ad2 As Addr) _


As Addr

Return New Addr(ad1.Value & ad2.Value)


End Operator
Explicit Array Bounds
Arrays are still zero based
You can now specify the lower and upper
bounds in the declaration

‘In VS 2002 and 2003, you did it this way


Dim x(10) As Integer

‘Now you can also declare it like this


Dim y(0 To 10) As Integer
Using Statement
Acquire, Execute, Release

Fast way correctly release resources


Easier to read than Try, Catch, Finally
Couple with Dispose Pattern stub gen
‘Using block disposes of resource
Using fStr As New FileStream(path, FileMode.Append)

For i As Integer = 0 To fStr.Length


fStr.ReadByte()
Next

‘End of block will close stream


End Using
Even more productive
ASP.NET
ASP.NET Enhancements
Data Source Controls
New DataBound Controls - GridView
ASP.NET Master Pages
Web Sites Today
Header
Navigation

Content

Footer
Web Sites Today
Header
Navigation

Content

Footer
Current Web Approaches
User Controls
Include File
Anything else that you can dream
of……….
Current Web Approaches
User Controls and Include Files

Encapsulate each user interface part


into a user control
Reference each user control from
the page
Challenges:
Mismatched html tags
Ex table tag for the header and footer
Each page contains references to the appropriate
User Controls
Must touch every page to update controls
Introducing Master Page
Enable flexible and reusable user
interface templates
Contains
Page layout:
(HTML Table)
Content:
(Header, Footer, ML, HTML,ASP.NET controls)
Place holder regions for content in the pages
Benefits
Consistent Page Layout
Shared UI & Code Elements
Can be defined programmatically and declaratively
Rich Visual Studio “Whidbey” designer-support
How Master Pages Work
Design time
MySite.master Default.aspx
<%@ Master %> <%@ Page Master=“~/mySiteMaster” %>
Header
<asp:Content
Navigation ContentPlaceHolderId=MainContent />

<asp:ContentPlaceHolder
Id=MainContent />

Footer
How Master Pages Work
Runtime
http://serverName/SiteName/Default.aspx

Header
Navigation

Default page content

Footer

Default.aspx’s content replace the contentPlace holder


of the master page at runtime
How To Create A Master Page
1. Create a text file with extension *.master
2. Define the master directive
<%@ Master .. %>
3. Add content
Can contain any page content
4. Define place holder regions :
Use the <asp:contentplaceholder ..> control
Add default content (optional)
Creating A Master Page
<%@ Master language="VB"%>
<html>
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<table>
<tr ><td><h1><!-- Header goes here --></h1></td></tr>
<tr> <td><h2><!-- Navigation goes here --></h2></td></tr>
<tr><td>
<!-- Content Place holder without default content -->
<asp:contentplaceholder id=“LeftSideContent" runat="server“/>
</td>
<td>
<!-- Content Place holder withdefault content -->
<asp:contentplaceholder id=“RightSideContent" runat="server">
<asp:label runat=Server id=foo>Default content!!!</asp:label>
</asp:contentplaceholder>
</td></tr></table>
</form>
</body>
</html>

MySite.master
Creating A Master Page
<%@ master language="VB"%>
<html>
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<table>
<tr ><td><h1><!-- Header goes here --></h1></td></tr>
<tr> <td><h2><!-- Navigation goes here --></h2></td></tr>
<tr><td>
<!-- Content Place holder without default content -->
<asp:contentplaceholder id=“LeftSideContent" runat="server“/>
</td>
<td>
<!-- Content Place holder withdefault content -->
<asp:contentplaceholder id=“rightSideContent" runat="server">
<asp:label runat=Server id=foo>Default content!!!</asp:label>
</asp:contentplaceholder>
</td></tr></table>
</form>
</body>
</html>

MySite.master
How To Use A Master Page
1. Create an ASP.NET page
2. Add the master attribute to the
page directive
<%@ page master= %>
3. Add content for the master place
holder regions
Use the <asp:content ..> control
The ContentPlaceHolderId define the place holder region
to redefine
Can contain any page content
Master Page
Device Specific
Mechanism to override the master page based
on the requesting device
Improve the end user experience
<%@ page language="VB" master="~/Mysite.master"
ie:master=“~/MysiteIE.master”
Longhorn:master=“~/MysiteLonghorn.master”
Mozilla:master=“~/MysiteMozilla.master” %>

<%@ page language="VB" master="~/Mysite.master" %>


<asp:content id="Content1" contentplaceholderid=“LeftSideContent">
<H2>Navigation </h2>
<asp:treeview id=“Navigation tree" runat="server“ datasourceid=“NavSource”/>
</asp:content>
<asp:content id="Content1" contentplaceholderid=“RightSideContent">
<asp:label runat="server">Support section</asp:label>
</asp:content>

Default.aspx
Master Page
Nested Master
Enable developers a way to define a Master
page for a master
Useful when site enforces a layout but allow
site section with a nested to define sub-layouts
Master Pages
Nested Master

Content Place Holder

Site.Master
Master Pages
Nested Master

Content Place Holder

DevelopmentTools.Master
Master Pages
Nested Master

Partners.aspx HowToBuy.aspx
Master Page
Programmability
Page can access the master page
Page.Master property
Can access public properties in Master
FindControl to access controls
Page can set its master page dynamically
Page.MasterFilePath property (Beta 1)
Dynamically set Master from the page
Ex. Co branding of the site
Will be available in Beta timeframe
Master Page
Configuration
Page level configuration
<%@ master language="VB“ master="~/Mysite.master" %>

Site wide configuration


<configuration>
<system.web>
<pages master="MySite.Master" />
</system.web>
</configuration>
Themes
Application Themes
\themes directory of application
Theme name is defined by the directory name
Global Themes
Install by default
Located in the framework directory
<WINDIR>\ASPNET_Client\system_web\2_0_*\themes
Built in themes
Basic Blue – simple no CSS
Smoke and glass – more stylized with CSS
How To Create A Theme
1. Create a Themes directory
2. Create a Directory in the theme directory
3. Add the skins and other files
How To Create A Skin
1. Create text file with .skin extension
2. Add control-level definitions:
Properties (if property exposed as theme-able)
Including Databinding code
Templates
Collections
3. To create an optional skin for a
pre-define control
Define the SkinId property of the control
Creating A Skins
< !– Default Skin --!>
<asp:label runat="server"
font-names=“verdana,arial" font-size="10pt“
ForeColor="#000066" BackColor="transparent" />

< !– Title Skin --!>


<asp:label runat="server" id=foo skinid=Title
font-names="verdana,arial" font-size="18pt"
ForeColor="#000066" BackColor="transparent"
font-bold="true" font-underline="true" />

LabelControlSkins.Skins
Using Theme And Skin
1. On the page
Define a reference to the theme
Note: Links are page relative
2. On a control
Define nothing. If a default control skin exists it
will be picked up
Define the skinid on the control; will utilize the
named skin
3. Prevent themeing by:
EnableThemeing=false on page or control
All child controls inherit the parent setting
Using Theme And Skin
<%@ Page language="VB“ theme=“BabyBoy”%>
<html><head runat="server"></head><body><form id="form1" runat="server">
<table>
<tr ><td><h1><asp:label runat=Server skinid=Title id=foo text=Default /></td></tr>
/>
<tr> <td><h2><!-- Navigation goes here --></h2></td></tr>
<tr><td>
<!-- Content Place holder without default content -->
<asp:contentplaceholder id=“LeftSideContent" runat="server“/>
</td>
<td>
<!-- Content Place holder withdefault content -->
<asp:contentplaceholder id=“RightSideContent" runat="server">
<asp:label runat=Server id=foo>Default content!!!</asp:label>
</asp:contentplaceholder>
</td></tr></table>
</form>
</body>
</html>

Default.aspx
Themes
Configuration
Page level configuration
<%@ master language="VB“ theme=“BasicBlue" %>

Site wide configuration


<configuration>
<system.web>
<pages theme="SmokeAndGlass" />
</system.web>
</configuration>
More ASP.NET 2.0 Goodies…
Cross Page Posting
Validation Groups
New Image Generation Service in 2.0
<asp:DynamicImage> Control
Secure non ASP.NET webs with ASP.NET 2.0
2.0 enables devs to use ASP.NET forms authentication for non-ASP.NET files
Dynamic Files: .ASP, .PHP, .JSP
Requires IIS 6.0 (new ExecuteUrl feature)
Client-side click event handlers on controls
Focus mechanisms: Page.SetFocus(control), TextBox.Focus() (beta)
Default button and focus
Validation Error Focus
Auto-scroll maintenance on postback (beta)
Summary…
.NET Framework 2.0 and VS 2005 increase
developer productivity
CLR 2.0 adds support for template based
development for all managed compilers to
consume
ASP.NET 2.0 focuses on doing more with less
code
WinForms 2.0 have been enhanced for
performance, responsiveness and powered with
more controls!
© 2001 Microsoft Corporation. All rights reserved.

You might also like