A built-in data type, such as an int or char, or
A user-defined data type, such as a class or interface.
Data types can also be defined as being either:
Structs
public struct Book
{
public decimal price;
public string title;
public string author;
}
Enumerations
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
Numeric types
Integral types
Type Range Size
sbyte
-128 to 127
Signed 8-bit integer
byte
0 to 255
Unsigned 8-bit integer
char
U+0000 to U+ffff
Unicode 16-bit character
short
-32,768 to 32,767
Signed 16-bit integer
ushort
0 to 65,535
Unsigned 16-bit integer
int
-2,147,483,648 to 2,147,483,647
Signed 32-bit integer
uint
0 to 4,294,967,295
Unsigned 32-bit integer
long
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Signed 64-bit integer
ulong
0 to 18,446,744,073,709,551,615
Unsigned 64-bit integer
Floating-point types
Type Approximate range Precision
float
±1.5e−45 to ±3.4e38
7 digits
double
±5.0e−324 to ±1.7e308
15-16 digits
decimal
ype Approximate Range Precision .NET Framework type
decimal
±1.0 × 10−28 to ±7.9 × 1028
28-29 significant digits
System.Decimal
bool
// keyword_bool.cs
using System;
public class MyClass
{
static void Main()
{
bool i = true;
char c = '0';
Console.WriteLine(i);
i = false;
Console.WriteLine(i);
bool Alphabetic = (c > 64 && c < 123);
Console.WriteLine(Alphabetic);
}
}
User defined structs.
Reference Types (C# Reference)
class
class TestClass
{
// Methods, properties, fields, events, delegates
// and nested classes go here.
}
interface
interface ISampleInterface
{
void SampleMethod();
}
class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}
static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();
// Call the member.
obj.SampleMethod();
}
}
delegate
using System;
// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);
class MainClass
{
// Regular method that matches signature:
static void SampleDelegateMethod(string message)
{
Console.WriteLine(message);
}
static void Main()
{
// Instantiate delegate with named method:
SampleDelegate d1 = SampleDelegateMethod;
// Instantiate delegate with anonymous method:
SampleDelegate d2 = delegate(string message)
{
Console.WriteLine(message);
};
// Invoke delegate d1:
d1("Hello");
// Invoke delegate d2:
d2(" World");
}
}
This section also introduces the following built-in reference types:
object
// keyword_object.cs
using System;
class SampleClass
{
public int i = 10;
}
class MainClass
{
static void Main()
{
object a;
a = 1; // an example of boxing
Console.WriteLine(a);
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
a = new SampleClass();
SampleClass classRef;
classRef = (SampleClass)a;
Console.WriteLine(classRef.i);
}
}
string
string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
No comments:
Post a Comment