Unit 2 C#
Unit 2 C#
C# Object
In other words, object is an entity that has state and behavior. Here, state means
data and behavior means functionality.
Object is a runtime entity, it is created at runtime.
Object is an instance of a class. All the members of the class can be accessed
through object.
Let's see an example to create object using new keyword.
Student s1 = new Student();//creating an object of Student
In this example, Student is the type and s1 is the reference variable that refers to
the instance of Student class. The new keyword allocates memory at runtime.
C# Class
class is a group of similar objects. It is a template from which objects are created.
It can have fields, methods, constructors etc.
Let's see an example of C# class that has two fields only.
namespace First
{
public class Hello
{
public void sayHello() { Console.WriteLine("Hello Namespace"); }
}
}
namespace Second
{
public class Welcome
{
public void sayWelcome() { Console.WriteLine("Welcome Namespace"); }
}
}
public class Test Namespace
{
public static void Main()
{
Hello h1 = new Hello();
Welcome w1 = new Welcome();
h1.sayHello();
w1.sayWelcome();
}
}
TYPES
Private Assemblies:
Shared Assemblies:
They are used to store resources and code that are shared by various
applications.
Shared Assemblies are created using the strong name tool (sn.exe).
Satellite Assemblies:
Localized resources like strings, pictures, and audio files are kept in satellite
assemblies.
The resgen.exe tool is used to create a resource file (.resx) from a text file.
A Dynamic Link Library (DLL) is a type of assembly that contains code that can
be used by multiple applications.
DLLs are created with the intention of being used by numerous
applications. DLLs, however, differ from shared assemblies in that they do not
have a distinctive name.
DLLs are kept in the directory of the application or a subdirectory of the directory
of the application.
They can be used by multiple applications by simply copying the DLL to the
application's directory.
DLLs are created using the same tools that are used to create shared assemblies.
However, DLLs do not require a strong name. They can be signed with a digital
signature, but this is optional.
Web Server
Web server is a computer where the web content is stored.
web server is used to host the web sites but there exists other web servers also such as
gaming, storage, FTP, email etc.
Web site is collection of web pages whileweb server is a software that respond to the request
for web resources.
Architecture
Web Server Architecture follows the following two approaches:
1. Concurrent Approach
Concurrent approach allows the web server to handle multiple client requests at the same
time. It can be achieved by following methods:
Multi-processing
In this a single process (parent process) initiates several single-threaded child processes
and distribute incoming requests to these child processes. Each of the child processes are
responsible for handling single request.
It is the responsibility of parent process to monitor the load and decide if processes
should be killed or forked.
Multi-threaded
it creates multiple single-threaded process.
Hybrid
It is combination of above two approaches.
ASP.NET provides web forms controls that are used to create HTML components.
ASP.NET CheckBox
It is used to get multiple inputs from the user. It allows user to select choices
from the set of choices.
It takes user input in yes or no format. It is useful when we want multiple choices
from the user.
This is a server side control and ASP.NET provides own tag to create it. The
example is given below.
Server renders it as the HTML control and produces the following code to the
browser.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Select Courses</h2>
<asp:CheckBox ID="CheckBox1" runat="server" Text="J2SE" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="Spring" />
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /
>
</p>
</form>
<p>
Courses Selected: <asp:Label runat="server" ID="ShowCourses"></asp:Label>
</p>
</body>
</html>
ASP.NET ListBox
The ListBox Control provides us a user interface that will display the List of the
items.
The list box is like a set of radio buttons except that the list box is scrollable
and can contain more items than a set of Radio Buttons.
The number of entries added to the list box can also be at the run time.
The list box is a web server control.
It allows the selection of single and multiple items in the list
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> An example of ASP.Net ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Choose a color:</h2>
<asp: ListBox ID ='ListBox1’ runat = 'server'>
<asp: ListItem> Dark Grey </asp: ListItem>
<asp: ListItem> Red </asp: ListItem>
<asp: ListItem> Green </asp: ListItem>
<asp: ListItem> Blue </asp:ListItem>
<asp: ListItem> Yellow </asp: ListItem>
<asp: ListItem> Black </asp: ListItem>
</asp: ListBox>
</div>
</form>
</body>
</html>
Rich Control
1.FileUpload control
FileUpload control is used to browse and upload files. After the file is
uploaded, you can store the file on any drive or database. FileUpload
control is the combination of a browse button and a text box for entering
the filename.
2.Calendar control
Calendar control provides you lots of property and events. By using these
properties and events you can perform the following task with calendar
control.
Select date.
Selecting a day, a week or a month.
Customize the calendar's appearance.
3.AdRotator control
AdRotator control is used to display different advertisements randomly in
a page.
The list of advertisements is stored in either an XML file or in a database
table. Lots of websites uses AdRotator control to display the
advertisements on the web page.
4.MultiView control
MultiView control can be used when you want to create a tabbed page.
In many situations, a web form may be very long, and then you can
divide a long form into multiple sub forms.
MultiView control is made up of multiple view controls. You can put
multiple ASP.NET controls inside view controls.
Common Properties ASP .Net
Bin Directory
ASP.NET projects used to compile all the source files into an assembly and put them into
a folder called "Bin"
it used to compile all files into a DLL and copy them to the special folder called "Bin".
The bin folder holds binary files, which are the actual executable code for your
application or library.
Virtual Directory
A virtual directory represents a web application and it points to a physical folder in
your computer.
A web application is accessed using a virtual directory name instead of a physical
folder name
A virtual directory is a user-defined directory name that is configured in Internet Information
Services (IIS) and linked to a physical directory on the local server's storage
How to create a virtual directory by using IIS Manager
In IIS Manager, expand the local computer and the Web site to which you want to add a virtual
directory.
Right-click the site or folder in which you want to create the virtual directory, click New, and
then click Virtual Directory.
In the Add Virtual Directory dialog box, at a minimum enter information in the Alias and
Physical path and then click OK.