Operator and type conversion, operator type conversion

Source: Internet
Author: User

Operator and type conversion, operator type conversion
1. Operators

(1) Classification

Arithmetic Operators, Relational operators, logical operators, bit operators, value assignment operators, and other operators

>. Arithmetic operators:

Operator Description
+ Add two operands
- Subtract the second operand from the first operand
* Multiply two operands
/ Numerator divided by denominator
% Modulo operator, remainder after Division
++ Auto-increment operator. The integer is increased by 1.
-- The auto-subtraction operator reduces the integer by 1.

 

>. Relational operators:

Operator Description
= Check whether the values of the two operands are equal. If they are equal, the condition is true.
! = Check whether the values of the two operands are equal. If they are not equal, the condition is true.
> Check whether the value of the left operand is greater than the value of the right operand. If yes, the condition is true.
< Check whether the value of the left operand is smaller than the value of the right operand. If yes, the condition is true.
> = Check whether the value of the left operand is greater than or equal to the value of the right operand. If yes, the condition is true.
<= Check whether the value of the left operand is smaller than or equal to the value of the right operand. If yes, the condition is true.

 

Using System;

Namespace study
{
    Public class demo0_operator
    {
        Static void Main(string[] args)
        {
            UserControl u1 = new UserControl("1", 2, 3);
            UserControl u2 = new UserControl("1", 4, 5);
            Console.WriteLine("hashcode of u1 is=" + u1.GetHashCode());
            Console.WriteLine("hashcode of u2 is=" + u2.GetHashCode());
            Console.WriteLine("u1==u2 is " + (u1 == u2));

            //demo string
            String s1 = "hello word";
            String s2 = "hello word";
            String s3 = "hello " + "word";
            Console.WriteLine("hashcode of s1 is=" + s1.GetHashCode());
            Console.WriteLine("hashcode of s2 is=" + s2.GetHashCode());
            Console.WriteLine("hashcode of s3 is=" + s3.GetHashCode());
            Console.WriteLine("s1==s2 is " + (s1 == s2));
            Console.WriteLine("s1==s3 is " + (s1 == s3));

            //Run the result as follows:
            
            // hashcode of u1 is=1062342447
            // hashcode of u2 is=472133479
            // u1==u2 is True
            // hashcode of s1 is=-645689584
            // hashcode of s2 is=-645689584
            // hashcode of s3 is=-645689584
            // s1==s2 is True

        }
    }

    Public class UserControl
    {
        Public string id;
        Public int width;
        Public int height;

        Public UserControl(string id, int width, int height)
        {
            This.id = id;
            This.width = width;
            This.height = height;
        }

        Public static bool operator ==(UserControl lhs, UserControl rhs)
        {
            Return lhs.id == rhs.id;
        }

        Public static bool operator !=(UserControl lhs, UserControl rhs)
        {
            Return !(lhs == rhs);
        }

        Public override bool Equals(object obj)
        {
            If (obj is UserControl) return false;
            Return this == (UserControl)obj;
        }

        Public override int GetHashCode()
        {
            Return this.ToString().GetHashCode();
        }

        Public override string ToString()
        {
            Return "control id:" + this.id + "width" + this.width + ";height:" + this.height;
        }

    }
}
Operator Overloading and Comparison

 

>. Logical operators:

Operator Description
&& It is called logic and operator. If both operands are non-zero, the condition is true.
| It is called a logic or operator. If either of the two operands is non-zero, the condition is true.
! It is called a logical non-operator. Used to reverse the logic status of an operand. If the condition is true, the logical non-operator will make it false.

 

>. Bitwise OPERATOR:

Bitwise operators act on bitwise AND perform operations one by one. &, | And ^ are as follows:

P Q P & q P | q P ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

 

>. Assignment operator:

Operator Description
= A simple value assignment operator assigns the value of the right operand to the left operand.
+ = Add and assign an operator to assign the result of the right operand plus the left operand to the left operand
-= Subtract and assign the value operator. Assign the result of the left operand minus the right operand to the left operand.
* = Multiply the right operand by the result of the left operand and assign the value to the left operand.
/= Divide the left operand by the right operand and assign the value to the left operand.
% = Evaluate the modulo and assign the value operator. Evaluate the modulo value of the two operands to the left operand.
<= Left shift and value assignment operator
>>= Right Shift and value assignment operator
& = Bitwise AND value assignment operator
^ = Bitwise XOR and value assignment operator
| = Bitwise OR and value assignment operator

>. Other operators:

Operator Description
Sizeof () The size of the returned data type.
Typeof () Returns the class type.
& Return the address of the variable.
* Variable pointer.
? : Conditional expressions
Is Determines whether the object is of a certain type.
As Forced conversion does not throw an exception even if the conversion fails.

 

using System;

namespace study
{
    public class demo2_reflection
    {
        private string currentType = typeof(demo2_reflection).ToString();

        private string value;

        public demo2_reflection(string value)
        {
            this.value = value;
        }

        public void showName()
        {
            Console.WriteLine("currentType is " + currentType);
            Console.WriteLine("value=" + this.value);
        }

        static void Main(string[] args)
        {
            Type t = Type.GetType("study.demo2_reflection");
            Console.WriteLine("type of t is "+t.GetType().ToString());
            Object[] constructParms = new object[] {"hello word"};  //构造器参数
            demo2_reflection obj = (demo2_reflection)Activator.CreateInstance(t, constructParms);
            obj.showName();
        }
    }


}
Type

 

2. OperatorsPriority

Category Operator
Suffix () []->. ++ --
RMB 1 + -! ~ ++--(Type) * & sizeof
Multiplication and division */%
Addition and subtraction +-
Shift <>
Link <=> =
Equal =! =
Bit AND &
Bitwise OR XOR ^
Bit OR |
Logic AND &&
Logic OR |
Condition ? :
Assignment = + =-= * =/=%=>=<<<=<=^= | =
Comma ,

 

2. type conversion

Type conversion in C #

(1) implicit conversion

The conversion from type A to type B can be performed in all cases. The conversion rule is very simple and allows the compiler to execute the conversion.

Implicit conversion does not require any work or code writing. For example, convert int type data to double type data:

Int a = 10; double B = a; // implicit conversion

 

(2) Explicit conversions

The conversion from type A to type B can only be performed in some cases. The conversion rules are complicated and some type of extra processing should be performed. Explicit conversion is also called forced type conversion. explicit conversion requires you to explicitly specify the conversion type. For example, convert double type data to int type data:

Double c = 10.5; int d = (int) c; // display Conversion

 

(3) type conversion through methods

Serial number Method & Description
1 ToBooleanIf possible, convert the type to boolean.
2 ToByteConverts a data type to a byte type.
3 ToCharIf possible, convert the type to a single Unicode character type.
4 ToDateTimeConverts a type (integer or string type) to a date-time structure.
5 ToDecimalConvert the float or Integer type to the decimal type.
6 ToDoubleConvert the data type to the Double Precision Floating Point type.
7 ToInt16Converts a data type to a 16-bit integer type.
8 ToInt32Converts a data type to a 32-bit integer.
9 ToInt64Converts a data type to a 64-bit integer type.
10 ToSbyteConverts a data type to a signed byte type.
11 ToSingleConverts a data type to a small floating point number.
12 ToStringConverts a string type.
13 ToTypeConverts a type to a specified type.
14 ToUInt16Converts a data type to a 16-bit unsigned integer type.
15 ToUInt32Converts a data type to a 32-bit unsigned integer.
16 ToUInt64Converts a data type to a 64-bit unsigned integer type.
using System;

namespace study
{
    public static class  typeConversion
    {
        public static string timeToString(DateTime value)
        {
            return value.ToString("yyyy-MM-dd HH:mm:ss fff");
        }

        public static string timeToStringEx(this DateTime value)
        {
            return value.ToString("yyyy-MM-dd HH:mm:ss fff");
        }

        static void Main(string[] args)
        {
            DateTime now=DateTime.Now;
            Console.WriteLine("timeToString="+typeConversion.timeToString(now));
            Console.WriteLine("timeToStringEx="+now.timeToStringEx());
        }
    }
}
Time type conversion
using System;

namespace study
{
    public class typeConversion
    {
        static IUserControl getInstance(string controlName)
        {
            switch (controlName)
            {
                case "TextBox":
                    return new User_TextBox();
                case "Dropdown":
                    return new User_Dropdown();
                default:
                    return new User_TextBox();
            }


        }
        static void Main(string[] args)
        {
            IUserControl contorl1 = new User_TextBox();
            IUserControl contorl2 = new User_Dropdown();

            Console.WriteLine("type of contorl1" + contorl1.GetType().ToString());
            Console.WriteLine("type of contorl2" + contorl2.GetType().ToString());

            IUserControl contorl3 = (IUserControl)contorl2;
            Console.WriteLine("type of contorl3" + contorl3.GetType().ToString());

            IUserControl contorl4 = getInstance("TextBox");
            IUserControl contorl5 = getInstance("Dropdown");

            Console.WriteLine("type of contorl4" + contorl4.GetType().ToString());
            Console.WriteLine("type of contorl5" + contorl5.GetType().ToString());

        }

    }

    public interface IUserControl
    {
        void getControl();
        void checkdata();
        void resetControl();
    }

    public class User_TextBox : IUserControl
    {
        public void checkdata()
        {
            Console.WriteLine("checkdata is User_TextBox");
        }

        public void getControl()
        {
            Console.WriteLine("getControl is User_TextBox");
        }

        public void resetControl()
        {
            Console.WriteLine("resetControl is User_TextBox");
        }
    }
    public class User_Dropdown : IUserControl
    {
        public void checkdata()
        {
            Console.WriteLine("checkdata is User_Dropdown");
        }

        public void getControl()
        {
            Console.WriteLine("getControl is User_Dropdown");
        }

        public void resetControl()
        {
            Console.WriteLine("resetControl is User_Dropdown");
        }
    }

}
User Controls

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.