- Introduction to C# and Microsoft .NET FrameworkC# (pronounced "see sharp") is a multi-paradigm programming language encompassing imperative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within the .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270). C# is one of the programming languages designed for the Common Language Infrastructure.C# is intended to be a simple, modern, general-purpose, object-oriented programming language.[3] Its development team is led by Anders Hejlsberg, the designer of Borland's Turbo Pascal, who has said that its object-oriented syntax is based on C++ and other languages. James Gosling, who created the Java programming language in 1994, called it an 'imitation' of that language. The most recent version is C# 3.0, which was released in conjunction with the .NET Framework 3.5 in 2007. The next proposed version, 4.0, is in development.
The Microsoft .NET Framework is a software framework that can be installed on computers running Microsoft Windows operating systems. It includes a large library of coded solutions to common programming problems and a virtual machine that manages the execution of programs written specifically for the framework. The .NET Framework is a Microsoft offering and is intended to be used by most new applications created for the Windows platform. - Value TypesThe .NET framework includes a large number of built-in types that you can use directly or use to build your own custom types.The Value types directly contain their data offering excellent performance. However, value types that store very small pieces of data. In the .NET framework, all value types are 16 bytes or shorter. You can create user-defined types that store multiple values and methods.In object-oriented developement environements, a large portion of your application logic will be stored in user-defined types.We can also use Enumerations to improve code readability by providing symbols for a set of values.
- Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead
- General value types: Built-in types - User-defined - types Enumerations
- There are nearly 300 value types in the framework.
- Even though value types often represent simple values, they still function as objects
- All types are derived from System.Object
- Value types have an implicit constructor. The constructor assigns a default value (usually null or 0) to the new instance.
Default Values Table C# Type .NET Framework Type CLS-Compliant Description byte System.Byte Yes 8 bit unsigned integer sbyte System.SByte No 8 bit signed integer short System.Int16 Yes 16 bit signed integer ushort System.UInt16 No 16 bit unsigned integer int System.Int32 Yes 32 bit signed integer uint System.UInt32 No 32 bit unsigned integer long System.Int64 Yes 64 bit signed integer ulong System.UInt64 No 64 bit unsigned integer float System.Single Yes 32 bit; IEEE comform Floating-Point-Value double System.Double Yes 64 bit; IEEE comform Floating-Point-Value decimal System.Decimal Yes 128 bit Floating-Point-Value
(1 bit sign; 96 bit value; 8 bit exponent; 23 bit unused)
- We should explicitly initialize the variable within declaration
//C# code
bool b = false; - We can declare the variable as nullable. (Example: Yes/No question)
Nullablew<bool> b = null;
//shorthand notation, only for C#
bool? b= null;
//For this example, we can test if the user has answered to our Yes or No question
if(b.HasValue)
Console.WriteLine("b is {0}.",b.Value);
else
Console.WriteLine("b is not set.");
- User-Defined Types
Structures or simply structs In most other ways, structures behave nearly identical to classes.
Example:
- Enumerations
enum Titles : int{Mr,Mme, Dr};
Titles t = Titles.Dr;
The pupose of enumerations is to simplify coding and improve code readability.
struct Cycle
{
//Private fields
int _val,_min,_max;
//constructor
public Cycle(int min, int max)
{
_val = min;
_min = min;
_max = max;
}
public int value
{
get { return _val}
set{
if(value > _max || value < _min)
_val = _min;
}
}
public override string ToString()
{
return Value.ToString();
}
//operators
public static Cycle operator+(Cycle arg1, int arg2)
{
arg1.value += arg2;
return arg1;
}
}
We should define a structure rather than a class, if the type will perform better as a value type than a reference type
Logically represents a single value Has an instance size less than 16 bytes Will not be changed after creation Will not be cast to a reference type.
| Commentaires |
|
|
|
|
|
|
|
|
|
|
|
|
|
|


Twitter
Myspace
Facebook
Slashdot
Furl
Yahoo
Technorati
Googlize this
Blinklist
Wikio