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
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:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-set-a-null-value-to-a-value-type/
2011-12-05 by liuyunsx | Categories : C# | Tags: c#, dotnet, null, type | No Comments
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 :
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
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-calculate-the-time-interval-between-two-dates/
2011-12-05 by liuyunsx | Categories : C# | Tags: c#, date, dotnet, TimeSpan | No Comments
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).
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:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-retrieve-the-default-of-a-kind/
2011-12-04 by liuyunsx | Categories : C# | Tags: c#, dotnet, type | No Comments
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”:
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:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-perform-a-bit-shift-on-a-number/
2011-12-04 by liuyunsx | Categories : C# | Tags: c#, dotnet, number | No Comments
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)
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]); } } } |
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-declare-and-use-the-one-dimensional-arrays/
2011-11-12 by liuyunsx | Categories : C# | Tags: arrays, c#, dotnet | No Comments
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:
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 »
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-verify-that-an-object-is-of-a-certain-type/
2011-10-19 by liuyunsx | Categories : C# | Tags: . NET Framework, c#, dotnet, verify object, verify type | No Comments
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.
1 2 3 4 | void @while() { int @class = 2007; } |
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-use-a-reserved-keyword-as-a-variable-name-or-function/
2011-10-19 by liuyunsx | Categories : C# | Tags: . NET Framework, dotnet, keyword | No Comments
How to refer to the current object?
In C #, it is possible to explicitly access to members of the current object via the this keyword.
This allows an ambiguity if a local variable or parameter has the same name as a member of the current object:
1 2 3 4 5 6 7 8 9 | class ExempleThis { private String monString; public ExempleThis(string monString) { this.monString = monString; } } |
The keyword this can also refer to the current object, for example to pass it to another class:
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 | class Parent { private List<Enfants> _enfants = new List<Enfant>(); public IEnumerable<Enfant> Enfants { get { return _enfants; } } public Enfant AddEnfant() { Enfant e = new Enfant(this); // On passe l'objet courant en paramètre _enfants.Add(e); return e; } } class Enfant { public Enfant(Parent parent) { this.Parent = parent; } public Parent Parent { get; private set; } } |
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-refer-to-the-current-object/
2011-10-04 by liuyunsx | Categories : C# | Tags: . NET Framework, c#, dotnet, refer | No Comments