A cylinder device comprising: a piston inserted into a housing; a guide hole provided in the piston so as to open downward; a holding rod provided to protrude upward from a lower wall of the housing so that the holding rod is held in the guide hole; characterised by an electric or electronic switch fixed to a middle-lower portion of a rod hole, which is provided in the holding rod so as to extend in an up-down direction; a detection rod inserted ascendably and descendably into a middle-upper portion of the rod hole, the detection rod provided to detect a position of the piston; a plurality of support holes (22, 22) provided radially in the holding rod at predetermined intervals with respect to a circumferential direction of the holding rod; a first engagement ball and a second engagement ball respectively inserted into the support holes (22, 22) so as to be movable; a first cam surface configured to be in contact with the first engagement ball, the cam surface provided at an outer periphery of the detection rod so that a distance between the first cam surface and an axis of the detection rod increases upward; a first pushing portion configured to be in contact with the first engagement ball, the first pushing portion provided inside the guide hole so that a distance between the first pushing portion and the axis decreases upward; a second cam surface configured to be in contact with the second engagement ball, the second cam surface provided at the outer periphery of the detection rod so that a distance between the second cam surface and the axis decreases upward; and a second pushing portion configured to be in contact with the second engagement ball, the second pushing portion provided inside the guide hole so that a distance between the second pushing portion and the axis increases upward, wherein the position of the piston is detected by electrically or electronically detecting movement of the detection rod by the switch. a piston inserted into a housing; a guide hole provided in the piston so as to open downward; a holding rod provided to protrude upward from a lower wall of the housing so that the holding rod is held in the guide hole; characterised by an electric or electronic switch fixed to a middle-lower portion of a rod hole, which is provided in the holding rod so as to extend in an up-down direction; a detection rod inserted ascendably and descendably into a middle-upper portion of the rod hole, the detection rod provided to detect a position of the piston; a plurality of support holes (22, 22) provided radially in the holding rod at predetermined intervals with respect to a circumferential direction of the holding rod; a first engagement ball and a second engagement ball respectively inserted into the support holes (22, 22) so as to be movable; a first cam surface configured to be in contact with the first engagement ball, the cam surface provided at an outer periphery of the detection rod so that a distance between the first cam surface and an axis of the detection rod increases upward; a first pushing portion configured to be in contact with the first engagement ball, the first pushing portion provided inside the guide hole so that a distance between the first pushing portion and the axis decreases upward; a second cam surface configured to be in contact with the second engagement ball, the second cam surface provided at the outer periphery of the detection rod so that a distance between the second cam surface and the axis decreases upward; and a second pushing portion configured to be in contact with the second engagement ball, the second pushing portion provided inside the guide hole so that a distance between the second pushing portion and the axis increases upward, wherein the position of the piston is detected by electrically or electronically detecting movement of the detection rod by the switch.