Difference Between Shadowing and Override in C#
Posted On Friday, June 12, 2009 at at 2:15 PM by test
Difference between Shadowing and Override in C#
Criterion | Shadowing | Overriding |
---|---|---|
Purpose | Protecting against a subsequent base class modification introducing a member you have already defined in your derived class | Achieving |
Redefined element | Any declared element type | Only a procedure (Function or Sub) or property |
Redefining element | Any declared element type | Only a procedure or property with the identical calling sequence1 |
Accessibility | Any accessibility | Cannot expand the accessibility of overridden element (for example cannot override Protected with Public) |
Readability and writability | Any combination | Cannot change readability or writability of overridden property |
Keyword usage | Shadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specified | Overridable required in base class; Overrides required in derived class |
Inheritance of redefining element by classes deriving from your derived class | Shadowing element inherited by further derived classes; shadowed element still hidden2 | Overriding element inherited by further derived classes; overridden element still overridden |
Lazy Evaluation Feature in C# 4.0(VS2010)
Posted On Sunday, June 7, 2009 at at 2:28 PM by test
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
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
This is code from MSDN
using System.Linq;
public class Lazy
private Func
private T result;
private bool hasValue;
public Lazy(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
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
Free ASP.NET4.0 Hosting Available
Posted On Saturday, June 6, 2009 at at 1:37 PM by test
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.
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.
Access Twitter API using C#
Posted On Tuesday, June 2, 2009 at at 12:17 PM by test
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
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