Difference Between Shadowing and Override in C#

Your Ad Here

Difference between Shadowing and Override in C#

CriterionShadowingOverriding
PurposeProtecting against a subsequent base class modification introducing a member you have already defined in your derived classAchieving polymorphism by defining a different implementation of a procedure or property with the same calling sequence
Redefined elementAny declared element typeOnly a procedure (Function or Sub) or property
Redefining elementAny declared element typeOnly a procedure or property with the identical calling sequence1
AccessibilityAny accessibilityCannot expand the accessibility of overridden element (for example cannot override Protected with Public)
Readability and writabilityAny combinationCannot change readability or writability of overridden property
Keyword usageShadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specifiedOverridable required in base class; Overrides required in derived class
Inheritance of redefining element by classes deriving from your derived classShadowing element inherited by further derived classes; shadowed element still hidden2Overriding element inherited by further derived classes; overridden element still overridden

 

Subscribe
Posted in Labels: kick it on DotNetKicks.com | 0 comments Links to this post

Lazy Evaluation Feature in C# 4.0(VS2010)

Your Ad Here

Lazy Evaluation Feature in C#

In any programming language , there can be two types of evaluation supported

1: Eager evaluation
2: Lazy evaluation

What is Eager evaluation?

Eager evaluation is the evaluation of the expressions in any programming language without checking its usage in the program whether that expression value is being used in the program or not . For example

Int x = 2 ;
Int y = 3;

Int add( x , x+y ){
Return x }

In above given function , we eagerly evaluated the x+y which is a parameter to the function which is never used in the function body.

What is Lazy evaluation?

Lazy evaluation says that the evaluation of expression should not be performed until it is required in the program
Lets take the same example

Int x = 2 ;
Int y = 3;
Int add(x , x+y){return x;}

In this case, programming language supports lazy evaluation than the argument passed to the ADD function/method will not be evaluated as it is not used in the function.

How Lazy evaluation is supported by programming languages ?

There are two ways to implement the Lazy evaluation in programming languages
1: Language implicitly supports this feature than programmer is free from these concerns
2: Language provides some syntax or library to support the Lazy evaluation .

In C#4.0, second approach is used to support the lazy evaluation
.Net Framework 4.0 provides us with a new class called Lazy. As documentation states then Lazy provides support for several common patterns of lazy initialization, including the ability to initialize value types and to use null values. So it is construct that helps us implement lazy loading.

This is code from MSDN

using System.Linq;
public class Lazy {
private Func func;
private T result;
private bool hasValue;
public Lazy(Func func) {
this.func = func;
this.hasValue = false;
}
public T Value {
get {
if (!this.hasValue) {
this.result = this.func();
this.hasValue = true;
}
return this.result;
}
}
.
The class has three fields:
Func field: is a delegate that will be executed when someone will access the value,
hasValue field: is a logical value saying whether we already called the delegate
result field :stores the result after calling the delegate.
Value property:The logic of the class is in the Value property, which returns a value calculated by the delegate. The getter of the property first tests if we already have the result, and if not, it calls the delegate and stores the result. Otherwise we can just return the result that was calculated earlier(doesn’t it seem like use of Singleton pattern)

Hello World Example

Lazy lazy = new Lazy(() => {
Console.WriteLine("Running for first time only afterwards it caches the result for future invocations...");
Return “Hello World”;
});
Console.WriteLine("Starting...");
Console.WriteLine("output = {0}", lazy.Value);
Console.WriteLine("Output (again) = {0}", lazy.Value);

When we access lazy.value , it executes the func delegate for the first time and caches it result for future calls and in the second call , it returns the same result and hence it also increased performance because of caching . It can be used where we have to a complex calucation and you don’t know 100% that whether you will use it or not .

Happy Coding

Subscribe
Posted in Labels: kick it on DotNetKicks.com | 1 comments Links to this post

Free ASP.NET4.0 Hosting Available

Your Ad Here

Guys from ORCS Web are offering free ASP.NET 4.0 beta hosting until the end of October. Hosting plan is called “VS2010 and Web Deployment Tool Beta Program”. If you are interested in it then please feel free to sign up.

Beta hosting plan contains following features.

Windows Server 2008 / IIS 7,
Web Deployment for publishing (no FTP),
ASP.NET 4.0 (or 2.0/3.0/3.5 per request),
200MB web space,
100MB SQL Server 2008 space,
ORCS Web support.

Subscribe
Posted in Labels: kick it on DotNetKicks.com | 0 comments Links to this post

Access Twitter API using C#

Your Ad Here

There are various APIs or Wrapper libraries available on the following links

C# .NET Libraries for accessing twitter


static void Main(string[] args)
{

TwitterHelper objTH = new TwitterHelper();
objTH.DataFormat = "xml";
objTH.TwitterUrl = "http://twitter.com/statuses/friends";
objTH.TwitterUser= "UserNaME";
objTH.TwitterPass = "PASSWORD";
string xmlData = objTH.getTwitterMessage();
Console.WriteLine(xmlData);
Console.ReadLine();
}
}


Twitter Helper Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;


namespace TwitterNameSpace
{
class TwitterHelper
{
///
/// Please Set TwitterURL without the dataFormat extension
///

public string TwitterUrl { get; set; }
public string TwitterUser { get; set; }
public string TwitterPass { get; set; }
public string DataFormat { get; set; }
public string getTwitterMessage()
{
try
{
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.TwitterUrl+"."+this.DataFormat);
SetRequestParams(request);
request.Credentials = new NetworkCredential(this.TwitterUser, this.TwitterPass);
WebResponse response = request.GetResponse();
string content;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
content = reader.ReadToEnd();
}
}
return content;

}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return null;

}
private static void SetRequestParams(HttpWebRequest request)
{
request.Timeout = 500000;
request.Method = "GET";


}
}
}

Happy Coding

Please feel free to ask if you face any problem

Subscribe
Posted in Labels: kick it on DotNetKicks.com | 0 comments Links to this post