New Features of C# 3.0


One of the frequently asked questions in interview is “What are the new features added in C# 3.0”.

Following are the new feature added in C# 3.0

1. Automatic Properties

Automatic properties provide you with a shorthand method for defining a new property.

public class AutomaticProperties

{

// Automatic Properties

public int Id { get; set; }

public string Description { get; set; }

// Normal Property

private decimal _Price;

public decimal Price

{

get { return _Price; }

set { _Price = value; }

}

}

2. Initializers

we can use initializer  to reduce the amount of work it takes to create the new instance of a class. Syntax is like that.

public class Product

{

public int Id { get; set; }

public string Name { get; set; }

public decimal Price { get; set; }

}

Product product2 = new Product {Id=1, Name=”Laptop Computer”, Price=40000.00  };

In C# 2.0 we have to initialize like this

Product product1 = new Product();

product1.Id = 1;

product1.Name = “Laptop Computer”;

product1.Price = 40000.00;

3. Type Inference

We can create the variable like JavaScript. C# Compiler determine the type of variable at compile time.

For example in C#, we can write like this

var message = “Hello World!”;

4.Anonymous Types

Anonymous Types are essentially compiler generated types

Anonymous types are useful when

  • you don’t want to do the work to create the class.
  • You don’t know the return data type

For example, I can write like this

Var customer=new Customer {FirstName=”Chandradev” LastName=”Prasad”}

5.Lambda Expression

It is used to wired of the event. For example,if you want to programmatically wire up the click event handler to button Control. Then we can write like this.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Init()

{

BtnSubmit.Click += (Sender, e) => lblMsg.Text = DateTime.Now.ToString();

}

protected void Page_Load(object sender, EventArgs e)

{

}

}

Advertisement

How to create Slid show in asp.net using Repeater Control?


Hi,

There are so many methods to create Slid show functionality in asp.net,

 But one of the easiest method to create Slid Show is using JavaScript and CSS.

Step1: Create a Image folder in Solution Explorer and keep some Images there.

Step2: Include this Css and Javascript Code in .aspx page Like this

<%@ Page Title=”Home Page” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true”

    CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<asp:Content ID=”HeaderContent” runat=”server” ContentPlaceHolderID=”HeadContent”>

</asp:Content>

<asp:Content ID=”BodyContent” runat=”server” ContentPlaceHolderID=”MainContent”>

<div style=”margin-left:200px;”><h1>Repeater Slide Show</h1></div>

<style type=”text/css”>

        .photo

        {

            width:400px;

            margin-left:200px;

            margin-top:40px;

            background-color:white;

            filter:progid:DXImageTransform.Microsoft.Fade(duration=2);

        }

    </style>

    <script type=”text/javascript”>

        var photos = new Array();

        window.setInterval(showImage, 5000);

        function showImage() {

            if (photos.length > 0) {

                var index = Math.floor(Math.random() * photos.length);

                var image = document.getElementById(‘imgPhoto’);

                image.src = photos[index];

                if (image.filters) {

                    image.filters[0].Apply();

                    image.filters[0].Play();

                }

            }

        }       

    </script>

<img id=”imgPhoto” alt=”” />

    <script type=”text/javascript”>

    <asp:Repeater

        id=”rptPhotos”

        Runat=”server”>

        <ItemTemplate>

        <%# Eval(“Name”, “photos.push(‘Photo/{0}’)”) %>

        </ItemTemplate>

    </asp:Repeater>

    showImage();

    </script>

</asp:Content>

 

Step3: Write this code in Code behind file

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            DirectoryInfo dir = new DirectoryInfo(MapPath(“~/Photo”));

            rptPhotos.DataSource = dir.GetFiles(“*.jpg”);

            rptPhotos.DataBind();

        }

    }

}

Then you will get output like this