Owner: c# Slackers URL:http://csharp-slackers.blogspot.com Join Date: Mon, 25 Aug 2008 05:33:47 -0500 Rating:0 Site Description: Chapter wise Information about C# topics from basics to Development level. Browse through Topics for more details. Site statistics:Click here
Type Conversion of Expressions in C# 2008-08-24 12:13:00 In addition to occurring within an assignment, type conversions also take place within an expression. In an expression, you can freely mix two or more different types of data as long as they are compatible with each other. For example, you can mix short and long within an expression because they are both numeric types. When different types of data are mixed within an expression, they are converted Read more:Conversion
, Expressions
C# Type Conversion and Casting 2008-08-24 12:11:00 In programming, it is common to assign one type of variable to another. For example, you might want to assign an int value to a float variable, as shown here: int i;float f;i = 10;f = i; // assign an int to a floatWhen compatible types are mixed in an assignment, the value of the right side is automatically converted to the type of the left side. Thus, in the preceding fragment, the value in i is Read more:Casting
, Conversion
The Scope and Lifetime of Variables in C sharp 2008-08-24 12:08:00 So far, all of the variables that we have been using were declared at the start of the Main( ) method. However, C# allows a local variable to be declared within any block. A block is begun with an opening curly brace and ended by a closing curly brace. A block defines a declaration space, or scope. Thus, each time you start a new block, you are creating a new scope. A scope determines what ob Read more:Lifetime
, Scope
, sharp
A Closer Look at Variables in C# 2008-08-24 12:06:00 Variables are declared using this form of statement:type var-name;where type is the data type of the variable and var-name is its name. You can declare a variable of any valid type, including the value types just described. When you create a variable, you are creating an instance of its type. Thus, the capabilities of a variable are determined by its type. For example, a variable of type bool cann
C# Literals 2008-08-24 11:58:00 In C#, literals refer to fixed values that are represented in their human-readable form. For example, the number 100 is a literal. Literals are also commonly called constants. For the most part, literals and their usage are so intuitive that they have been used in one form or another by all the preceding sample programs. Now the time has come to explain them formally.C# literals can be of any valu
C# Some Output Options 2008-08-24 11:54:00
Up to this point, when data has been output using a WriteLine( ) statement, it has been displayed using the default format provided by C#. However, C# defines a sophisticated formatting mechanism that gives you detailed control over how data is displayed. Although formatted I/O is covered in detail later in this book, it is useful to introduce some formatting options at this time. Using these opt Read more:Options
, Output
C# The bool Type 2008-08-24 11:50:00 The bool type represents true/false values. C# defines the values true and false using the reserved words true and false. Thus, a variable or expression of type bool will be one of these two values. Unlike some other computer languages, in C# there is no conversion defined between bool and integer values. For example, 1 does not convert to true, and 0 does not convert to false.Here is a program th
C# Characters 2008-08-24 11:46:00 In C#, characters are not 8-bit quantities like they are in many other computer languages, such as C++. Instead, C# uses a 16-bit character type called Unicode. Unicode defines a character set that is large enough to represent all of the characters found in all human languages. Although many languages, such as English, French, or German, use relatively small alphabets, some languages, such as Chin Read more:Characters
C# The Decimal Type 2008-08-24 09:44:00 Perhaps the most interesting C# numeric type is decimal, which is intended for use in monetary calculations. The decimal type utilizes 128 bits to represent values within the range 1E−28 to 7.9E+28. As you may know, normal floating-point arithmetic is subject to a variety of rounding errors when it is applied to decimal values. The decimal type eliminates these errors and can accurately represen
C# Floating-Point Types 2008-08-24 09:41:00 The floating-point types can represent numbers that have fractional components. There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. The type float is 32 bits wide and has an approximate range of 1.5E−45 to 3.4E+38. The double type is 64 bits wide and has an approximate range of 5E−324 to 1.7E+308.Of the two, double Read more:Floating
C# Integers 2008-08-24 09:38:00 C# defines nine integer types: char, byte, sbyte, short, ushort, int, uint, long, and ulong. However, the char type is primarily used for representing characters, and it is discussed later in this chapter. The remaining eight integer types are used for numeric calculations. Their bit-width and ranges are shown here:Type Width in Bits Range byte 8 0 to 255 sbyte 8 −128 to 127 short 16 −32,768 t
C# Value Types 2008-08-24 09:32:00 C# contains two general categories of built-in data types: value types and reference types. C#’s reference types are defined by classes, and a discussion of classes is deferred until later. However, at the core of C# are its 13 value types. These are built-in types that are defined by keywords in the C# language, and they are available for use by any C# program.Table 3-1: The C# Value
Types Type
The C# Class Library 2008-08-24 09:30:00 The sample programs shown in this chapter make use of two of C#’s built-in methods: WriteLine( ) and Write( ). As mentioned, these methods are members of the Console class, which is part of the System namespace, which is defined by the .NET Framework’s class library. As explained earlier in this chapter, the C# environment relies on the .NET Framework class library to provide support for such Read more:Library
C# Identifiers 2008-08-24 09:26:00 In C#, an identifier is a name assigned to a method, a variable, or any other user-defined item. Identifiers can be from one to several characters long. Variable names may start with any letter of the alphabet or an underscore. Next may be a letter, a digit, or an underscore. The underscore can be used to enhance the readability of a variable name, as in line_count. However, identifiers containing
The C# Keywords 2008-08-24 09:24:00 There are currently 77 keywords defined in the C# language . These keywords, combined with the syntax of the operators and separators, form the definition of the C# language. These keywords cannot be used as names for a variable, class, or method.The C# Keywords
abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit
Semicolons, Positioning, and Indentation in C# 2008-08-24 09:15:00 As mentioned earlier, the semicolon signals the end of a statement. That is, each individual statement must end with a semicolon. A block, however, is not terminated with a semicolon. Since a block is a group of statements, with a semicolon after each statement, it makes sense that a block is not terminated by a semicolon; instead, the end of the block is indicated by the closing brace.C# does not Read more:Positioning
Handling Syntax Errors in C# 2008-08-24 09:05:00 If you are new to programming, it is important to learn how to interpret and respond to errors that may occur when you try to compile a program. Most compilation errors are caused by typing mistakes. As all programmers soon find out, it is quite easy to accidentally type something incorrectly. Fortunately, if you type something wrong, the compiler will report a syntax error message when it tries t Read more:Errors
, Handling
, Syntax
Using the Visual Studio IDE 2008-08-24 08:46:00 1. To edit, compile, and run a C# program using Visual
Studio 2005, follow these steps.
Create a new, empty C# project by selecting File
New Project. Next, select Visual C#, and then Console Application, as shown here:
Then, change the name to Project1 and press OK.
Note: The precise screens that you see may vary based on which version of Visual Studio
2005 you have installed and your speci
Using csc.exe, the C# Command-Line Compiler 2008-08-24 08:42:00 Although the Visual Studio IDE is what you will probably be using for your commercial projects, the C# command-line compiler is the easiest way to compile and run most of the sample programs shown in this book. To create and run programs using the C# commandline compiler, you will follow these three steps:
Enter the program using a text editor.
Compile the program.
Run the program.
Entering the
A First Simple Program 2008-08-24 08:37:00 It is now time to look at an actual C# program. We will begin by compiling and running the short program shown here:
/*
This is a simple C# program.
Call this program Example.cs.
*/
using System;
class Example {
// A C# program begins with a call to Main().
public static void Main() {
Console.WriteLine("A simple C# program.");
}
}
The primary development environment for C# is Microsof Read more:First
, Program
, Simple
Inheritance 2008-08-24 08:34:00 Inheritance is the process by which one object can acquire the properties of another object. This is important because it supports the concept of hierarchical classification. If you think about it, most knowledge is made manageable by hierarchical (that is, top-down) classifications. For example, a Red Delicious apple is part of the classification apple, which in turn is part of the fruit class, w Read more:Inheritance
Polymorphism 2008-08-24 08:31:00 Polymorphism (from the Greek, meaning “many forms”) is the quality that allows one interface to access a general class of actions. A simple example of polymorphism is found in the steering wheel of an automobile. The steering wheel (the interface) is the same no matter what type of actual steering mechanism is used. That is, the steering wheel works the same whether your car has manual steerin
Encapsulation 2008-08-24 08:20:00 Encapsulation is a programming mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse. In an object-oriented language, code and data can be bound together in such a way that a self-contained black box is created. Within the box are all necessary data and code. When code and data are linked together in this fashion, an object is
C# Object-Oriented Programming 2008-08-24 08:15:00 At the center of C# is object-oriented programming (OOP). The object-oriented methodology is inseparable from C#, and all C# programs are to at least some extent object oriented. Because of its importance to C#, it is useful to understand OOP’s basic principles before you write even a simple C# program.
OOP is a powerful way to approach the job of programming. Programming
methodologies have ch Read more:Object
, Oriented
Connecting to a Password-Protected Access Database in ADO 2008-09-16 23:41:00 Use the Jet OLEDB:DatabasePassword
attribute in the connection string to specify the password.The sample code contains a single event handler:Connect Button.ClickCreates and opens a connection to a password-secured Microsoft Access
database using the OLE DB .NET data provider. Information about the database is displayed from the properties of the OleDbConnection object.File: AccessPasswordForm.cs Read more:Connecting
Connecting to a Microsoft Excel Workbook in ADO 2008-09-16 23:32:00 Use the OLE DB Jet provider to create, access, and modify data stored in an Excel
workbook.The sample code contains two event handlers:Form.LoadCreates an OleDbDataAdapter that uses the Jet OLE DB provider to access an Excel workbook. Custom insert and update logic is created for the DataAdapter. A DataTable is filled from the first worksheet, Sheet1, in the Excel workbook and the default view of Read more:Connecting
, Microsoft
, Workbook
Ensure That Only One Instance of an Application Can Execute Concurrently 2008-09-16 14:31:00 The Mutex provides a mechanism for synchronizing the execution of threads across process boundaries and in addition provides a convenient mechanism through which to ensure that only a single instance of an application is running concurrently. By trying to acquire ownership of a named Mutex at startup and exiting if the Mutex can't be acquired, you can ensure that only one instance of your ap Read more:Application
Start a New Process 2008-09-16 14:27:00 The Process
class provides a managed representation of an operating system process and provides a simple mechanism through which you can execute both managed and unmanaged applications. The Process class implements four overloads of the Start
method, which you use to start a new process. Two of these overloads are static methods that allow you to specify only the name and arguments for the new
Terminate a Process 2008-09-16 14:25:00 If you start a new process from managed code using the Process
class , you can terminate the process using the Process object that represents the new process. You can also obtain Process objects that refer to other currently running processes using the static methods of the Process class summarized Here
Methods for Obtaining Process References
Method Description
GetCurrentProc
Create a Thread-Safe Collection Instance 2008-09-16 14:25:00 By default, the standard collection classes from the System.Collections and System.Collections.Specialized namespaces will support multiple threads reading the collection's content concurrently. However, if one or more of these threads tries to modify the collection, you will almost certainly encounter problems. This is because the operating system can interrupt the actions of the thread while Read more:Thread