Showing posts with label C#-4.0. Show all posts
Showing posts with label C#-4.0. Show all posts
Wednesday, April 21, 2010

Visual Studio 2010 New Feature-Generate From usage.

Visual Studio 2010 is Great IDE and I am exploring everyday a new things. Recently I was working with it and I have found a great features called Generate from usage. This feature is allow us to create a class from the generation from usage. Let’s take a sample example for that. Let’s Create a simple example where we will use a product class which is not there in project. Then from generate from usage feature we will create a product class. Here is code where product class does not exist in my console application project.

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Product objProduct = new Product();
}
}
}
Now lets create a new class from this new feature like following. Goto Product class line right click and then click generate->Class as shown in image below.GenerateClass

That’s is it will create a new class there in your solution like this.

ProductClass

So with this feature you will create as per you usage in class. Hope this will help you.Happy programming..

Shout it
kick it on DotNetKicks.com
Share:
Tuesday, April 6, 2010

ASP.NET 4.0 –List View Control Enhancement

With asp.net 3.5 we are having a great control called list view its provide almost all the functionality like grid view and its rendering is also easy but in asp.net 3.5 you need to specify a layout template where in asp.net 4.0 layout template is not required. So no need to include one extra placeholder as layout template. Like in asp.net 3.5 you nee do render formview control like following.

<asp:ListView ID="myListView" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="LayoutPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<% Eval("myDataBaseColumn")%>
</ItemTemplate>
</asp:ListView>
This can be replaced by following in asp.net 4.0. Now no need to for layout template like following.
<asp:ListView ID="myListView" runat="server">
<ItemTemplate>
<% Eval("myDataBaseColumn")%>
</ItemTemplate>
</asp:ListView>
Hope this will help you..Happy coding..

Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:

ASP.NET 4.0-FormView Control Enhancement –RenderOuterTable Property

Form view control is part of asp.net standard control suite since asp.net 2.0. We are using it when we need to display one record at a time. ASP.NET 4.0 has made form view control more enhanced.Now its has a property called RenderOuterTable which will decide the whether outer table was render in form view or not. So now the html generated by formview control is more css friendly and easy to manage. Like following we can define the property of from view control.

<asp:FormView ID="myFormView" runat="server" RenderOuterTable="true">
<ItemTemplate>
<div>
this is form view inner content
</div>
</ItemTemplate>
</asp:FormView>
If we made redneroutertable=”false” then it will render html like following.
<div>
this is form view inner content
</div>
So now form view control is more css friendly in asp.net and its easy to manage markup generated by asp.net.

Shout it
kick it on DotNetKicks.com
Share:
Saturday, March 27, 2010

.NET 4.0- A new method StringBuilder.Clear() to clear stringbuilder

With Microsoft.NET 4.0 we have a convenient method called clear method which will clear string builder. This method is very use full where we need to use string builder for multiple string manipulation. So we don’t need to create a separate string builder for it. Let’s take a simple example which will print string builder string before,after clear so we can see how it works. Following is simple console application for this.

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Text.StringBuilder myStringBuilder =
new System.Text.StringBuilder(string.Empty);
myStringBuilder.Append("This is my string");
Console.WriteLine("String Builder Output Before Clear:{0}",
myStringBuilder.ToString());
myStringBuilder.Clear();
Console.WriteLine("String Builder Output After Clear:{0}",
myStringBuilder.ToString());
myStringBuilder.Append("This is my another string");
Console.WriteLine("String Builder Output After new string:{0}",
myStringBuilder.ToString());
}
}
}
Here is a output as expected.Strinbuilder

Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:
Monday, March 22, 2010

C# 4.0-string.IsNullOrWhiteSpace()

We already have string.IsNullOrEmpty() method to check whether the string is null or empty.Now with Microsoft.NET Framework 4.0 there is a new another method that is called string.IsNullOrWhiteSpace() which will also check whether string is having whitespaces or not with null and empty string.This method is help full where we can check string is having whitespace rather then its empty or null. It will return a true if a string is empty,null or its having whitespaces. Let’s create a simple example and will see how its works. Here we are going test it with 4 options Empty string,Normal String,White spaces and Null String. Following is a code for that.

class Program
{
static void Main(string[] args)
{
string TestString = string.Empty;
Console.WriteLine("Test with empty string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = "Normal String";
Console.WriteLine("Test with normal string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = " ";
Console.WriteLine("Test with whitespace:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = null;
Console.WriteLine("Test with null:{0}",
string.IsNullOrWhiteSpace(TestString));
Console.ReadLine();
}
}
Following is a output of the above code here you can see its returning false with Normal string and for all other options its returning true as expected.
image

Shout it
kick it on DotNetKicks.com
Share:
Saturday, March 20, 2010

Visual Studio 2010-Automatically Adjust Visual Experience.

Visual studio 2010 is great IDE and i am discovering everyday some thing new. Today i have discovered one of fantastic option to increase performance of your visual studio IDE. Visual Studio 2010 has option when we enabled it it will automatically adjust the visual and client experience as per your hardware configuration. It will increase the performance of visual studio and productivity in lower hardware also. You can find that checkbox under Tools->General-> Automatically adjust visual experience based on client performance. There are two checkbox is given one for above and another for Rich User Experience but they may decrease your visual studio performance. Like following.

Visual Studio 2010, User Experience

Shout it
kick it on DotNetKicks.com
Share:

Indexer class in C#

While taking interviews for Microsoft.NET i often ask about indexer classes in C#.NET and i found that most of people are unable to give answer that how we can create a indexer class in C#.NET. We know the array in C#.NET and each element in array can be accessed by index same way for indexer class a object was class can be accessed by index. Its very easy to create a indexer class in C#. First lets looks some points related to indexer class in C#.

  • Indexer class object can be accessed by index only.
  • We can use same mechanism to access object of indexer class as we are doing it for array.
  • You can can specify any valid C# type as return type of indexer classes.
  • You must have to create a property with parameter using this keyword for indexer class.
  • You can also implement multi parameter indexer also.

Let’s create simple indexer class with single parameter and for string type.

 public class MyIndexer
{
private string[] MyIndexerData;
private int SizeOfIndexer;

//declaring cursor to assing size of indexer
public MyIndexer(int sizeOfIndex)
{
this.SizeOfIndexer = sizeOfIndex;
MyIndexerData = new string[SizeOfIndexer];
}

public string this[int Position]
{
get
{
return MyIndexerData[Position];
}
set
{
MyIndexerData[Position] = value;
}
}
}
Now we done with indexer class and lets create object of that indexer class and will see output of our console application also. Following is the code for that.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indexSize=5;
MyIndexer objMyIndexer = new MyIndexer(indexSize);
for (int i = 0; i < indexSize; i++)
{
objMyIndexer[i] = i.ToString();
Console.WriteLine("Indexer Object Value-[{0}]: {1}",
i, objMyIndexer[i]);
}
Console.ReadKey();
}
}
}
and Here is the output for the indexer class console application.

Indexer Class Example in C#,Indexer Class,C#

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Microsoft.NET 4.0/VB.NET 10 Automatic Properties

In C# we are having automatic properties since C# 3.5 framework but now with Microsoft.NET 4.0 Framework VB.NET 10.0 version we are also having automatic properties for VB.NET Also.

Like in C# we can define automatic like following.

Public string TestProperty
{
get;
set;
}
Same way we can define in automatic Property in VB.NET as follows.
Public Property TestProperty As String 
You can use this properties any where in class same as C# automatic property.

Shout it
kick it on DotNetKicks.com
Share:
Monday, March 15, 2010

Rename feature in Visual Web Developer 2010/Visual Studio 2010

Visual web developer is great tool and I am playing more and more with it and every time I am discovering some new features of it. Recently I have discovered a very cool feature of it. I want to rename a variable in visual studio 2010 Web Developer express edition and I found a great refractor tool for that which will rename that variable in all the instance and all the methods. First you need to select variable which you want to rename and then you need to Right Click ->Refractor->Rename. You can also invoke that via its shortcut Ctrl + R,Ctrl +R and it will be available For reference see the below screenshot.

Rename variable featuers in visual studio 2010

Once you click the rename which will have a dialog box open which will ask for new name or variable like following.

RenameDilaog option in visual web developer 2010

It is also having options for search in comments and search in string. Search in comment will search and replace in comment and search in string will replace word for any string which contains that variable name. Once you put new name in dialog box it will display a preview of replaced strings like following. Here also you can select where you need to rename and where you don’t want to rename and this is really cool. :)

Preview dialog for visual web developer 2010

Shout it
Share:
Saturday, February 20, 2010

How to remove all the cache object from asp.net application

Cache object in .NET technologies very power full feature and we all use it for increasing out application performance. In one of my project i need to remove all of the my cache once some task was completed. So i have done some digging and i have found some beautiful way of removing all the cache object. As you know cache it self is dictionary entry object so we can always remove them via loop here are some of code examples from which we can remove the all the cache object.

//Following namespace you need.
using System.Collections;
using System;
using System.Collections.Generic;
using System.Web;

IDictionaryEnumerator cacheEnumerator = HttpContext.Current.Cache.GetEnumerator();

while (cacheEnumerator.MoveNext())
{
HttpContext.Current.Cache.Remove(cacheEnumerator.Key.ToString());
}
Here is the another example of removing cache.
foreach (DictionaryEntry dEntry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(dEntry.Key.ToString());
}
Please make sure this is way only for the cache object it will not going to remove other cache like output cache etc.



kick it on DotNetKicks.com
Shout it
Share:
Monday, November 30, 2009

C# 4.0-Dynamic Data Type, Difference between var and dynamic

C# 4.0 introduces a new keyword called 'Dynamic'. It can consume any object anything. Let's see some examples for that.
dynamic intExample = 1;
Response.Write(intExample);

dynamic floatExample = 2.5;
Response.Write(floatExample);

dynamic stringExample = "DotNetJaps";
Response.Write(stringExample);
It will print out put on web page as following.

C#-4.0-Dynamic-Keyword-Diffrence-Between-Var-And-Dynamic-Type

Now, you will have question what's new in that. It could be also done with var keyword . Yes, you can do same thing with var but dynamic keyword is slightly different then var keyword.

Diffrence between var and dynamic keyword:

var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. I know you guys don't believe me without example. So let's take example of string.
string s = "DotNetJaps-A Blog for asp.net,C#.net,VB.NET";
var varstring=s;
Response.Write(varstring.MethodDoesnotExist());
Now try to compile above code it will not compile and it gives a error that 'string' does not contain a definition for 'MethodDoesnotExist' and no extension method 'MethodDoesnotExist' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?'. So var keyword knows that what value or object or anything assigned to it. Now lets try to compile same code with dynamic example like following.
string s = "DotNetJaps-A Blog for asp.net,C#.net,VB.NET";
dynamic varstring=s;
Response.Write(varstring.MethodDoesnotExist());
This will compile. So it is the difference between dynamic and var keyword. With dynamic keyword anything assigned to it like property,objects operators anything that will be determined at compile time. It can be useful while we are doing programming with com,JavaScript which can have runtime properties.

Happy Programming...

Technorati Tags: ,,,
Shout it

kick it on DotNetKicks.com
Share:
Friday, November 20, 2009

ASP.NET 4.0 New Feature- RepeatLayout property for CheckBoxList and RadioButtonList Controls.

ASP.NET 4.0 having many many new features. One of them is the RepeatLayout property for the CheckBoxList and RadioButtonList controls. The property is useful when we need decide how this controls will be render as html when its load in browser .



Code Snippet



  1. <div>
  2. <asp:CheckBoxList ID="checkboxList" runat="server" RepeatLayout=UnorderedList>
  3. <asp:ListItem Text="Checkbox1" Value="1"></asp:ListItem>
  4. <asp:ListItem Text="Checkbox2" Value="2"></asp:ListItem>
  5. </asp:CheckBoxList>
  6. </div>





There are four options available there.

  1. Flow
  2. OrderedList
  3. Table
  4. UnorderedList

Flow: This option will render control with span tag. This option will be better when you need tables less html for your site.

OrderedList:This option will load contriol as orderedlist. It will load html with <ol> and <li> Tags.

Table:If you love table structure then this is the option for you it will load html with <table><tr> and <td> tags.

UnorderedList: This option will load control as UnorderedList. It will load with <ul> and <li> tags.

So this property will useful when you want to have more control over html rendering in your browser as per your requirement.

Happy Programming...


Shout it
kick it on DotNetKicks.com
Share:
Saturday, November 14, 2009

C# 4.0 New feature - Named Parameter

C# 4.0 has one new cool features which is the named parameter. Suppose you have so many parameter in function and when you call them its hard to remember the sequence of the code now with the named parameter you can have name of the parameter with value like Parameter:value and one another cool feature of the name parameter is the you don't need to pass the parameter in exact sequence. Let see the following example



Code Snippet



  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. foreach (int i in Square( max:10,min:1))
  4. {
  5. Response.Write(i.ToString() + " ");
  6. }
  7. }

  8. public static IEnumerable<int> Square(int min, int max)
  9. {
  10. for (int i = min; i < max; i++)
  11. {
  12. yield return i*i;
  13. }
  14. }





In above example I am having a function called square with two parameter min and max and having a function which will return the square of the given range between min and max. See the for each loop carefully. I have named parameters and another thing you will notice that i have changed the sequence of parameter but still its working fine. So this is a very cool new feature from the C# 4.0.

Happy Programming...

Technorati Tags: ,

kick it on DotNetKicks.com
Shout it
Share:

Visual Studio 2010- New Features Selected Text.

I am loving more and more visual studio 2010 for some of the cool things. One of the new feature i have found in visual studio 2010 is the selected text. In the earlier version of visual studio 2010 when we select tax then its not maintaining the highlighted code but with visual studio 2010 it is maintain the colored syntax. Isn't that cool?. We can still know that what we are selecting. Like following.

Text

Shout it
Share:
Tuesday, November 10, 2009

Yield statement in C#

Yield statement is one of most interesting statement in the C# 2.0. I have heard about lot of yield statement but today i have learned about yield statement. Yield statement can be used to return multiple object from a method while retaining its state. You can get item in sequence with help of yield.Let take a simple example to see the power or yield statement.

Let's create one example which will help you the understand how its works Let create a function will return the square each time this function is called.



Code Snippet



  1. public static IEnumerable<int> Square(int min, int max)
  2. {
  3. for (int i = min; i < max; i++)
  4. {
  5. yield return i*i;
  6. }
  7. }





Now each time this method is called it will return the square of current value within a given range and its also maintains the state between calls. Let create for each loop to call the the square function above.



Code Snippet



  1. foreach (int i in Square(1, 10))
  2. {
  3. Response.Write(i.ToString() + " ");
  4. }





And here will be the output in the browser.

image

So it will create IEnumerable without implementing any interface and lesser code.

Happy Programming..

Technorati Tags: ,,

kick it on DotNetKicks.com
Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews