Friday, June 3, 2011

HttpWebRequest and HttpWebresponse using for loop

Program:-

using System;

using System.Net;

using System.Collections;
using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Http
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine("\n THE HEADERS THAT WERE RECEIVED IN THE RESPONSE:");
for (int i = 0; i < resp.Headers.Count; ++i)
Console.WriteLine("\n Header Name:{0},Value:{1}", resp.Headers.Keys[i], resp.Headers[i]);


resp.Close();
}
}
}


Output:-

IEnumerable Interface and HttpWebRequest/Response using foreach

Here in this program i have used both the concept of IEnumerable interface and foreach concept,each performing separate work.

1)Firstly,using IEnumerable Interface i have declared an array to define some properties of headers using GetEnumerator to create the object.Hereby using foreach to iterate it.It has been seen that to use foreach in such a context i had to define the IEnumerator/IEnumerable interface.

2)Secondly,i used foreach to illustrate the concept of HttpWebRequest and HttpWebResponse.

Program:

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

namespace sample
{
class Program
{
private string Name;
private string SetBy;
public Program( string name,string setby)
{
this.Name=name;
this.SetBy=setby;
}
public string NAME
{
get
{
return this.Name;
}
}
public override string ToString()
{
return "Name:" + this.Name.ToString() + "\nSetBy:" + SetBy;
}
}

class Http:IEnumerable,IEnumerator
{
ArrayList List=new ArrayList();
private int Position=-1;
public void AddContent(Program Prog)
{
List.Add(Prog);
}
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if(Position < List.Count-1)
{
++Position;
return true;
}
return false;
}
public void Reset()
{
Position=-1;
}
public object Current
{
get{
return List[Position];
}
}



static void Main(string[] args)
{
Http List = new Http();
Program p1 = new Program("Accept", "Set by the Accept property");
Program p2 = new Program("Content-Length", "Set by the conenction property");
List.AddContent(p1);
List.AddContent(p2);
foreach (Program prg in List)
{
Console.WriteLine(prg);
}
Console.WriteLine("\nEnumerator Program creating object of IEnumerator");
IEnumerator WebEnumerator = List.GetEnumerator();
WebEnumerator.Reset();
while (WebEnumerator.MoveNext())
{
Console.WriteLine((Program)WebEnumerator.Current);
}

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();


Console.WriteLine("\n THE HEADERS THAT WERE RECEIVED IN THE RESPONSE:");

foreach (var i in resp.Headers.AllKeys)
{
Console.WriteLine("\nHeader Name:{0},\tValue:{1}", i, resp.Headers[i]);

}

}
}
}

OUTPUT:-
1)


2)

Saturday, May 28, 2011

INTERFACE

INPUT:
PROGRAM 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
class Program : abc
{
static void Main(string[] args)
{
Console.WriteLine("Hello interfaces");
Program p = new Program();

p.xyz();
}
public void xyz()
{
Console.WriteLine("In xyz");
}
}

interface abc
{
void xyz();
}
}

OUTPUT:

Hello interfaces

In xyz



INPUT:

PROGRAM 2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
class ClassA : abc
{
static void Main(string[] args)
{
Console.WriteLine("Hello interfaces\n");
ClassA p = new ClassA();
ClassB q = new ClassB();
p.xyz();
q.xyz();
}
public void xyz()
{
Console.WriteLine("In ClassA:xyz\n");
}
}

interface abc
{
void xyz();
}
class ClassB : abc
{
public void xyz()
{
Console.WriteLine("In ClassB:xyz\n");
}
}
}

OUTPUT:

Hello Interfaces

In ClassA:xyz

InClassB:xyz

Friday, May 27, 2011

Disposable objects

The Main Objective Of Disposable Object:

It is an optional, standard way to provide "a method that releases allocated unmanaged resources".
So what does that actually mean? Well, if you're using unmanaged resources,and you keep those resources allocated throughout the lifetime of your object then you should implement IDisposable.Therefore using that the Developers who use this class can then call Dispose if they wish to free these resources early, before your object falls out of scope.
Before doing that,the user should define a class that inherits dispose() method,which is done as follows:
e.g. class MyDisposableClass:IDisposable

After this you need to define a method and inside it keep the resource(s) which you want to use regularly without having to call it everytime.

Important Points:

1)The pattern for disposing an object, referred to as a dispose pattern, imposes order on the lifetime of an object. The dispose pattern is used only for objects that access unmanaged resources. This is because the garbage collector is very efficient at reclaiming unused managed objects.

2)Using a Disposable object a Dispose method could be callable multiple times without throwing an exception.

3)Using Dispose() method we dont need to call Close() method explicitly.
Furthermore the main function of using block is to call the dispose() method.

Simple e.g showing the use of Disposable object:

using System;
using System.IO;

namespace Disposable
{
class Disp : IDisposable
{
public void Dispose()
{
Console.WriteLine("I am disposed \n");
}
}
public class MyDisposableClass
{


static void Main(string[] args)
{
using (Disp d = new Disp())
{
Console.WriteLine("Inside using block\n");


}
Console.WriteLine("Outside using block\n");
}
}
}

Its outputs:

Inside using block
I'm disposed
outside using block

Disposable object

DESCRIPTION:
First i made a disposable class using the the syntax -- " class Disp :IDisposable ".
Then i defined the method where i displayed the statement "Im disposed".
Then in the main class i used the 'using' keyword to display another satement "inside using block" inside it.It is seen that after i close the 'using' scope the compiler automatically calls the disposable class.Then finally i declared another statement "outside using block".Therefore whilst compiling the code,it shows the 'inside using block" statement first then calls the desposable class which in turn displays "im disposed" and then finally prints the "outside using block" statement.


INPUT:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
namespace Disposable
{
class Disp : IDisposable
{
public void Dispose()
{
Console.WriteLine("im disposed");
}
}
public class MyDisposableClass
{


static void Main(string[] args)
{
using (Disp d = new Disp())
{
Console.WriteLine("inside using block");


}
Console.WriteLine("outside using block");
}
}
}

OUTPUT:

On the console:

inside using block
Im disposed
Outside using block

Wednesday, May 25, 2011

Using Keyword.

What is using keyword?

1) Using as Directive in C# can be used for importing namespaces to your classes.

2)Another most useful advantage is it can be used in codeblocks what is does is it calls the Dispose() method automatically to release the resources and also automatically implements the try catch blocks for us.Here it is called a using statement.So the advantage is that we dont have to explicitly write a try-catch block and our code looks small.

3)using block,
If we declare any un-managed object in using block, u don't need to explicitly dispose those object.. once the block execution is over, the Dispose will be called automatically.

C# programming

C# (pronounced see sharp) is a multi-paradigm programming language encompassing imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within the .NET initiative.

Program 1: Read/Write TextFile
(Write TextFile)

Input:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WriteFile
{
class Program
{
static void Main(string[] args)
{
using (TextWriter tw = new StreamWriter("sample.txt"))
{
tw.WriteLine("HELLO WORLD");
}

}
}
}

Output:
A sample txt file was created in the executable folder of this project.The txt file consisted of the message that was mentioned in Console.WriteLine statement.

(Read TextFile)

Input:

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

namespace ReadFile
{
class Program
{
static void Main(string[] args)
{
using (TextReader tr = new StreamReader("sample.txt"))
{
Console.WriteLine(tr.ReadLine());
}

}
}
}

Ouput:

The txt file of the name sample.txt that was already made before is kept in the executable folder of this project.Whatever message was there in the txt file was displayed in the command prompt.