Keywords in C#.net

C#.net have 77 Reserve word. You are not declare variable/method in reserve words .if you want to declare reserved word you can use " @ " symbol.

Example:

private void btn_Click(object sender, EventArgs e)
{
string string="Welocme to DevEnvExe"
}
Suppose if you want use above coding
private void btn_Click(object sender, EventArgs e)
{
string @string="Welocme to DevEnvExe"
}
Below list are C#.net reserve words
unchecked
unsafe
ushort
boolirtual
class
abstract
as
base
byte
char
decimal
int
sbyte
uint
ulong
break
case
catch
finally
checked
const
continue
default
delegate
do
double
else
enum
event
explicit
extern
false
fixed
float
for
foreach
goto
if
implicit
in
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sealed
short
sizeof
stackalloc
static
string
struct
switch
while
this
throw
true
try
typeof
using
void
volatile






What is Web.Config File?

   It is an optional XML File which stores configuration details for a specific asp.net web application.

What is Machine.config File?

The Machine.Config file, which specifies the settings that are global to a particular machine. This file is located at the following path:

\WINNT\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config

As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.

You can override settings in the Machine.Config file for all the applications in a particular Web site by placing a Web.Config file in the root directory of the Web site as follows:

\InetPub\wwwroot\Web.Config

Macine Config VS WebConfiguration

Difference between Machine.Config and Web.Config

Machine.Config:

i) This is automatically installed when you install Visual Studio. Net.
ii) This is also called machine level configuration file.
iii)Only one machine.config file exists on a server.
iv) This file is at the highest level in the configuration hierarchy.

Web.Config:

i) This is automatically created when you create an ASP.Net web application project.

ii) This is also called application level configuration file.

iii)This file inherits setting from the machine.config

2D Array

You want to use a 2D array containing any type(int,string ,etc) of value in your C# program

2D array contain 2 pair of values
Performance PERF
2D Array are slower to index elements than 1D arrays. They are sometimes more memory
2D Array String
String[,] os=new string[,]{
{"dev","Svalue"},
{"devenvexe","JValue"}

};
2D Array Int:
int[,] os=new int[,]{ {1,2}, {3,4} };
Get the Upperbound to Loop:
string[,] ad = new string[,]
{
{"dev", "devenvexe"},
{"TCS", "Tata"},
{"CTS", "Cogn"},
};

// Get the upper bound to loop.
//Upper bound means specify the dimention (0 or 1)
for (int i = 0; i <= ad.GetUpperBound(0); i++)
{
string FDim = ad[i, 0]; // JS, TCS, CTS...
string SDim = ad[i, 1]; // J Suqare, Tata, Cogn...
MessageBox.Show(FDim);
MessageBox.Show(SDim);
}
If you want use Length keyword .it will return 6 so
string[,] ad = new string[,]
{
{"dev", "devenvexe"},
{"TCS", "Tata"},
{"CTS", "Cogn"},
};

// Get the upper bound to loop.
//Upper bound means specify the dimention (0 or 1)
for (int i = 0; i <= ad.Length/2; i++)
{
string FDim = ad[i, 0]; // JS, TCS, CTS...
string SDim = ad[i, 1]; // J Suqare, Tata, Cogn...
MessageBox.Show(FDim);
MessageBox.Show(SDim);
}
GetUpperBound VS Length:
Its faster to use Array Length

Looping Speed:

GetUpperBound: 142 ms
Length/2 : 47 ms
Arrays are references that point to fixed sizes of memory that you can store different kinds of elements in, such as values or references to other objects. In these examples, we look at simple examples of arrays.

Arrays is fixed number of memory allocate in system

It is initialize and with accessing with Square brackets []

Two dimensional array use comma with in bracket

How to create Array:

Int[] value=new int[Length]

// Int means data type suppose if you want string,bool,etc
// value is data type
// length means ,you can enter int value

Ex:

String val=new String[3];

Ex Program:

Using system;
Class Progra
{
//Public void button Click
Sting val=new String[3];
Val[0]="devenvexe";
Val[1]="Suthahar";
Val[2]="Suresh";
Foreach(string n in Val)
{
MesageBox.Show(n.Tostring);
}
}

How to use C# string Format

string Format method replace the argument Object into a text equivalent System.String.

Replace string Particular index for ex System.String.Format("Hi welcome Mr {0}","Sutahahr")

System.String.format(string format,Object arg0)

Parameters:

String format : The format String

The format String Syntax is like {indexNumber:formatCharacter}

Object arg0 : The object to be formatted.

Returns:

String : The formatted String

Examples :

Currency :

String.Format("{0:c}", 10) will return $10.00
The currency symbol ($) displayed depends on the global locale settings.

Date :

String.Format("Today's date is {0:D}", DateTime.Now)

You will get Today's date like : 01 January 2005

Time :

String.Format("The current time is {0:T}", DateTime.Now)

You will get Current Time Like : 10:10:12
--
J.SuThahar MCA

MicroSoft Technology Specialist

Microsoft Certified ProfessionalDeveloper

www.jsuthahar.tk

09943151415
When the program is executed the CLR activates JIT compiler ,inturn this JIT converts the MSIL Code (Non Executable) to Native Code(Executable) on demand basis as each part of Program is needed

Types Of JIT Compilers are as stated below :

(1) PRE JIT

(2) ECONO JIT

(3) NORMALE JIT

(1) PRE JIT : It Compiles complete source code to native caode In a single Compilation.

(2) ECONO JIT : It compiles only those methods that are called at Runtime.

(3) NORMALE JIT : It compiles only those methods that are called at Runtime and are stored in cache.When these methods called again they retrieve from cache.

Default JIt is PRE Jit .

What is Resource File?


A resource file is a XML file that contains the strings that we want to
a. Translate into different languages.
b. Can be updated dynamically so that user themselves can modify values in resource files once the application is deployed on the server without re-compiling the entire application itself.
The resource file contains key / value pairs.
Each pair is an individual resource.
Key names are not case sensitive.

Types of Resources:

There are two types of resources
  • Local Resources
  • Global Resources

Local Resources:

Local resource is specific to a single Web page and used for providing versions of a Web page in different languages.
Local resources must be stored in App_LocalResources sub folder.
Local resources must be named in format <WebPageName> [.language / language and culture].resx.
Ex: Default.aspx.resx- Base resource file. This is the default, or fallback, resource file.
Default.aspx.de.resx- A resource file for German etc.
Generating Default Resource File using VS2005:
In Visual Studio designer, click the designer surface or a control.
Select Tools --> Generate Local Resource
An XML - based local resource file for the Web page in the App_LocalResources folder with Text and ToolTip values for all existing controls on the page, as well as the page title will be generated.

Generating Resource Files for Other Cultures:

In Solution Explorer

Right click the 'Default.aspx.resx' file and click the Copy

Right click the App_LocalResources folder and click the Paste

Right click the 'Copy of Default.aspx.resx' and click the Rename

Type the new name for the resource file that includes the new language and culture code before the extension.

Ex: 'Default.aspx.fr.resx' to create a French language version of 'Default.aspx' page.

Double click the resource file to open it in Visual Studio. Update the values of each resource for the new culture, and save the file

Testing Resource Files for Other Culture

ASP.NET automatically determines the user's preferred culture based on information provided by the Web Browser. To test other culture we need to update the preferred language in Web Browser as

In Internet Explorer, select Tools --> Internet Options

Under General tab, select Languages

In Language Preference dialog box click on Add button, Select the language from Add Language dialog box and click on Ok button

In Language Preference dialog box, under Language list make newly added language on top using Move Up , and then click on Ok

Now Webpage will be displayed using selected language resource.

Global Resources:

Global resource can be read from any page or code that is in the application.

Global resource must be stored in App_GlobalResources at the root of the application.

Generating Global Resource File using VS2005:

In Solution Explorer, right-click the root of your Web Site or Project in Web Application, click add ASP.NET folder and the click App_GlobalResources folder

Right click the App_GlobalResources folder, then click on add new item.

Under visual studio installed template, click on Resource file. Give any name with an extension .resx [e.g.MyResource.resx].

Copy and paste the resource file to create the resource file for creating different cultures. For each culture, add the culture identifier immediately before the .resx extension [e.g.- MyResource.fr.resx- for French culture]

Working with Resources in Web Pages:

Resource files are used in ASP.NET

To fill the property values of controls on the page.

To update the resource values dynamically i.e. after publishing the web site, to change the Page Titles, Error Messages, and Labels Text etc.

To use resource to set control's property values, we can use

Implicit localization

If local resource files are created in Application, then implicit localization can be used to fill the property values for the control.

To use implicit localization, following naming convention must be used for resources in local resource file

key.property

Where key: Any name for the resource

Property: Property of the control that we are localizing

Ex: If we are creating resource for control label named lblErrorMsg, you need to create following key/value pairs in local resource file

lblErrorMsg.Text = "Error Message"
lblErrorMsg.ForeColor= "Red"

In aspx page, we need to use a special meta attribute in the markup for the control to specify implicit localization as-

<asp:Label ID="lblErrorMsg" runat="server" meta:resourcekey=" lblErrorMsg" Text="Label"></asp:Label>

No need to explicitly specify which properties are localized.
The resourcekey value matches a key in resource file. 

At run time, ASP.NET matches resources to control properties using the resourcekey, if property value is defined in resource file, ASP.NET substitute the resource value for the property.

Explicit localization

We use resource expression for each property of a control

For ex: A label control that is configured to set a text and fore color property from global resource look like-
<asp:Label ID="lblErrorMsg" runat="server" Text="<%$ Resources:ResourceFileName, lblErrorsgText %>"></asp:Label>

The resource expression takes the following form

<%$ Resources:Class, ResourceID %>

Where Class: Identifies the resource file to be used
ResourceID: Identifier of the resource to be read from resource file
Localizing Static Text

ASP.NET Localize control is used to localize the static text on the page.

The Localize control renders no markup, it just function to act as a place holder for static text

At run time ASP.NET treat Localize control as a Literal control

<asp:Localize ID="WelcomeMessage" runat="server" meta:resourcekey="literalResource" Text="WelcomeMessage" />

Retrieve Resource Values Programmatically

Call the GetLocalResourceObject() or GetGlobalResourceObject() method to read specific resource from local or global resource file, respectively.

Ex: string localresourcestring = string.Empty;

string globalresourcestring = string.Empty;

// Get the local resource string.
try
{
localresourcestring = (String)GetLocalResourceObject ("LocalResourceString1");
}
catch
{
localresourcestring = "Could not find local resource.";
}
// Get the global resource string.
try
{
globalresourcestring = (String)GetGlobalResourceObject("MyResource", "GlobalResourceString1");
}
catch
{
globalresourcestring = "Could not find global resource.";

}
Editing Resource Files after publishing in ASP.NET 2.0

The beauty of the resource file in ASP.NET 2.0 is modifying the resource file after the application is deployed on a server without re-compiling the entire application itself.

But the ability to do so depends on what project model we have followed for Web application in VS 2005

Project Model- Website Project Model (Default in VS2005)

Resource file only under the App_LocalResources will get published as a raw .resx files on the server as these are not compiled. These resources files can be edited on the server as they are compiled during runtime.

Files under the App_GlobalResources folders are compiled into individual resource specific dlls and published on the server, so we cannot edit resource under this folder

Project Model- Web Application Project(WAP)

Files both under the App_LocalResources or App_GlobalResources folders will get published as raw .resx files which are editable.

So with this project model, we are able to edit the resource files after publishing the web application.









If data are not available in some kind of XML format (like RSS or web services), sometime you need to deal with HTML output of web page. HTML is usually much harder to analyze than XML because you need to write your own parser which is different for every web site. But, the first step, reading of HTML output of web page is pretty simple. To get HTML of web page you need only few lines of code.

To start, place two TextBox controls named txtURL and txtPageHTML, and one button control on web form, like in image bellow:
Image 1: Web form for getting page HTML at design time
Now, on button's click event function, place this code:

[ C# ]
// We need these namespaces

using System;
using System.Text;
using System.Net;

public partial class DefaultCS : System.Web.UI.Page
{
protected void btnGetHTML_Click(object sender, EventArgs e)
{
// We'll use WebClient class for reading HTML of web page
WebClient MyWebClient = new WebClient();
// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (txtURL.Text != "")
{
PageHTMLBytes = MyWebClient.DownloadData(txtURL.Text);
// Convert result from byte array to string
// and display it in TextBox txtPageHTML
UTF8Encoding oUTF8 = new UTF8Encoding();
txtPageHTML.Text = oUTF8.GetString(PageHTMLBytes);
}
}
}
[ VB.NET ]
' We need these namespaces

Imports System
Imports System.Text
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnGetHTML_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnGetHTML.Click

' We'll use WebClient class for reading HTML of web page

Dim MyWebClient As WebClient = New WebClient()

' Read web page HTML to byte array

Dim PageHTMLBytes() As Byte
If txtURL.Text <> "" Then
PageHTMLBytes = MyWebClient.DownloadData(txtURL.Text)

' Convert result from byte array to string

' and display it in TextBox txtPageHTML

Dim oUTF8 As UTF8Encoding = New UTF8Encoding()
txtPageHTML.Text = oUTF8.GetString(PageHTMLBytes)
End If
End Sub
End Class

Now you can start sample project, type some valid URL in first TextBox control and click to "btnGetHTML" button. Code listed above will return HTML code of requested URL and display it in second text box, like in image bellow:


Image 2: HTML Code is read and shown in text box

As you see, loading of HTML code of web page is relatively easy. Analyzing of this data is much harder and depends of page structure.

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts