Using the keywords ref and out. The difference between ref and out is as follows:
With ref , initialization parameter is the responsibility of the calling code.
The method is not obliged to modify the parameter value.
With out the initialization of the variable is the responsibility of the method, which must assign a value before terminating.
The calling code does not have to initialize it.
Here is an example to clarify all this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class TestRef
{
public void TestRef ( ref int i)
{
if (i < 0 )
i = 0 ;
}
public void TestOut ( out int j)
{
j = 5 ;
}
public static void Main()
{
// We initialize the integer a
int a = -3;
// It is passed as a parameter to the function by reference
TestRef(ref a);
Console.WriteLine("a vaut maintenant 1");
// Utilisation de out
int b;
// We pass a parameter b without having initialized
TestOut(b);
Console.WriteLine("b vaut maintenant 5");
}
} |
2012-04-30 by liuyunsx | Categories
: C# | Tags: c#, dotnet, method, pass | No Comments
A value type, by definition, can not be null : it necessarily a value.
However, there is a generic type Nullable which allows to include a value type, and can assert null :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// integer not nullable
int monInt1 = 42 ;
// integer nullable
nullable < int > monInt2 = null ;
// integer nullable, scoring short
int? monInt3 = null ;
// Conversion implicit in int to nullable <int>
monInt2 = monInt1 ;
// Copy of an int to a Nullable <int>
if (monInt2.HasValue)
{
// No implicit conversion from int to nullable <int>, use the Value property
monInt1 = monInt2.Value;
} |
2011-12-05 by liuyunsx | Categories
: C# | Tags: c#, dotnet, null, type | 1 Comment
The difference between two dates is done using the operator – which is redefined in the DateTime type to return an object of type TimeSpan (time interval).
calculate the number of days since the creation of this issue :
1
2
3
4
5
| DateTime DateCourante = DateTime.Now;
DateTime DateCreationquestion = new DateTime(2007, 1, 3);
TimeSpan Ts = DateCourante - DateCreationquestion;
Console.WriteLine("It took {0} day (s) since the inception of this question!", Ts.Days); |
Note that various arithmetic operators are redefined in the DateTime and TimeSpan structures, so that it can perform various operations on dates.
DateTime – DateTime = TimeSpan
DateTime + TimeSpan = DateTime
DateTime – TimeSpan = DateTime
TimeSpan + TimeSpan = TimeSpan
DateTime + DateTime = impossible because it does not make sense
2011-12-05 by liuyunsx | Categories
: C# | Tags: c#, date, dotnet, TimeSpan | No Comments
The keyword default provides the default of a type. For a reference type, the default value is always null .
For a value type, default value is an instance of the type where all fields have their default value (all bytes to 0).
1
2
| Console.WriteLine("Default value of int : {0}", default(int) != null ? default(int).ToString() : "NULL");
Console.WriteLine("Default string : {0}", default(string) != null ? default(string).ToString() : "NULL"); |
This keyword is especially useful when writing a generic class or method.
2011-12-04 by liuyunsx | Categories
: C# | Tags: c#, dotnet, type | No Comments
You can perform a shift left or right of a number of bits using the operators “and”:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| int value = 8 ;
int res ;
Console . WriteLine ( " The value of start is {0} " , value) ;
// Shift Left
// res is multiplied by 2
res = value << 1;
Console.WriteLine("After a lag of one to the left, is res {0}", res);
// Shift Right
// res is divided by 2
res = value >> 1;
Console.WriteLine("After a lag of one to the right, is res {0}", res);
// Shift Left
// res is multiplied by 4
res = value << 2;
Console.WriteLine("After a lag of two to the left, is res {0}", res); |
2011-12-04 by liuyunsx | Categories
: C# | Tags: c#, dotnet, number | No Comments
To use an array, C #, you must follow the type of the array elements by [].
To access the array elements, you must navigate by using the index (attention, the first index of array elements starts at 0 and not 1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| public class Tableau
{
// An array of type string
private string[] _TableauString;
public static void Main()
{
// We define the array size
_TableauString = new string[3];
// Fill the table
for (int i = 0; i < 3; i++)
{
_TableauString[i] = "Chaine " + i;
}
// Viewing the contents of the table
for (int i = 0; i < 3; i++)
{
Console.Write("Case " + i);
Console.WriteLine("Contenu: " + _TableauString[i]);
}
}
} |
2011-11-12 by liuyunsx | Categories
: C# | Tags: arrays, c#, dotnet | No Comments
There are two solutions to determine if an object is of a type:
The first is to use the keyword is. It returns true if the object is of the type requested:
1
2
3
4
5
| String monString = "This is a test!";
if (monString is String)
Console.WriteLine("monString is String!");
else
Console.WriteLine("monString is not a String!"); |
Read more »
2011-10-19 by liuyunsx | Categories
: C# | Tags: . NET Framework, c#, dotnet, verify object, verify type | No Comments
While this is not recommended, it is possible to use a reserved keyword by prefixing the “@” character.
1
2
3
4
| void @while()
{
int @class = 2007;
} |
2011-10-19 by liuyunsx | Categories
: C# | Tags: . NET Framework, dotnet, keyword | No Comments