PROCEDURES AND FUNCTIONS IN .NET LANGUAGES Implicit coercion

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES Implicit coercion can be used to easily set the value of a SqlType. Also note that a SqlTypecannot be implicitly coerced to its corresponding CLR type. Some examples of this are shown in the following code fragment. // sets the Value of si to 12 SqlInt32 si = 12; // this will produce a compile-time error // because there is no implicit coercion from // SqlInt32 to int int i = si; // you must use explicit coercion int i1 = (int)si; SqlDouble and SqlSingle behave a bit differently from their CLR counterparts. SqlDoubleand SqlSinglecannot represent NaN or infinity. An attempt to set a SqlDoubleor SqlSingleto either of these will result in a runtime error. The following code fragment shows this. double d = double.Nan; // this will cause a runtime error SqlDouble sd = d; d = 4.7; // this will work as expected SqlDouble sd2 = d; The CLR defines several special names for methods that are used to implement binary and unary operators, such as op_Addition or op_LogicalNot. Most languages use these special names when a class implements them for operations like addition. Although the CLR doesn t prohibit it, some languages, including VB.NET and C#, do not allow you to use these names for methods in your classes or to call these methods. Instead, they have symbols that are used in their place for example + and ! . The methods for these special names, if a class has them, are implemented as static methods taking up to two instances of the class s type as parameters and returning an instance of the class s type. The following code fragment shows how SqlInt32implements op_Addition. public static SqlInt32 op_Addition(SqlInt32 x, SqlInt32 y); Because of this, you can add one SqlInt32 to another using the + operator. The following code fragment shows this. public SqlInt32 A = new SqlInt32(3); public SqlInt32 B = new SqlInt32(5); // this uses op_Addition to add A and B public SqlInt32 Sum = A + B;

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Bookmark the permalink.

Comments are closed.