How to pass a parameter by reference to a method?



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:

?View Code CSHARP
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");
        }
    }

How to set a null value to a value type?



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 :

?View Code CSHARP
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;
	}

Please indicate the source reference, the original address:
http://www.mydeveloperblog.com/dotnet/c-sharp/how-to-set-a-null-value-to-a-value-type/

How to calculate the time interval between two dates?



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 :

?View Code CSHARP
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

How to retrieve the default of a kind?



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).

?Download download.txt
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.

Please indicate the source reference, the original address:
http://www.mydeveloperblog.com/dotnet/c-sharp/how-to-retrieve-the-default-of-a-kind/

How to perform a bit shift on a number?



You can perform a shift left or right of a number of bits using the operators “and”:

?View Code CSHARP
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);

Please indicate the source reference, the original address:
http://www.mydeveloperblog.com/dotnet/c-sharp/how-to-perform-a-bit-shift-on-a-number/

How to declare and use the one-dimensional arrays?



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)

?View Code CSHARP
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]);
            }
        }
    }

How to verify that an object is of a certain type?



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:

?View Code CSHARP
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 »

How to use a reserved keyword as a variable name or function?



While this is not recommended, it is possible to use a reserved keyword by prefixing the “@” character.

?View Code CSHARP
1
2
3
4
void @while()
      {
          int @class = 2007;
      }