331

3Programmer's Heaven C# School Book

Embed Size (px)

Citation preview

Page 1: 3Programmer's Heaven C# School Book

��������������� ���������

�� �� �� ������������������ � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������������������������������������������������������������� �� ������!�"�#��$ �����������������������������������������������������������������������������������������������������������������������������%���&���������'�(������� �'�)������������� ��!�*������������ ����������������������������������������������������������������++,��-����������������!������.���� �����������������������������������������������������������������������������������������������������������������������������-���#�'������� ���!�&�� ���/�� $���� � ���������������������������������������������������������������������������������������������������+%��(0��$� ��1��� �� �������������������������������������������������������������������������������������������������������������������������������������������+��2���������!�(3���� ���������������������������������������������������������������������������������������������������������������������������������������������4��5 ������!�5 �6��-$$� ��� �������������������������������������������������������������������������������������������������������������+4����/���5 �6���������!�&������2 ����70�������������������������������������������������������������������������������������������2����-�������� ���-2��*�����������������������������������������������������������������������������������������������������������������������������4����/��� � ��� �������������������������������������������������������������������������������������������������������������������������������������������������,�����8 ��� ���&#�����!�&���������������������������������������������������������������������������������������������������������������������������������%%����*�6������������������4 ������������������������������������������������������������������������������������������������������������������������������������,��8 ��9��- �� ���������������������������������������������������������������������������������������������������������������������������������������������%

Page 2: 3Programmer's Heaven C# School Book

��������������� ���������

�� �� �� ���������������� � ��������������������������������������������������������������������������������������������������������������������������������������������������������

������ ����������������������������������������������������������������������������������������������������������������������������������������������������������� ������!������"����������������������������������������������������������������������������������������������������������������������������������������������� ������#� ����� ��������������������������������������������������������������������������������������������������������������������������������������������� �����$��%������������ "�$��&����'��( ����������������������������������������������������������������������������������������������������� ��������� #� �����)� ����*�#)+��������������������������������������������������������������������������������������������������������� �,��#*,�����!�� �����"����#� �����+��"����������������������������������������������������������������������������������������������� �-��� �������.����*-����+����������������������������������������������������������������������������������������������������������������������� /���&����'��(���#�0����*&�#+��������������������������������������������������������������������������������������������������������������� /�������� #� ������.���!������ *�#�+ �������������������������������������������������������������������������������������������������� /�������� ��.������*���+�������������������������������������������������������������������������������������������������������������������123��0������������ *3�+ ���������������������������������������������������������������������������������������������������������������������������������12����$��&����'��( ����������������������������������������������������������������������������������������������������������������������������������������12�����.���"���44 ����������������������������������������������������������������������������������������������������������������������������������������12���5�������"���$����6 ������������������������������������������������������������������������������������������������������������������������������1 ���7���� "������� ��������������������������������������������������������������������������������������������������������������������������������������1 ����0�89���.������ "���5��'��0 �������������������������������������������������������������������������������������������������������11����� �:���&������������"�� ���%..������� � �� ���������������������������������������������������������������������������11���(� ��������5�������"���$�� ����������������������������������������������������������������������������������������������������������������11����5�������"���$�� ������������������������������������������������������������������������������������������������������������������������������������1;< "���� "� �������������"%..������� ��"�� �����������������������������������������������������������������������������������������1=$���.���� �� ��������������������������������������������������������������������������������������������������������������������������������������������1=����� �>��'��" ������������������������������������������������������������������������������������������������������������������������������������������1?������>��'��" �������������������������������������������������������������������������������������������������������������������������������������������1?���,�� *+,����" ������������������������������������������������������������������������������������������������������������������������������������������1?��� �� �� ����� ��� ������������������������������������������������������������������������������������������������������������������������������������1������ ����������������������������������������������������������������������������������������������������������������������������������������������������������1���.���� �.�� ��������0�� ��������������������������������������������������������������������������������������������������������������������������1�%����� ������������������"%..������� ���������������������������������������������������������������������������������������������������1/����� ������� ������������������"%..������� ����������������������������������������������������������������������������������1/

�������������������������� ������������������������������������������������������������������������������������������������������������������������������#�� ��� ������������������������������������������������������������������������������������������������������������������������������������������������������; @���������.�� "�������..� ������*����� ��.������+�����������������������������������������������������������; 5����0�������������������������������������������������������������������������������������������������������������������������������������������������������������;1�� �� �5����0�������0�� ������������������������������������������������������������������������������������������������������������������������;;$��� ��� �� ��� !�������0��� "�����"����������������������������������������������������������������������������������������������;;A.������� �� ������������������������������������������������������������������������������������������������������������������������������������������������;B%���������A.����������������������������������������������������������������������������������������������������������������������������������������������;B���!�8� "���!�8 ������ �������������������������������������������������������������������������������������������������������������������������������;?

Page 3: 3Programmer's Heaven C# School Book

��������������� ���������

/

%�� �� �A.������ ��������������������������������������������������������������������������������������������������������������������������������������;?)������ ��A.�����������������������������������������������������������������������������������������������������������������������������������������������;�A����A.������ ������������������������������������������������������������������������������������������������������������������������������������������������;/A.�����������"� �� ����������������������������������������������������������������������������������������������������������������������������������������;/&��'�� ����% "�� "���� ��������� � ������������������������������������������������������������������������������������������������������;/����!������������ ����������������������������������������������������������������������������������������������������������������������������������������;/���'���������������� � ������������������������������������������������������������������������������������������������������������������������������B1#��.� ��������������������������������������������������������������������������������������������������������������������������������������������������������B=���!��#��.�����������������������������������������������������������������������������������������������������������������������������������������������������B=������.���� �.�� ��0������!�����. ��������������������������������������������������������������������������������������������������������B?���"����'����#��. �����������������������������������������������������������������������������������������������������������������������������������������B�'����#��.��������������������������������������������������������������������������������������������������������������������������������������������������������B�%����� �� �����������������������������������������������������������������������������������������������������������������������������������������������������B/%�������������� ���������������������������������������������������������������������������������������������������������������������������������������������B/%���� �������������"� � �����������������������������������������������������������������������������������������������������������������B/!������#��. ����������������������������������������������������������������������������������������������������������������������������������������������������=2

��������������������� ��������������������������������������������������������������������������������������������������������������������������������������������#�� ��� ������������������������������������������������������������������������������������������������������������������������������������������������������=;�� ��.��!������������������������������������������������������������������������������������������������������������������������������������������������=;A07�����������������������������������������������������������������������������������������������������������������������������������������������������������������=;&���" ����������������������������������������������������������������������������������������������������������������������������������������������������������������=B,����"������������������������������������������������������������������������������������������������������������������������������������������������������������==� �� ����� ������� ���������������������������������������������������������������������������������������������������������������������������������������=?%���� �������0���!���� ��������������������������������������������������������������������������������������������������������������������=?%���,�"�!�����%����0�����#���� ��������������������������������������������������������������������������������������������������������=����.���������������������������������������������������������������������������������������������������������������������������������������������������������������?2<� ����.����� �����������������������������������������������������������������������������������������������������������������������������������������������? ��������� '�� �� �.��.����� �������������������������������������������������������������������������������������������������������������������?;������,��0���!������������������������������������������������������������������������������������������������������������������������������������?=����,����0���,����" ������������������������������������������������������������������������������������������������������������������������������?��� ������� �����������������������������������������������������������������������������������������������������������������������������������������������������?�&� ���C�*+,����"�!A07������ ���������������������������������������������������������������������������������������������������������������������2��������� ��������������������������������������������������������������������������������������������������������������������������������������������������������2,����"� "�� �������A������"� ���������������������������������������������������������������������������������������������������������������� A������"� ��� ������� ���������������������������������������������������������������������������������������������������������������������������������15������.�*���D��!>��'��"+ ��������������������������������������������������������������������������������������������������������������������;)�!��� ����.� �������������������������������������������������������������������������������������������������������������������������������������������������=���������0�����!��� ��� "�07��� ����������������������������������������������������������������������������������������������������������=

����� �� ������!�"�#��$ �����������������������������������������������������������������������������������������������������������������������������%#�� ��� ��������������������������������������������������������������������������������������������������������������������������������������������������������� ������ ������������������������������������������������������������������������������������������������������������������������������������������������������������� ������ ��� �� �����������������������������������������������������������������������������������������������������������������������������������������������/

Page 4: 3Programmer's Heaven C# School Book

��������������� ���������

2

��.���� �� �� ������ ��� ��������������������������������������������������������������������������������������������������������������������������2�� ������������ � ������ ���������������������������������������������������������������������������������������������������������������������������;���0��(��'��"E����� ��� ��������!���0��E����8.���������������������������������������������������������������������=��������"%���,�"�!��� ��������������������������������������������������������������������������������������������������������������������������������������������"� ��� ��%���,�"�!��� ��������������������������������������������������������������������������������������������������������������������"(��'��"��������������������������������������������������������������������������������������������������������������������������������������������A07������E���0���!�������������������������������������������������������������������������������������������������������������������������/�������.��� ��������������������������������������������������������������������������������������������������������������������������������������������������/2<� ������!��� ���!���0����.�!����!��� �� �����07����!����"��.������������������������������������������������/2<� ������"'��������� ���� ���@��� "�����0E��� �����������������������������������������������������������������/ A�����"� ���������"E�������� "������"�(��'��"���������������������������������������������������������������������������������/1��� �'(��'��" ���������������������������������������������������������������������������������������������������������������������������������������������/=��.����� �����07���E<.E���� �� "��' E���� � ��������������������������������������������������������������������������������/?����� "�(��'��" ������������������������������������������������������������������������������������������������������������������������������������/?@�8� �� "< E0�8� � �������������������������������������������������������������������������������������������������������������������������������������/�

���&���������'�(������� �'�)������������� ��!�*������������ ����������������������������������������������������������������++#�� ��� ������������������������������������������������������������������������������������������������������������������������������������������������������//���������*�����+ ���������������������������������������������������������������������������������������������������������������������������������������������//��!� � ������� �����������������������������������������������������������������������������������������������������������������������������������������������//� �� ����� ��������� ������������������������������������������������������������������������������������������������������������������������������������ 22������5������.������������������������������������������������������������������������������������������������������������������������������������� 2B6 �������� ��������������������������������������������������������������������������������������������������������������������������������������������������� 2=���$��"!��6 �������� ����������������������������������������������������������������������������������������������������������������������������� 2=<� �6 �������� *� ��+ ���������������������������������������������������������������������������������������������������������������������������� 2?,����0���6 �������� ������������������������������������������������������������������������������������������������������������������������������ 2�3��0������������ � �$������������������������������������������������������������������������������������������������������������������������������� 2���������� "���!���� ��A������"��������������������������������������������������������������������������������������������������������� 1������3���������*+�����" ������������������������������������������������������������������������������������������������������������������������� 1$���"����� �� �������������������������������������������������������������������������������������������������������������������������������������� 1

,��-����������������!������.���� ��������������������������������������������������������������������������������������������������������������������������#�� ��� ���������������������������������������������������������������������������������������������������������������������������������������������������� =%0��������� ��������������������������������������������������������������������������������������������������������������������������������������������� =� ���!��� �������������������������������������������������������������������������������������������������������������������������������������������������������� ���.���� �� �,������ A �� ���!����������������������������������������������������������������������������������������������������������� 1 68.�������.���� ����� �!�����" �������������������������������������������������������������������������������������������������������������� 1B���� ���� � ���!����� ��� "��.�������������������������������������������������������������������������������������������������� 1=% � ���!���� ������ �� �������� ���!�������������������������������������������������������������������������������������������������� 1�

���-���#�'������� ���!�&�� ���/�� $���� � ���������������������������������������������������������������������������������������������������+#�� ��� ���������������������������������������������������������������������������������������������������������������������������������������������������� 1/%����)������"��������������������������������������������������������������������������������������������������������������������������������������������� 1/,����"��� �� ��%������������������������������������������������������������������������������������������������������������������������������������ 1/� �� ����� �� "����� ��������� ��!�����"��� �� ������������������������������������������������������������������� ;

Page 5: 3Programmer's Heaven C# School Book

��������������� ���������

� �� ����� �� "����� �-����"%��������������������������������������������������������������������������������������������������������� ;1�����������.���� �.�� ��0��������"��� �� �������������������������������������������������������������������������������� ;B���!������#��. ������������������������������������������������������������������������������������������������������������������������������������������� ;=��������� ������������������������������������������������������������������������������������������������������������������������������������������������������ ;����%����#����� ���������������������������������������������������������������������������������������������������������������������������������������� ;��������(��� ����������������������������������������������������������������������������������������������������������������������������������������������� B ���F������� ��������������������������������������������������������������������������������������������������������������������������������������������� B;������ ���� ���������������������������������������������������������������������������������������������������������������������������������������������������� BB��������0����� ���������������������������������������������������������������������������������������������������������������������������������������� BB�� ������ �������0�� ������������������������������������������������������������������������������������������������������������������������������� BB%""� �������������0�� �������������������������������������������������������������������������������������������������������������������������� B=)������� �����!�����������0���������������������������������������������������������������������������������������������������������������� B=)����� ��.������������� ���������������������������������������������������������������������������������������������������������������������������� B=3���� ������������� �!(��� "������������������������������������������������������������������������������������������������������������ B?����(� �!������8��� ���!�.�������������� ������0������������������������������������������������������������������������� B���������"#����� ��������������������������������������������������������������������������������������������������������������������������������������� B����� ��� "�� �� �� ������������������������������������������������������������������������������������������������������������������������������������ B/������ ����� "�����0���������������������������������������������������������������������������������������������������������������������� =2������� �@���"����� ���������������������������������������������������������������������������������������������������������������������������������� =1

%��(0��$� ��1��� �� �������������������������������������������������������������������������������������������������������������������������������������������#�� ��� ���������������������������������������������������������������������������������������������������������������������������������������������������� ==68��.��� @���������������������������������������������������������������������������������������������������������������������������������������������� ==��� ��"!��68��.��� ��������������������������������������������������������������������������������������������������������������������������������� ==68��.��� � ��� "�$�������������������������������������������������������������������������������������������������������������������������������� =��� "�� �68��.��� �� ������������������!� ����0���( ����������������������������������������������������������������������������� =�<��!��������������0���(����������������������������������������������������������������������������������������������������������������������������� =�68��.��� ���G,����� "����(��������.����� ������������������������������������������������������������������������������������ =/���!� ����0���( �������������������������������������������������������������������������������������������������������������������������������������������� ? ������ �,����.��68��.��� �� ������.�������0���( ��������������������������������������������������������������������������� ?;% ��.���� �.�� ��������0��� �����.�������0���(����������������������������������������������������������������������������� ??A������.���� �.�� ��0���68��.��� �� "�� �� ��������������������������������������������������������������������������������� ?���!� � ������' ������8��.��� ����������������������������������������������������������������������������������������������������������� ?�68��.��� ���������� ����$��&����'��(�������������������������������������������������������������������������������������������������� ?�����'� �� �8��.��� ��������'(��'��"��������������������������������������������������������������������������������������������������� ?/

+��2���������!�(3���� ��������������������������������������������������������������������������������������������������������������������������������������������#�� ��� ���������������������������������������������������������������������������������������������������������������������������������������������������� � ��������@������������������������������������������������������������������������������������������������������������������������������������������������ � �����.����� ������!��������"���"��������� .�� �������������������������������������������������������������������������� � ���"���������!��� ��9������ 0���"����!��� ��������" �������������������������������������������������������������������� �;;���������������"��!��� ��"0����"������� �������������������������������������������������������������������������������������������� �;����� ���������������"���������"������� ���������������������������������������������������������������������������������������������� �;�� !��� � ����� ������������������������������������������������������������������������������������������������������������������������������������ �?

Page 6: 3Programmer's Heaven C# School Book

��������������� ���������

1

��������� ����$��&����'��( ������������������������������������������������������������������������������������������������������������������� �?��� �"��������������" ������������������������������������������������������������������������������������������������������������������������� �?,��������������� ���������������������������������������������������������������������������������������������������������������������������������������� ����.���� �� ��,��������������� ��������������������������������������������������������������������������������������������������������������� ��)����� �������"!��������������"�������G� ������� ��� ����������������������������������������������������������������� �26�� �� "6�� ��� "�� � ���������������������������������������������������������������������������������������������������������������������������� � 6�� ��� "�� �� ��������������������������������������������������������������������������������������������������������������������������������������� � %����(�����68��.�� �������������������������������������������������������������������������������������������������������������������������������� �;,���������� ����������������������������������������������������������������������������������������������������������������������������������������������� �=��� ����"���'������6�� ����0E���� �������6�� �%�� ������������������������������������������������������������� �?

�4��5 ������!�5 �6��-$$� ��� �������������������������������������������������������������������������������������������������������������+4#�� ��� ���������������������������������������������������������������������������������������������������������������������������������������������������� /2�� "�'%..������� � "�$����������������������������������������������������������������������������������������������������������������������� /2�� &���@��� ��������������������������������������������������������������������������������������������������������������������������������������������� /2@���"� ����H������� &���H%..������� ��������������������������������������������������������������������������������������������������� / < "���� "� ������"� ��������������������������������������������������������������������������������������������������������������������������������� /1%""� �6�� ��� "�� ����������������������������������������������������������������������������������������������������������������������������������� /;5�������"���$��D����6*� �������"������.�� �6 ���� �� �+����������������������������������������������������������� /?� ������� �� "������.���� ����������������������������������������������������������������������������������������������������������������������� /?��"�&��"� � �������������������������������������������������������������������������������������������������������������������������������������������������� /�� �������"���.����9������� 0���"��� "��0����� ������������������������������������������������������������������������������������ /�&������� �������������������������������������������������������������������������������������������������������������������������������������������������� /�������� 68.����� ������������������������������������������������������������������������������������������������������������������������������������������� /�,� �� ���5�������"���$����6������������������������������������������������������������������������������������������������������������� //<� �5�������"���$����0���"���H������� &���H%..������� ������������������������������������������������������������122������ �� �'���7��� �����������������������������������������������������������������������������������������������������������������������������������122����� �������.��.������!���!���������������������������������������������������������������������������������������������������������������12 %""� ��� ���������&��� �������������������������������������������������������������������������������������������������������������������������121%""� �6�� ��� "�� �����������������������������������������������������������������������������������������������������������������������������������12;68����� �����..������� �������������������������������������������������������������������������������������������������������������������������������12;�����"��� �����"0����&������� �������������������������������������������������������������������������������������������������������12B<� �,����� ������������������������������������������������������������������������������������������������������������������������������������������12B<� ��������� ����� � �..������� �����������G���..� �������������������������������������������������������������12=���� � ����!���� ".���� ������ ������������������������������������������������������������������������������������������������������12?����� ���"�!��6�� ��� "�� � ������������������������������������������������������������������������������������������������������������������12�������.���� ���� �!��"��� � ��� "�'%..������� �����������������������������������������������������������������������1 2

����/���5 �6���������!�&������2 ����70���������������������������������������������������������������������������������������#�� ��� ����������������������������������������������������������������������������������������������������������������������������������������������������1 ��������� �� ���� ����������������������������������������������������������������������������������������������������������������������������������������1 #��@�8�� �������������������������������������������������������������������������������������������������������������������������������������������������1 %""� �������������0�8 ���������������������������������������������������������������������������������������������������������������������������1 1%���� ������ ������0�8�����������������������������������������������������������������������������������������������������������������������1 1

Page 7: 3Programmer's Heaven C# School Book

��������������� ���������

;

)����� �����!���������0�8 ������������������������������������������������������������������������������������������������������������������1 ;#��@�86�� � ����������������������������������������������������������������������������������������������������������������������������������������������1 ;���0�@�8�� ���� ���������������������������������������������������������������������������������������������������������������������������������������1 B����5��'�������������������������������������������������������������������������������������������������������������������������������������������������������1 B�������$�"�6"������������������������������������������������������������������������������������������������������������������������������������������1 =%""� �I)����� ��������� ���� ���������������������������������������������������������������������������������������������������������������1 ?����5��'6�� � �������������������������������������������������������������������������������������������������������������������������������������������1 ������#���� ���� �����������������������������������������������������������������������������������������������������������������������������������������1 /%������ �% �����#����"�!!��� ��� ���� �����������������������������������������������������������������������������������������������1 /#��5��'�� �����������������������������������������������������������������������������������������������������������������������������������������������112�'������#��� ���#��5��'�� ���������������������������������������������������������������������������������������������������������112%""� ����������������'�� ������ �"��� �� �������������������������������������������������������������������������������������112%""� ��������� ������ ���"����������������������������������������������������������������������������������������������������������������11 6�� �!��#��5��'�� ���� �������������������������������������������������������������������������������������������������������������������������111,�� ,� � �����������������������������������������������������������������������������������������������������������������������������������������������������111����@�� ���������������������������������������������������������������������������������������������������������������������������������������������������������11;�����������(����������������������������������������������������������������������������������������������������������������������������������������������11;�� "�'��� "��"������@�8��������������������������������������������������������������������������������������������������������������������11BA.� &���������@�8 ������������������������������������������������������������������������������������������������������������������������������������11B<� ����A.� &���������@�8 ��������������������������������������������������������������������������������������������������������������������11=����&���������@�8 �������������������������������������������������������������������������������������������������������������������������������������11?&� �� "�����������@�8��������������������������������������������������������������������������������������������������������������������������11�

����2����-�������� ���-2��*�����������������������������������������������������������������������������������������������������������������������������4#�� ��� ����������������������������������������������������������������������������������������������������������������������������������������������������1;2� ���"��� �%�A�$���������������������������������������������������������������������������������������������������������������������������������������1;2��!!��� ����.� � ��!%�A�$�� ����������������������������������������������������������������������������������������������������������������1; %�����'�!0����F#J����������������������������������������������������������������������������������������������������������������������������1;1�F#�6#6��������� ���������������������������������������������������������������������������������������������������������������������������������1;1�F#�$�6)�������� � ��������������������������������������������������������������������������������������������������������������������������������1;1�F#<��%�6������� ��������������������������������������������������������������������������������������������������������������������������������1;1�F#�6#6�6������� � �������������������������������������������������������������������������������������������������������������������������������1;;���!���� ������ "���������('���%�A�$�� ����������������������������������������������������������������������������������1;;%���� ������� �%�A�$�����������������������������������������������������������������������������������������������������������������������1;B��!� � ������ ����� ��� � �����������������������������������������������������������������������������������������������������������������������1;B��!� � ���� ����� �����������������������������������������������������������������������������������������������������������������������������������1;=��!� � ��������� "������� "��� � ����������������������������������������������������������������������������������������������������1;=��!� � ��������%"�.��� �����������������������������������������������������������������������������������������������������������������������������1;?������ �� "!���� ����������� ���������������������������������������������������������������������������������������������������������������������1;?%���� ������ %..������� ������������������������������������������������������������������������������������������������������������������������1;�#��"� ���0�� ������������������������������������������������������������������������������������������������������������������������������������������������1;�&���� ������ ����� ���&������������������������������������������������������������������������������������������������������������������������1;/$������� ����������������" ����������������������������������������������������������������������������������������������������������������������1B

Page 8: 3Programmer's Heaven C# School Book

��������������� ���������

B

<."��� ������0�� ������������������������������������������������������������������������������������������������������������������������������������������1B1@���"� ����%..������� ��������������������������������������������������������������������������������������������������������������������������������1B;#��"� ������0��� ""�.���� �"���� ���!���G�� ���� �������������������������������������������������������������������������1BB� ������ ������ " ��������������������������������������������������������������������������������������������������������������������������������������1BB%""� �������������������� "�������������������������������������������������������������������������������������������������������������1B?����������� ����*+�����"�!����..������� �������������������������������������������������������������������������������������������1B?6"��� �*��<."��� �+)����" �����������������������������������������������������������������������������������������������������������������������1B�6�� ��� "���!���������@���� �����������������������������������������������������������������������������������������������������������������1B�6�� ��� "���!������� ���@���� �������������������������������������������������������������������������������������������������������������1=2� ���� �)����"��������������������������������������������������������������������������������������������������������������������������������������������1= ������ ��)����"�������������������������������������������������������������������������������������������������������������������������������������������1=1<� ������"�����"��� ��������������������������������������������������������������������������������������������������������������������������������1=B���.�������"�����"���������������������������������������������������������������������������������������������������������������������������������1=B<��%�6�����"�����"�������������������������������������������������������������������������������������������������������������������������������1=B�$�6)������"�����"��� �����������������������������������������������������������������������������������������������������������������������������1==�6#6�6�����"�����"��� ����������������������������������������������������������������������������������������������������������������������������1==�6#6�������"�����"��������������������������������������������������������������������������������������������������������������������������������1==<� ������"�����"���'���%�A�$��� ������������������������������������������������������������������������������������������������1=?�����"�!��"� ������C������ "*+�����" �������������������������������������������������������������������������������������������������1=�<� �����3��"�� ������5��'�$��"��� ���������������������������������������������������������������������������������������������������1=/%���� ������ %..������� !������3��"�� �����������������������������������������������������������������������������������������1=/���� "���� ������ E<� ������.��������"��0�� ����������������������������������������������������������������������������������1?2)������� �"����� �����6#6������� " �����������������������������������������������������������������������������������������������1?1<."��� �)����"�� ��$�6)�9<��%�6� "�6#6�6����� "��������������������������������������������������������1?B

����/��� � ��� �������������������������������������������������������������������������������������������������������������������������������������������������,�#�� ��� ����������������������������������������������������������������������������������������������������������������������������������������������������1?=�����,���������"� ������������������������������������������������������������������������������������������������������������������������������������1?=,���������"� �� �� ��������������������������������������������������������������������������������������������������������������������������������������1?������"&� ���� ����� ��������������������������������������������������������������������������������������������������������������������������������������1?/���������0���!��������������"� �������"�������������������������������������������������������������������������������������1?/� �� �����0���!��������������� �������"�����������������������������������������������������������������������������������1�2�����"���� ������ 68��.��E@���A.������ ��������������������������������������������������������������������������������������1�2�����"���� ������ 68��.��E�����"�������������������������������������������������������������������������������������������������1�B�����"���� ������ 68��.��E�����"68������ �� ����������������������������������������������������������������������������1��<� �-�� *+��'���!���� � ������" ��������������������������������������������������������������������������������������������������������1�/�����"�� ���� �C���� ���������������������������������������������������������������������������������������������������������������������������������1�1�����#��(� �,���� �� ��������������������������������������������������������������������������������������������������������������������������1�1�����"����������"���( �������������������������������������������������������������������������������������������������������������������������1�=

����8 ��� ���&#�����!�&���������������������������������������������������������������������������������������������������������������������������������%%#�� ��� ����������������������������������������������������������������������������������������������������������������������������������������������������1�����(� �'������&��������������������������������������������������������������������������������������������������������������������������������1��A0��� � ����%..������� K6 ���� �� �� !������� L���������6 ���� �� ���� ����������������������������1��

Page 9: 3Programmer's Heaven C# School Book

��������������� ���������

=

���� ������ %..������� L6 ���� �� �� !������� �������������������������������������������������������������������������������1��A0��� � ����.����!�������� "�'��� "��"!��"��L6 ���� �� ��3��&��"������*+ ������������������������1/2,� �.����� �&����� ��������A�&���� "�������A�&���� !����� ����������������������������������������������������1/1�������A�&������ ���������������������������������������������������������������������������������������������������������������������������������������1/1������ ��!����� �������*+�����" ������������������������������������������������������������������������������������������������������������1/;��.�� �� ",��� ��!����� ���.�*+� ",���*+�����" ��������������������������������������������������������������������1/B����(� �����8��� ���!���!����� �68��*+�����" ������������������������������������������������������������������������������1/B3���� �%����0����!�!����� �3��%����0���*+�����"������������������������������������������������������������������������������1/B�������A�&���� !���� ��������������������������������������������������������������������������������������������������������������������������������1/=%J���(� "��.���8��.�� ��������������������������������������������������������������������������������������������������������������������������1/=,� �.����� �����������*!��"��+�� ��������A����������� "�������A����������� !����� ���������1/?�������A�������������������������������������������������������������������������������������������������������������������������������������������1/������� �9"����� �� "����(� �!������8��� ���!"�����������������������������������������������������������������������������1/�3���� ������ �� �*!���� "�0E"���������+�!�"�������� �����������������������������������������������������������������������1/��������A����������� !���� �����������������������������������������������������������������������������������������������������������������������1//���� ������ �..������� !��������������� !���� ����������������������������������������������������������������������������������;22�����������������������������������������������������������������������������������������������������������������������������������������������������������������;2 % �������'�!���"�!!��� ���.��!����� �����������������������������������������������������������������������������������������������;21������������������L���0���!��������� ����$��!����'��( ����������������������������������������������������;21��!!��� ���.��!!��������L)��"� �� "����� ���!��������������������������������������������������������������������������;2;<� ��������A�&��������������"� "'����"�����!��� ��������������������������������������������������������������������������;2;%��� ���.��� �� ����.���� " ����!���!�������������������������������������������������������������������������������������������;2;A.� � �� "���"� �!����!������������������������������������������������������������������������������������������������������������������������;2B<� �@� ���)��"��� "@� ��������������"� "'����.����������!��� �������������������������������������������������;2?<� �������)��"��� "�����������������"� "'������8�!������������������������������������������������������������������;2��������C���� � "��E������C���� ������������������������������������������������������������������������������������������������������������������;2/��.���� �� ��������C���� � "��������C���� L%��.���8��.�� �����������������������������������������������������������;2/&��������� �������C���� �����������������������������������������������������������������������������������������������������������������������������; 1����� �� ������� ����� �!����������C� �L���M$� �������C�"N�����0�������������������������������������������������; 13���� � ���!��"'�� ��������C� �E������������C���� ����@��(� ���!��� ����������������������������������������������; =%� ���� ��)��"� �� "����� �'��������� ����������������������������������������������������������������������������������������; �%"��� ������ �..������� ��������������������������������������������������������������������������������������������������������������������������; /���)����"� �%� ���� ��)��"I����� �������������������������������������������������������������������������������������������������;12��.���� �.�� ������"� �������!�����������������������������������������������������������������������������������������������������;1

����*�6������������������4 ������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������������������������������������������������������������������;11��� ��"!���� ���� �������������������������������������������������������������������������������������������������������������������������������������;113� ������������� �����������������������������������������������������������������������������������������������������������������������������������������;1;������ ��� ������.� ������������������������������������������������������������������������������������������������������������������������������������;1=�� ���� � ���.�.���������������������������������������������������������������������������������������������������������������������������������;1�&� ���������� �� ����������������������������������������������������������������������������������������������������������������������������������;1/���������.� ����������������������������������������������������������������������������������������������������������������������������������������������������;1/

Page 10: 3Programmer's Heaven C# School Book

��������������� ���������

?

$����0����.��������������������������������������������������������������������������������������������������������������������������������������������������;;1% � ���������"� ��� ��� "�� � ���������������������������������������������������������������������������������������������������������;;B%"�� ����'���� � ���������" �����������������������������������������������������������������������������������������������������������;;=&� ���������� ��1�2 ��������������������������������������������������������������������������������������������������������������������������������;;�

�,��8 ��9��- �� ���������������������������������������������������������������������������������������������������������������������������������������������%#��� � �,���������������������������������������������������������������������������������������������������������������������������������������������������;;�3���� ����. ���������������������������������������������������������������������������������������������������������������������������������������������������;;�@��(������� 44���������������������������������������������������������������������������������������������������������������������������������������������;;�3��"#��(O ����������������������������������������������������������������������������������������������������������������������������������������������������;;�

Page 11: 3Programmer's Heaven C# School Book

��������������� ���������

���������� ��

8 ��5�#�

���0��(�"���"�"� �������.��������������� ���!���0��� ������'�'���0�"���� �����$��&����'��(9��#� �����&� "��� ���� "A07���A��� ��"��������� ��� ������ "� �����"��������� '�'������ ��"�.��'���A07���A��� ��"�� ���������� ������ ��9.������.���9�0���������9� ���!���9��������9� �������� � "�8��.��� �� �������"� "!� ���"�� ��"����� '�'���"����� ��'������J����"����.���� �����'���"�..������� �� ���'���@��#�0�����9!���� �� ��.��������������� 9��������96�� �� "�� "�'��������� �'���� ��0���!�� ����9�'��������%���'���%�A�$��9�����"� "�������

8���.�� �������

A���8��.������0�� '����� '���� ����� "��"��6*5�������"���$��+���������� "��������0��� ����,�����!��������� ����!!��� �5�������"���$��122=68.��6"���� !��!���9� "����������0�������.�II�" �������!�����I���"��I�8.��I�% ����� ������.� �������69����.������.9�������0��!������.�II'''������.��"�����IA.� ������I��I�������.��0������� ���8��"����*����$���.�"+��'�����������"��:��'��� ��"��"�' ���"� "� ��������$��&����'��(��>9'������ 0��0��� �"!�����!������.�II�" �������!�����I ��!����'��(I"�' ���"I��'�������.����� ��"�"���� �$6��..������� � "9������.���� ���!���9�� ��� ��������.�����������'��� ��"�����.���.���������'�����&� ����9!�� � E�� "�'���9���,� ����7����..���� �.� ����������.����9�$���� ����� "�����0������.���� ����� �&������� !������� 9�����.�II'''��� �E.��7�������I� �����"��8��.��� ���0��('���'����� � "����"'�������$��&����'��(����� � � "5�������"��122;90������"'��(!� �'������������� ��������� ��0�� ����"'���,� ��

8 ��������������

��*.�� �� ��"�E����.+� �"��0������ ������!������� ����$��� ���� �� ������'���� �'�� �����!����!���0��('��"���.���0���������'����'����0� ���! �'9�8���� �� ".����� �!����������� A07���A��� ��"��������� ��� ������ "����������9�� �������������-���9�44� "5@�� !���9�����0� ����.�'��� "�!!���� ���!�449�����.��� "���� AA"��� �!-���� "����� �������.��!������ �!5����@����

Page 12: 3Programmer's Heaven C# School Book

��������������� ���������

#�(�-���9�����"�� ������'�����.��� ������ ����������!.�� ���*� �!�I�� ���"��"�+90��"��.����"����0������������������ ���� ����9��.�� "��������������(� ����'����9�� �������-%5%9����� ��� ���� �J����!���.������ �!�44��(��.�������������"� �9� �������� 9.��E.������"��������9.�� ���*� � �� ���"I� E�!���"�+9!� ���� .�� ���*� ���!����!"�������+� ".��������������.�����..���� ��� �8������ �#�(�5@9������..�������� ��.��!.��.�����*�� ��8�� �����!���"+�� �""���� �����9�������.'������ �'� "�8���� �!������������!������ 9�����0���9�������� �9������ �9�����"9�����9"�������'���%�A�$��� "����

8 ���*���-�� ������������*��������6�:�

� ����$��%������������ "����$��&����'��(��������"�!!��� ���.���� ������ "�� ��.�'����'�'���"���� �0�� ��E

8 ����������������9��� ���;��9<�

��������.���� ��� ��.��!����$��&����'��(�����8��� ��� "!� ���� ������!����$������� #� �����)� ����*�#)+9��������"�$��)� ����!�����������!����'��(������������"��0������A�� "�� "������8������ �!�������$���..������� �A��.������"� G�"������������ �����'������A�0���������������#)�

/&���;/ ���.��������� ������������<����

��� '����.�������$����������� �� ��$�����.��� ��� �����*������95@�$�����44�$��+����������"�"�� ������� �����"� ������8�����0��0� �����"�90����� � �����"������"�( �' �,��#'������ ���.����"0��������� #� �����)� �����,��#��.����� ������ "���"'���� "�.� "� ���"��<.� .�������8������ 9���,��#*� �����"������"�+��� �����"��0� ����8�����0����"�* �������"�+������ ������������ ��.���.��0������,��#��"��������!�������$���� ������

��� ����#��*������������

/&��� ����

(0���������*�� 3�����

#� ��������.����

-��� �������.����

���������� �� ���

5 �6���&�

��������������9��� ���;��9<�

�����*���-$$� ��� ���

Page 13: 3Programmer's Heaven C# School Book

��������������� ���������

/

=�������8 �����$ �����;=�8���<�

��� ����#���.���"��"� ��"��0��8�����"9����#)� ��(����-�����.����9'�������.�������#��"��� ������8�����0����"�*��8����"��+�����"��� �"!�����.���!������� �� "A��-����� �� �'�����"�!!��� �!������"���� �����.������������.�������#�� �������"�� ��'�� "����"P����9'�� �!� ���� ������"9����#�!���!� ���� G0�"���� �����"�� �������"����� ������9���.����!��"������ ����"0�����.����������� � ������ �����"�� �������"���!����#��"���� �����"�� �������"�9��� ��� �8�������G ��"�"9����#)����������*�����"����.���"+��.�'��������E���.��� ����9�!�.�������� !���������*���� ��������������!���!� ���� ��������"+9��� ��'� G������ �7��E� E����.��!���� ��.� �����%-��������'����!���.���!��.������� "A����� ����9������ �.����C������"��8��������!!���� ��������� �� ������0���..������� �%��9� ���-�����.����( �'����8�������� ������!�8�����0����"�9������ ����.����C������"�0�� E�� � �����!� ���� ����*��(���.���� �0�"��!����!� ���� '�� �������"� ����.9��� ����!� ���� ��������+�%�������,�����!�����"������� "�$����� �����.��� �'����� �������(��44� �!!���� ��� ".��"�!�8������ 9-������ ��(�������"���� !������� �44��"�� ������'�� ���.��������� ����� �8�� "�".����"�!����*��(�'�0E�����+�

8 �������6�:�������� ����#�;���<�

����$��&����'��(.����"������&����'��(*��@��+���#�0����*&�#+!������� 9������(�&�#�� ��� ����� "�!������.����"��������� "�'%��� "����� !� ���� ��(����� �,� �.������ 9����� �������������9�A9������9�����"9��������9$��'��(��������� �9�� "�'��������� �9��0��������� �9����%���9���������.������������� "��"��0����������..�"'���� �"�����.�� �� ���� �� ���.�������� ��� ���������0��.����!�����0���������!����'�8��������!!���� �AA"��� *"��� .����� +��(� ����������� "��������.��� ".��"����0���:���� ���������� &�#� ����.������7������'���"��� ����������:���� ��� �..��� ������ ��� ".������.�������������� �

8 ����������������&$�� . ��� ��;��&<�

6������9'���"�������G�$�����.��� �#� �����G� "����"�����������$�����.��� ��� ������� ��(����!�#)� "&�#�@��'�����(���� ������G�$�����.��� �G�� �����Q���� '����������� #� ������.���!������ *�#�+�,�����!���������"��������!.���!������ ���������� ���������"������J����!����$�����.��� �#� ������%�#������������ �����9��� �� ������!����� ���������.���� ��������#!� ���� �����P������9�������� ��"������������0���!�#���J����!����$�����.��� ��� ����������������� '����� ��� �����*.����"����� "AA+��� �'�� � �� "������$����0�������#�0��������""����� �����"��� ���� "���"�' ������ �� "��"�&��� �� ��9���������" G�0�� ����0��!� ���� "��������� 9 �.�� ���9 ������.��� ������ ��� "��� ���(�����������.���� �.�� ��� �������������!���(��.������"�'���� ����#�0�� "���9������"������� ���"��0���0��� � �������$���� ������

Page 14: 3Programmer's Heaven C# School Book

��������������� ���������

12

8 �������8#$��&#�����;�8&<�

�$�����"�!� ������� ��.������*���+�#�(��#�9�����������!�� "��"����"�!� ����0���"�����.������#� "���� "�6����$�����.��� ��� ���������"��.��"�����.��������� "��""�����.�������(���.��0��!�����1�� ������������ �����'������������0�.�� �I������� �.����������� "!�������������&���8��.��9���"�!� ����.�9�����9� � ������"�����.��!;10��*B0���+'�������..�"0�������������� "5@�$����������������"�����.��

)������������� ��;)�<�

�#)����� ��� ���3��0������������*3�+9'������ � ���'E.������������"� "����(!��� E��!��� ��"9"� ����������������"������.�����!��!� "���"�������� ��� �����!��� ��"0�� ������0��I��!��� ��9����E�������� "����� �������A�����.��� ���!��� "��"3��0������������!������.���������!���(��.� �����(�!"� ��� �"����%(� ��44.�����������'0��������!���O

8 ���*��������6�:�

����$��&����'��(�������0� ���� �!������!�#)9&�#9����� "R,#����� "����� "�'9��0�..������� � "��0��������%"�������!����$��&����'��(�.��� ��"0���'!��0������ "���� "� ��

�����$�������>>�

� �����!.��!���� ��� "�!!���� ��� ������!������� "������������9�44"�����������@�����.��!���� ��� ".��"���� ��������'�� ������������� ��"�����.�� �� ���� �� �Q$�O�44� �"��0���������.��89�0������ "��'E������� �������.���������0��"� .���������'����� ���.� �0������� "���..����% �����.��0���'����44�������������� �������������9'�������� �

5 �6���&�

��������������9��� ���;��9<�*��0�����9��.�����(� �9-��9�8��.��� 93�+

�����6�:�������� ����#�;���<�*�A9������9���(��9��������9)�!������ 9<�+

2����;-2��*��<����?/��� ����#�

�����*���-$$� ��� ���*�� &���9��0%..������� 9��0�������+

Page 15: 3Programmer's Heaven C# School Book

��������������� ���������

1

�� ������� �����9� "�� ��!�������������.�������P����9���� "�����"���� ����������� �������������0��9.�� �������!��� ��������������������.�����.�'��!����"�� �� �����'������0�� ��"�'����(��.� �� �� "����8.���� ���!"�����.�������������� "���������� � ��"�A ��!���0������� ��� � ���"�����.�� ����(�������(�.���������������E��0��9��� ��� �0��9����0��9.����0��� "�����"�0��������������'�����������6����� ����44"�����.��( �'��'"�!!������������ ���0������44.������� ""�0����������� 0�� ����������!� "������� '���.������������� "��������������� !�����9����9����0��('��"E���.���0����������������"��44� ������������!�9���������".�������� ��� �����9��� ����0�����������07������� ��".�������� ��� ������ ��9� "�!������.��������'������� �����������"'���!���� �.��0���9'������������(�����(�� �07������� ��"�..�����Q%��@7�� ���������.9���!�� "���!�44��"9H�44�������E.���"����� ����� ��� ��AA�� �����H����.�����"�� �����!�� ���� ���"��..���!���������� �� ������� ����9&����'��(�����0����9� "� �'9���� �07������� ��""��� !����!���0��('��"E���.���0����������

8 ��@ �����&�� �*����2(�

,�����!�5�������"���$���� � �������"������.�� �6 ���� �� �*��6+9'���������������!5�������"��?���������"�����.�� �.�����!����$��%..������� *5���$��95@�$��95�44�$��9-����.��$��9-��$��9%���$��9� "����+������������� ����..������ ��� �'��.����"����� �����!��������5�������"���$�����.��� �#� ���������������69"�0�����9.��7���� "������ �8.�����9������'9.��.�������09����0�89�� "��"�� �� "����0������(��!�������!5�������"���$��� ���"�������6.����"���������!��"�����.�� ����������

�� >��'��"� "� ��8���������� � �� � ������ �*�������.����+9'�������.0�����������������.���� ����� ��8������.��"��*�+

'����07���9� �������� 9 ���.���� "'�� ��������S$�'T(��'��"��� ���7���� "������ �� ����� �'��������� �8.������������.���� ����..������� �� ��� ��!

�����.��!���� �� ���.0���"� ����� ���!���'�����.��"���� ""��.�..���� �� ���.�������0��������'�������"�!!��� �.��.�����!�������.��'� "�'� "'�0�� ����� �� ��� "��""�0�������������'�����"�0������.�������� �.���� �0���(.�� �!���0���� �

�� E����0�������� �� ������.������������(���� ��8�!������"�������.���� "����� ���!������ � �� �� �������.� � ��0���!��.���� ����,�����!�������.�� �$��'��(*,��$+��0������ ��� ���.��� �� "0���"� ��..������� � �� �������68������ '�����'���������"�0������ �� ��.���� ������$���..������� �������� ��� ������"�(�

"����������&��� ���

%���7��������0� ���� �!�8�����0��� "��0����!���������(�� �..������� ����"����%.��7���G� !������� �������.����"� �!���'�������8�� ��� G��.��7G'����G�G��.��� ��E����.����������95@�$��

Page 16: 3Programmer's Heaven C# School Book

��������������� ���������

11

.��7����������"�G��0.��7G!������������������"�!!��� �(� "�!.��7��������� ���%..������� 9�� "�'%..������� 9%���$����0%..������� 9���#�0������ "����� %������ � ���������� "��.�������"��!��"�!!��� ����������������".��7���������(�����..������� �&���8��.��9������� ����� ���!� %���$����0%..������� .��7���� "��� "�'&���.��7�������� !������� !��������� �����"� G�� G!���� "�� 0��� ���"�� �5�������"���$��G������� 68.������������� �����������5@?G���7���3���.� "5�44?G'��(.����

8��0'�"�$��� ������������@ �6�8����

$�'�������� �������0�8!��������5�������"���$��G�� ������ "������������0�8*������.��� �� �����!��� "�"�+�� ��� � ��0���!����� �� ����!��'� "�'9'�0� ""����..������� ��(������8�0�89����(0�89�������'9���0�89�� �9!����.� "�����9����

�� ������.�������0*������.��� �� ���������� "�"�� �����6+����'����������.��.������ �� ����� "!���'����������� �� ����"��

�� ������5��'��0��'������������������.��7����� ��� ��� �'�����������"� "!���"� ��������������������������5�44?G������'�

5� � ���A���� ����1����5���������-$$� ��� �� �����

� ���!����'� ���8�9'�'���0���"���!������..������� '���9� "��� '������95�������"���$�����'�������'��'����9���.���9� "�8�����������..������� �#����� ������.���9'�'����8.��� ���"�!!��� ��� ��.�� ���.������� �

5�: ���5 � ���@ �����&�� �*���

A.� $���.�"9��� ��������8��"����9� "'�������!����'� ���"�� � using System;

namespace MyHelloWorldApplication

{

class HelloWorld

{

static void Main(string[] args)

{

Console.WriteLine("Hello World");

}

}

}

Page 17: 3Programmer's Heaven C# School Book

��������������� ���������

1;

�������'���� �!��� ���'�������8�� �� H��H�68��.���G,�&���%..������� ��G�����.������!���9��������� ".���.�� "'����� csc MyFirstApplication.cs

���'������.�������.������� "������� ��8�!���*,�&���%..������� ��8�+� ������"��������� "'�����.���� ����������������������� ����.������9��.�� MyFirstApplication

���'���.�� ����������"�������� ������ ������� ����.��9� G���Q#��G"�������.����"���'���5�������"���$���

5 � �@ �����&�� �*���

�����,�����!�5�������"���$��� "�����&���E$�'E���7���P���'�����'����.� !���"������������5���������7���!������7�����.�� "������� ���%..������� !������.����������,����������"%..������� � ��� �����8�0�80���'� "����(A>�

Page 18: 3Programmer's Heaven C# School Book

��������������� ���������

1B

���'�����'������� �����"�!������"�!�����������'���"�..������� �

)��������"����� ����� ����� �*�� ������ �'���III+9��� ����� ����!���!������ �����������"� "'���� Console.WriteLine("Hello World"); in place of //TODO: Add code....

����(����.���������(��(� using System;

namespace MyHelloWorldApplication

{

class HelloWorld

{

[STAThread]

static void Main(string[] args)

{

Console.WriteLine("Hello World");

}

}

}

Page 19: 3Programmer's Heaven C# School Book

��������������� ���������

1=

$�'�����.���� "�8����������..������� 9�������0��E��������������0���� ���.������4&=����'����.� � �'�� ����� "�''������������"'����� � ���A �����.��� �(��9���'� "�''�������9����� ��� ����.�������

B������� ���� ��1����5���-$$� ��� ����C�

���!����� ��!���.������*�� ������P+�..���� �����������������.���������������������������!� ���� ������!������.�����������'���"�������0��������#��G!�����'��������� "�� �* ���.���,����������"%..������� +��� �

*����$����� ����� �

%$���.������.������������������� �!������"����� �����0� "�����������"����*��(�����������"'���"���0����������+� ��� ���"��������� ����� ���� ���.���*����9����%�������+�%��"�� ������'�'�����'��������� �����0���"� �.������9������.��.���!�� � ���.������.���� � ����� !�������������..� '�� ������������� ��0���!����9��������� ���&����'��(���#�0����*&�#+������������.��0����������� ����� ���� ����%��������� !����'�������� ����� ����!� ��� ��%��������������"���9�������������"�.����!�������.������ ���.���������!����J����!��" ����!��������'���0�����%���������� ����� � "� ��� ��%���������� ����� 9�� �������� �� ���0������!��������.����� ��9� ������ "�� ��!���.������'����"������ ��������!����'� �����*'���� UV0���(+���.����!,����������"%..������� ���.���� namespace MyHelloWorldApplication

{

...

}

����� ���.�������$A.��������..� ���������� -��������'������ ���.����� 0�� "�!!��� �!��"���������� ��.��!��..� ����������.��(���� -���� " ���.���� �� "��"�44���� ���.�������� ��� ����9��� �9�8��.��� 9"�������� "��� ����� ���.��������"G� ��� �� ���.���G�

Page 20: 3Programmer's Heaven C# School Book

��������������� ���������

1?

����� ��� �� ���.����� 0�"�!� �"��(����� namespace Parent

{

namespace Child

{

...

}

}

8 ���� ���D�#6��

���!����� ��!���.������'�� using System;

����� �(��'��"�0�������'������������� ���!����'� �G�����G ���.����@�"�� ����9'��� �'��������������"�!� �"� �������� ���.�����(�'�����0������������� ������� ���,�� �����"������A �.�� ��������0��������� �����'����������������� �����!��� ��" ���.���� ��� " ��� ��� ��� ��I����" ���.������ ��'������ ��"��'���� using System.Collections;

� ��"���������������"�!� �"� ��������� ���.���'�������0I� ��� �� ���.����!����� ���.����

8 ��������D�#6��

%���!�����.�������� ��� ������� ��������,�� *+�����"���"�� � ��!��������������������0� ���� �!"���*!���"+� "!� ���� *�����"+������ 0�.��!����"� ���"���� ��"�������������������� �����.��0������'���������� ��.��!���� ����"������ ������� �"�������� �����"�!� �"�� ����� ���(��'��"!����'�"0���� ����!����

8 ��/� �;<�/�� �

� ��� �8��� �'�"�!� �"���,�� *+�����"�!���.������� static void Main(string[] args)

��������� "��"�� ������!���,�� �����"� ������,�� �����"����� ���.�� ��!���.������9����9�����.�������������8������ !������!����� ��!,�� �����"� "����� ���'����������� ���� �!,�� �����"����,�� �����"�"��� ���"����������'���0������"0��������� #� �����)� ����*�#)+

Page 21: 3Programmer's Heaven C# School Book

��������������� ���������

1�

'��������(� �� ��07����!������������"���*'��������"�!� ���� �!�����������"9!���"� ".��.�����+���������"����"������"�������"�� ������� � ���� ����������*�� "��"+ ����!��������"9'����������������������!.�������������� 0�.��"����� '�����8����� ����.������!�������� "�� ����'�������������� A �� ������ �.�� ����������������������������������.��,�� *+�����"� ��.�������@��9����������8.��������"� ��!�'����,�� �����"����� ���.�� �������� E������44� "-�������������9��(� �������,�� ����'�����.����G,G� "�������� ��.�������

"� �� ������ ��������

A�� �8��� �.�� ����������"� ����� ������� � Console.WriteLine("Hello World");

����'������"�����#� �*+9�����������"�!����� ������"�!� �"� �������� ���.������������"��(����� �*� ����"� "��0��J����+���.��������� ".�� ���� ����� ���'� "�'� ��9��(������A07���A��� ��"�� �����9�����"��*�+�.������������������0�������0��*!���"+� "�����"�!�����%��90����*+�����"���"� ��!������"� �����"�� "��� ������������ ����"� "��0��J������� ���(*H+�#����9���������� �� ��*��(��9�44� "-���+� "'����������� *P+9��������"��������� ������ �����

��������

����� �������.���������G��8����8.��� �����"�9����� ���"0�������.����� "��� ��� ���"�"� ���!� ���8�����0����"������� ��8!������� ��������������-���� "�44������8�!����'� �"��0��������(*II� ������ �+����� ������ ���������� �� "'������� "�!����� �� // This is my main method of program

static void Main()

{

...

}

������..����������� �0���(�� ������9���'����0���(��� ���"0�������.�������������!���0���(�"������"0����E�����(*IW+� "� "'��������(E������(*WI+� static void Main()

{

/* These lines of text

will be ignored by the compiler */

...

Page 22: 3Programmer's Heaven C# School Book

��������������� ���������

1�

}

��� ���"���� �����(� "�!����� ������"G"����� ����� ����� �G����� ���������� ��������"����� ����� !����������� ".���������������� ������ �� "����'������.��������(*III+� /// These are documentation comments

��'���"�������� "������ ���� ������ �

��$������$ ���������������

�� :������8�����0��.���������"�� ������� �� ���� ���.�� ���.��������������������",�� *+'������"����� ��.� �� �������� ������� ���������"� "5��"���"�!!��� � �� �����.���*� ���9��09.���+����� ���"0�������.����0��'�� �����"���� ��9���!����'� ��

��������""��������� �!���,�� *+�����"����������� ��������� "�"� static void

Main ( )

{

...

}

�� :���A$G� ��"���������.������'������!��� �����!��������� ��� � �,�� *+�����" �� ������� 0������.��,�� *+�����"� ����.������ �� ���0�� "�����! ���.���9���� "�����"���"�!� �"0��.� � �� "���� ������0���(��UV �� % ���.����� ������������������ �!����'��� �.��������..� �� "�(*� ��(�-���+ �� ����� �(��'��"���"��� !������.����'�����������!�����"�!� ���� �!����* ���.���+

�����������0�������� ������.������� �� ����������.��!����� ��8��� ��P�� �90���(� ""����� ����� ���������� ���"0����

���.����� "�����"� ����� �� ��������"�0������ "� "���� "�0������!.������!�����"�����.���

�� 6 ���� ��������� ��� ���.�����.��� ���:���� '����.������'������������ ��� ����"0�� � ���.���

�� ��� ���� "���������,�� ,����"�!.��������(�G��� �MN���G�.������������.��!���������"��'����,�� �����"��� �

static void Main()

{

...

}

Page 23: 3Programmer's Heaven C# School Book

��������������� ���������

1/

-����� ������� 3��1����5���-$$� ��� ��

<.� ��� �'9'������� ���������������'���"�..������� ������������'����'���"'�� ����8�����"�#��K �'��(������� ��������������'���"����������������� �����!������.������'����(����������� ���� "'���������� ���I��� ���9��(�G�����&���CG'�� ���� ���"G&���CG�� ���#��K�������"�!���� static void Main(string[] args)

{

Console.Write("Please enter your name: ");

string name = Console.ReadLine();

Console.WriteLine

("Hello {0}, Good Luck in C#", name);

}

����������� ������������ ���������������������������������������������������������� �����������������������

������������������������������������������������������������ �

2 ����� ��������� ������� 3��1����5���-$$� ��� ��

� ���!����� �9'�������"� ����������"9�����*+9�!����� ���������������������������#� �*+�����"9"����"� ���.������.������90��"�� ����� ������� ��!���.�� �� ������� �� ����� ���� � ������ "�� �9'�"������"������0���!�����.���� �� "�����"��G ���G���� 9'����(��� ��!� .��!�������������������)��"#� �*+�����"�!����� ������� "����"��������� ���G ���G�����0�����������0�����.�������"��� ������!������ �"������.�������"��� �����8������ �!.�������5����0���� ���""�!!��� ���.��!"���"�.� "� �� �����"���E��.�9����9� ������0���� ����� �����'������� ������0���� �������� �*����+�!�������������)��"#� �*+�����"�!����� ������*�� ������������#� �*++���"��� ��!� .������ ������� ����� "�'�������� ���� .������ �"���9'����'�����"� ������ ������0��G ���G� ����������� �

��������������������� ���!��������"#�����������������������������������������������������

� �������"�� �9'�.�� ��"��� ������� 0����� �� �19��� �'������������ ���8��� ���������#� �*+�����"�!����� �����������'���"����0������� .��������U2V������'����� ����� ����"���� ��������0��G ���G����"0�'����� '�� ��������#� �*+�����"������"� Console.WriteLine

("Hello {0}, Good Luck in C#", name);

��� ������.����!� "��0������� .��������9U V9����.������'������* 4 +�������0��!����'� ������� �������� ��"�������"'���"��0��J������� ���(� "����.����������.�����"0���������� ��9� ���

Page 24: 3Programmer's Heaven C# School Book

��������������� ���������

;2

���'�� ������.����!� "U2V9����.������'���*24 +��9����9��� ������0��*G ���G+!����'� ������� �������� E����9����#)'������"���� Console.WriteLine

("Hello Faraz, Good Luck in C#");

�!���������!G ���G�&���C���� E�����%���� �������9���� ���0�'����� �� Console.WriteLine

("Hello " + name + ", Good Luck in C#");

������ �����0������� .�������������'��� ���� ���*�""+������ �����������!����������*���!����..���������������G.�� �!*+!� ���� '���������� "���������-���G������������ � �������"+ ��� '����.���� "�� ���.������������.��'���0�� Please enter your name: Faraz

Hello Faraz, Good Luck in C#

Page 25: 3Programmer's Heaven C# School Book

��������������� ���������

;

��������������������������� �

������"����

��"��'�'������� ����� �����!� "��� ����!�����'����8.�������"�����.�� ��9�� ������0��9�.������9!��'�� ���������� ���(��!�����9���.� ���������� "��'����������

7�� ��2����8#$������� � ����$$ ������8&�;�����8#$��&#����<�

���������'�(� "�!"�����.�� ���

�� 5������.�*��.�����"�����.�9������ "� �������� + �� )�!��� ����.�*�07���9"�������+

5������.����.��"�������"0�.�� �� �8�����.�'����)�!��� ����.����.��"�������"0�.�� �� ���������!��� ��*�� "��+���.�����"�����.����"�!� �"� ����� ���������0������ ������� "��9'�����8.�����"�����.������.����������"�0��� ������.�� ���.�����"�����.�� %'��'� ���!������9��.�����"�����.�� �$�����.��� ��� ����������..�"����.�� �������� ��.������*���+� "�#�*����� #� ������.���!������ +��� ��9������.�����"�����.�� �����������.� "� ��$����.�������.�����"�����.�� ������ ����.� �$����.� ��C�� 0��� �����.���

���������8#$���

0��� @��� ,���� ��� � �����!���2E1==

0��� �@��� ��� �"0���!���E 1��� 1�

���� � � ? 1 )� ��!���E;19�?���;19�?�

����� <� � ? 1 < �� �"9�� ��!���2��?=9=;=

� �*"�!����+ � �;1 B )� ��!���E19 B�9B�;9?B���19 B�9B�;9?B�

�� � <� �;1 B < �� �"9�� ��!���2��B91/B9/?�91/=

�� � � �?B � )� ��!���E/911;9;�192;?9�=B9��=9�2���/911;9;�192;?9�=B9��=9�2�

Page 26: 3Programmer's Heaven C# School Book

��������������� ���������

;1

��� � <� �?B � < �� �"9�� ��!���2�� �9BB?9�BB92�;9�2/9== 9? =

���� ���" ���8#$���

!���� �� ��� B )� ��!���X �=Y 2EB=��X;�BY 2;�'����"����.������ �)�J��������!!�8G!G��G&G

"��0��*"�!����+

���0�� � )� ��!���X=�2Y 2E;1B��X ��Y 2;2�'��� =E ?"�����������

�� ���8#$���0��� @����� �� ��� ������������!���

���� ���� 1 �� ��� � �� ���< ���"����������� ����"� � ���J������� ���(����G�G

"������ ������� 1 )� ��!��� �2Y 2E1�����/Y 21�'���1�E1/"����.������ �)�J��������!!�8G�G��G,G

��.�����"�����.������.��� ��"� �� ������� �(��'��"9������!����0�����(��'��"� ��*>��'��"������'��""�!� �"0������ ������ "�� ��0���"��"� ��!���+����'���� ��� �������� ������ ��.�����"�����.�� ��9�������(��'��"� ���������.�� ��0�����.�����"�����.��������������������.�� "�������"� ������(9'�������"�!� �"��.�����!��� ��"��.��������"�� �������.�%���(��"������������������������ �!���� !������*&�&A+!���� ����� �����!�������..����"0����.������� "���C��"������ �"��������.��������%���.�� ���!������������0�������.���������� �����)�!��� ����.������������"�� �������������0��!���������."� ��������*"��� �����8������ �!.������+�������0������������������!�� � E��!��� ��""���� ���."��� �����8������ �!.������� "����� ����.�����A.����� �������

@�� ������

���� �����8������ �!�.������9"�������.�����������"� �������%�����0������ ������� ���������������� ���"� ��.�����������.��!"������9���������0�����������"'������"�����.�� "�������� ��9�����0�����"������"�� <data type> <variable>;

����9 int i;

����0����� �'���������� �����!B0���� ������������� � �������.������9'����'���0���!����"��� �������!.������0�����"� ��!���G�G�:���� � ������C���������0������"��������*� ���!��+� "�� ���"������I� ������C������.�������0���!��������.�� �� ��������� �9����9

Page 27: 3Programmer's Heaven C# School Book

��������������� ���������

;;

bool isReady = true;

float percentage = 87.88, average = 43.9;

char digit = '7';

� ��*��(��������"�� �� �����+9������"�����������0��0�!����� ������%��9����������� ��.��!H��!� ���%�� �� �H� ��'������H����������0��*�����0��"�!� �"� ������"+���0�� ������C�"0�!���0�� ���"H����!����'� �.������'� G����.���� static void Main()

{

int age;

// age = 18;

Console.WriteLine(age); // error

}

@��9�!���� E����� ����1 "�� �9���.������'������.������"�� ����� "�!���������������������0������������.��!��� �����9����9������!.���������"�����.��� � ��0�����"� �������.������*�����.���0��+"�����.��:���� G������ ����������� @����� "�����.���(�'���"��"�� �I�44�

��������@�� ��������&#�����

�� �� ���������0��'��������9� ��"�!� �"9�� ��0���� ��"0����.��������� �� ������0�����"������"�� ������ �(��'��"9��(�� const double PI = 3.142;

�� �� ������0�����0�� ������C�"��������"������"������ ��8�������'����� const int MARKS;

����� �� ��� ��������.����������'�� ��� ��� �� ������0���

*�� �����3��� ���.��3�� ������������ ��

,�����!�������� ������$������ *!���������� ��'�����+!�������0��� "�����$������ *!���������� �..�����+!�������"�6���'��"�!������!���'��"� ��� ����!0��������0��� "�����"����"����'������.�����������&���8��.��9�����0�� ���!����'� ������ ������ ����"0�� salary totalSalary

myMathsMarks isPaid

������.���� ����!�����"!����'� ������$������ ���

Page 28: 3Programmer's Heaven C# School Book

��������������� ���������

;B

GetTotal() Start()

WriteLine() LastIndexOf()

%���������� ���� "�������!����'����� �� ��� 9���������������� "�"��������������!����'����� �� ��� �,�����!� ��� ����..����� ����� ������ 9��(��� ��,��(!��� ����������0���%��9�� ����� "������Z� �"� ��!���� ��� �������"�

�$������� �����

-� � ��� ���$�������

������������ �����������.�������������'�"� ��� �$����� �����.��� 4 %""E ��0�����W ,����.��I ����"�[ )���� "������"���44 � ����� �0� EE ������� �0� ���.������0���'�������.������� using System;

namespace CSharpSchool

{

class ArithmeticOperators

{

// The program shows the use of arithmetic operators

// + - * / % ++ --

static void Main()

{

// result of addition, subtraction,

// multiplication and modulus operator

int sum=0, difference=0, product=0, modulo=0;

float quotient=0; // result of division

int num1 = 10, num2 = 2; // operand variables

sum = num1 + num2;

difference = num1 - num2;

product = num1 * num2;

Page 29: 3Programmer's Heaven C# School Book

��������������� ���������

;=

quotient = num1 / num2;

// remainder of 3/2

modulo = 3 % num2;

Console.WriteLine("num1 = {0}, num2 = {1}", num1, num2);

Console.WriteLine();

Console.WriteLine ("Sum of {0} and {1} is {2}", num1, num2, sum);

Console.WriteLine("Difference of {0} and {1} is {2}", num1, num2, difference);

Console.WriteLine("Product of {0} and {1} is {2}", num1, num2, product);

Console.WriteLine("Quotient of {0} and {1} is {2}", num1, num2, quotient);

Console.WriteLine();

Console.WriteLine("Remainder when 3 is divided by {0} is {1}", num2, modulo);

num1++; // increment num1 by 1

num2--; // decrement num2 by 1

Console.WriteLine("num1 = {0}, num2 = {1}", num1, num2);

}

}

}

%����������.�������0����J������.��9�'���"��(���"�������� ��.������� ����� ���������#� �*+�����"9'�������"!�����E.���!���U� �V��� "��������.����� �!�����0��� ������ �� Console.WriteLine("Sum of {0} and {1} is {2}", num1, num2, sum);

����9U2V9U V� "U1V'���0���.����"0����������!��� �� 9 ��1� "�������0���� U�V9�.���!������*�4 +�������0���!���"��0��J����'�����.������'�� .�� ��"������� ������ ��9U2V'���0���.����"0����!���� �9U V'���0���.����"0������� "�����0��� "�� ��� % �����.�� ��� �������� �� 44����������� � ��� num1 = num1 + 1;

A�� num1 += 1;

*��'��������"����.��� �!��� "������ �������+

Page 30: 3Programmer's Heaven C# School Book

��������������� ���������

;?

"��. 0����"��. 0����� ��

@������44� "EL�.�������� 0���"�.��!�8��.��!�8�.�������� .��!�8!���� num1 = 3;

num2 = ++num1; // num1 = 4, num2 = 4

������.����'���!���� ����� � �� 0� � "��� '������ ���� ��1������� .��!�8!���� num2 = num1++; // num1 = 4, num2 = 3

������.����'���!������ �� �� ��1� "��� � ����� � �� 0� �

-�� ��������$�������

%�� �� ��.�����������"����� ������������0�������� ��� �� ��.������� ������ �$����� �����.��� \ ���.����� �� �4\ %""�������� �� �E\ ��0����������� �� �W\ ,����.����������� �� �I\ ������ ��� �� �[\ ,�"������ �� �����J���*\+�.���������"����� ��������� �07����#�(�'������� bool isPaid = false;

��� ��������G!���G���������"�����0���!@����� ��.�������!��� "� "������� "�"��!����J������ ��������� �� ��.���������0����.���0��9�����'��������.����'������.��� �0����� ��8������������������ ����"!����.��� ����� 9����9���� ����� "����������� ������0���!��.�"��0����������0���!��.�� �9'� ��"���..��� � ��������� double doubleValue = 4.67;

// intValue will be equal to 4

int intValue = (int) doubleValue;

A!�����9'�� ���� ���������'���"� ����!������!.������ P� �������0���9'�� ��������B�!�������� ��B�?����������9������� ���������� ��� ��������

Page 31: 3Programmer's Heaven C# School Book

��������������� ���������

;�

int intValue = 32800;

short shortValue = (short) intValue;

// shortValue would be equal to -32736

5����0���!��.������� � ����(�������� �� �!���E;1�?���;1�?�9��������0����� ����� ;1�22������5������� ������5�������(������ ?0��*�������� ���! ?0��+�!���� �����;1�229'����������������E;1�;?*� ��0�� ?9'������.��� ���������;1�?�� � � �9 �'��.��� �E;1�?�+��!������������ ���.���0����.���(�� bool isPaid = false;

int intValue = (int) isPaid;

��'� G�������.���"� "������.����'����� ������� ��8������

9���� �����$�������

)������ ���.�����������"!�����.���� .��.��� �� "���� �������� ������� ������� ���.������� ������ �$����� �����.��� \\ 6J����������(O\ < E�J����������(] 3��������� ^ #���� ]\ 3��������� ���J�����^\ #���� ���J�����)������ ���.��������'�������� �@����� ������ �P������������!����&���8��.���!'������'������0�� int num1 = 5, num2 = 6;

��� � num1 == num2 // false

num1 != num2 // true

num1 > num2 // false

num1 < num2 // true

num1 <= num2 // true

num1 >= num2 // false

Page 32: 3Programmer's Heaven C# School Book

��������������� ���������

;�

A �����.���0��"�����.��� 0����.���"����� ����"�����.����0���'���� � �9��!������� int i = 1;

bool b = true;

����� �����.����� "0!���J������*�\\0+����� ���"��'��������� �� ��8������ �� �������7 �6 ����$������������.�����������"!���������� "0��'������������ ������ �������� "0��'���.������� ������ �$����� �����.��� D @��'��%$�_ @��'��A)` @��'��RA)O @��'��$A�DD S#������T��S�����������T%$�__ S#������T��S�����������TA)����.������D9_� "`�����������"� ����.�������� �.�����������$A��.���������"�� ������@����� ��0��'���8.���� ��(�� bool b = false;

bool bb = !b;

// bb would be true

#������A.������DD� "__�����"�����0� ����.���� ��(� int i=6, j=12;

bool firstVar = i>3 && j<10;

// firstVar would be false

bool secondVar = i>3 || j<10;

// secondVar would be true

� ���!������.���� ������������'��������� ����� ���!0�������� "���� ���� "���������� ������ ������ "���.���� ������������'��������� �����!� ��!����� "���� ���� "���������� �����:���� 9�!�����9��0���DD� "__� � ��������� ���(�� bool firstVar = (i>3 && j<10) || (i<7 && j>10) // firstVar would be true

� ����0��������� �'���".��� ����������.����� "���� ���8.���� � "������"� ���0�������

Page 33: 3Programmer's Heaven C# School Book

��������������� ���������

;/

:���� ��D� "_�.������� .�����!DD� "__0��!�����0� � ��� "���� ���8.���� 9DD� "__��������!!���� �0�����������H�������������������� H�&���8��.��9�!� ����8.���� *�];DD7^ 2+9�];����������!���9������ "�8.���� 7^ 2'� G�0�����(�"� "!���'���0������ �"*'�� �� �%$�9�!� ��!���.������.� ��.��� "�!���9���'�����.������ '��������� !���+��� ��9� �����"0���������!��'�� �� ���� �� ��8.���� '���DD� "__�.����������D� "_�.������"� G�"��������������������� � ""��8��������������.���� 0�!�������� � ����������

�� ����$�������

�����������������.������.��� �� ���%����"����.��� �!��������� 0���'� �$����� �����.��� ^^ #�!���!�0��'���.������]] )������!�0��'���.������� ,��0������!���07���MN � "�8�.��������"� ������ "��������� *+ ����.������Q� ��� ����.������

�$������"���������

%���.��������� ��������"�J��������������� ��.��!H�.������.����"� ��H� ���&���8��.��� int i = 2 + 3 * 6;

// i would be 20 not 30

;'���0������.���"0�?!������ ��������'���0��""�"��1�����0�������������.������� �.������W��.����"� ����������""���� �.������4�&������.������0���!�.������.����"� ��9�� ���,��$������$��!����'��("����� ����� �

��6�������-���� � ����&����������

8 �� .������������������

�� "���� ����(� �����'��0�� ��������.���� ��� ������ � ��� ����������!�����������!������0���� �����"�����.����"��� "���� �������� �� ���!����!����!������������ ��������������!��������� ��� if(Boolean expression)

Statement or block of statements

else

Statement or block of statements

Page 34: 3Programmer's Heaven C# School Book

��������������� ���������

B2

������������0�����.��� ���%��.�����8��.���� if(i==5)

Console.WriteLine("Thank God, I finally became 5.");

� ����0����8��.��9����� ��������'���0�.�� ��"� ���!����8.���� �\\=����������������!���'���"��(�����(��������� '�� ����� "���� "�� ����������������9��� ����� ����������� if(i==5)

Console.WriteLine ("Thank God, I finally became 5.");

else

Console.WriteLine("Missed...When will I become 5?");

A �����!��������'���0�.�� ��"�!���J�����=�� � ���������*'�� �� ��=+9������ "�����'���0�.�� ��"��!���'� ������0���(�!������ �*������� � ������� �+� "���!�����9������ � ��������0���(� UV0���(���� � if(i==5)

{

j = i*2;

Console.WriteLine("Thank God, I finally became 5.");

}

else

{

j = i/2;

Console.WriteLine("Missed...When will I become 5?");

}

�'���"��'��������� "����UV0���(����� ������������� �!����'� ��!� "������ �!���� �������� ��������� ����� ��������"�0������ ".���� ��� �0�����������'���� ������!��� �����������.��!�!� "��������� �� :���� ��������!�!������!��!�������� "���� � �� if(i==5) // line 1

{

Console.WriteLine("Thank God, I finally became 5.");

}

else if(i==6) // line 5

{

Console.WriteLine("Ok, 6 is close to 5.");

}

Page 35: 3Programmer's Heaven C# School Book

��������������� ���������

B

else // line 9

{

Console.WriteLine("Missed...When will I become 5 or be close to 5?");

}

��������!*�\\?+��8�����"� ���!���!����� "���� �\\=�!���9� "������� �/'���0��8�����"� ���!������ "�� "���� �\\?*�� �=+�8������ "!���*�����90������!���� "��� "�� "���� !���+����.�� ������������� �/�������"���!� �� �=� �� ���!����������������� �9����� ����� "��������!������������ �* ��� �+9��(�� if(i>5) // line 1

{

if(i==6) // line 3

{

Console.WriteLine("Ok, 6 is close to 5.");

}

else // line 7

{

Console.WriteLine("Oops! I'm older than 5 but not 6!");

}

Console.WriteLine("Thank God, I finally became older than 5.");

}

else // line 13

{

Console.WriteLine("Missed...When will I become 5 or close to 5?");

}

������� �� ����������������"���!� �� �;'�������� �� � ;0��� ����!� �� � �&� ����9"� ���*�I�44.����������.�������+�����!������ ��8.���� ��@����� �8.���� � " ��� �������������G� �������'����� int flag = 0;

if(flag = 1)

{

// do something...

}

� ���"9����� ��������� int flag = 0;

if(flag == 1) // note ==

Page 36: 3Programmer's Heaven C# School Book

��������������� ���������

B1

{

// do something...

}

��9 bool flag = false;

if(flag = true) // Boolean expression

{

// do something...

}

���(��������"� ��� !��� � ������!� ����.��8���0� ���� �!�!����������

�� ��0���!�� �UV0���(��'���������!� "���� �� � "� ����� ����� � ������"���� �� �����"�0�������!�������� �5�������"���$������������

�"���������..�����"� �9����"����'���"�� "� ����� !������A����'��9�����������(������!���������!�

���� ���������� "���!����'����0����'����"��� ��

8 ���6 �� ������������������

�!��� ��"��.��!���������!.���!������(9'�����������.��� �� ��7��!����������� �������������!���'���������������� ��� switch(integral or string expression)

{

case constant-expression:

statements

breaking or jump statement

// some other case blocks

...

default:

statements

breaking or jump statement

}

����(�����������'������������� �� ��������!�������!������ ��#��G���(����'���� �8��.��� using System;

Page 37: 3Programmer's Heaven C# School Book

��������������� ���������

B;

// To execute the program write "SwitchCaseExample 2" or

// any other number at command line,

// if the name of .exe file is "SwitchCaseExample.exe"

namespace CSharpSchool

{

class SwitchCaseExample

{

// Demonstrates the use of switch...case statement along with

// the use of command line argument

static void Main(string [] userInput)

{

int input = int.Parse(userInput[0]);

// convert the string input to integer.

// Will throw a run-time exception if there is no input at run-time or if

// the input is not castable to integer.

switch(input) // what is input?

{

case 1: // if it is 1

Console.WriteLine("You typed 1 (one) as the first command line argument");

break; // get out of switch block

case 2: // if it is 2

Console.WriteLine("You typed 2 (two) as the first command line argument");

break; // get out of switch block

case 3: // if it is 3

Console.WriteLine("You typed 3 (three) as the first command line argument");

break; // get out of switch block

default: // if it is not any of the above

Console.WriteLine("You typed a number other than 1, 2 and 3");

break; // get out of switch block

}

}

}

}

���.���������0��..���"'���� � ���������� "�� ������� ��&���9���.������.������*���������� "�� ���� 5�������"���$��+���..��'���"�� �8�'��� ���H�'�������68��.����8�H9'�'���"�� ������������ "�� ���(����� C:>SwitchCaseExample 2

You typed 2 (two) as command line argument

A��

Page 38: 3Programmer's Heaven C# School Book

��������������� ���������

BB

C:>SwitchCaseExample 34

You typed a number other than 1, 2 and 3

�!���"�" ��� ���� ������ "�� ������� �������� � E� ����������� �9���.������'�������� �8��.��� � C:>SwitchCaseExample

Unhandled Exception: System.IndexOutOfRangeException:

Index was outside the bounds of the array.

at CSharpSchool.SwicthCaseExample.Main(String[] userInput) in

c:\visual studio projects\SwitchCaseExample\

SwitchCaseExample.cs :line 9

#��G������ ��� ��'��(� ��&���9'��� �����"���!�������� "�� ������� �*���� .��M2N+� ��� � ������0��� .���&���� ����� 9'���"������������*+�����"�!���� �"�����.����������"��(����� �� "����� ����J������ �� ������������ �8��.��� �!���� G��$�8�'�����(�"���������!� .�������0���� ��'���������� �� switch(input)

{

...

}

#����9� ���0���!���������!� .��9'����(.���!������� � "����.��������������� ��A ��������.���!�������� �� "9'����(��'������0���(������ �0�!�����������!� ��������*�����"�!����+0���(� case 3: // if it is 3

Console.WriteLine("You typed 3 (three) as first command line argument");

break; // get out of switch block

�!������.���!������(!���*� .��� � ��! 91� ";+9��������� �� "��"�!�����8������ default:

// if it is not any of the above

Console.WriteLine ("You typed a number other than 1, 2 and 3");

break; // get out of switch block

�������������.���� �.�� ��������0��'�� �� ����'���������������� �� ���

�� :���� ��������� �����*� �������� +����� �� �'���������� � �� ����8.���� !����'� �������0��� �� ����������������������0���!�������

Page 39: 3Programmer's Heaven C# School Book

��������������� ���������

B=

case i: // incorrect, syntax error

�� %���� ����"�!��������������� �� " ���������� P �� :���� �������.�������� �� "��� ������� ""�!���������� ��

case "Pakistan":

continent = "Asia";

Console.WriteLine("Pakistan is an Asian Country");

break;

default:

continent = "Un-recognized";

Console.WriteLine

("Un-recognized country discovered");

break;

�� ���� "�!������� ""�!���������� �����(�"'���0���(*������+������ ����"� G���UV

0���(�������(���0���(� '�����������'�������"�� �� �� ��"�� ������'!���E����������9����� G�����������"�!����'������0���(������ �*������ �

-������I�44+�������.����'���"������ "���.��� �0���������!!���E�������� ���'���������������� ��

�� ���0���(������ ���� !������8������ �� ��������!�������� �0���(� �� ������� �� "��"�!����'���0��8�����"�!� "� ���!�������������(!���� �� ��� �� ��������.����"�!���������� "�!'���������������� ��:���� ��� .�������"�!����

0���(0�!������!��������� 0��'�� ���P"�!����'���'��(�����������"���!��.����� ���'����9��(� �"�!����������0���(��� �� ��� ��� "������������� "�"�A!�����9����� G������������ � �"�!����0���(� �� ���'�����������

�$��������

#��.�����"!���������� .��.��9����9"�� ����(�����.������*������� ��������� ���� �� "���� ����+

8 ��.���$�

����������� ��.��!���.� ������!�����.����0������������!�!�����.��8������������� -���� "�I�44� "�� for(assignment; condition; increment/decrement)

statements or block of statements

enclosed in {} brackets

#��K���!�����.����'���'�������� �����!��� �� 2������� ����

Page 40: 3Programmer's Heaven C# School Book

��������������� ���������

B?

for(int i=1; i<=10; i++)

{

Console.WriteLine("In the loop, the value of i is {0}.", i);

}

%��������9���� ����������0����� ������C�"'������������! ���������� �� ���!�����.����8�����"'��������� "���� *�^\ 2+����� ��������������!��� ����� ��"*�44+0� ��������������.�����

&��� �$������$ ���������� ��.���$�

%������������� �� !��*+9��� �� �9�� "���� � "� ����� �I"������ �����.��� ���:���� ��� ����0� ���� �!����� "��� "���"� ������� �� ����������'���"��(�'���������"� "G� "�!� ������ !� ������.G*����.����'��� ����� "� ������ �����0���(� ������� �� ��� ����"� �"����0�"��!������.+�@��9��������������..������..��.������������ � for(; ;)

for( ; i<10; i++)

for(int i=3; ; i--)

for( ; i>5; )

�!���"� G������UV0���(��9��������� �����"����!����'� �!��*+'���0�������"������������ ������ ������8��.��0���'��"� ����������� ����� �0���� for(int i=1; i<=10; i++)

Console.WriteLine("In the loop, value of i is {0}.", i);

�'������� ������� "���������'����UV0���(��� ".��.��� "� ����� � �!���"������������0��� !��*+G��� �� �9����!�*��.�+'���� ������ �"�������.� "��'���"���!���������.0�"������ ���*� ��(������.���� ����� �!�44+��� ���!���'����� for(int i=1; i<=10; i++)

{

Console.WriteLine("In the loop, value of i is {0}.", i);

}

i++; // line 1

������.����'������.��� ���� � ����H��� � "������"�"� ��!���H� :���� ��0���(� "�� �� ��� !�����.��� ���������.����� ����� ������8������ .����0���(����� ���������.� "��� !������8������ ���.�� �7������"����!�����.�

Page 41: 3Programmer's Heaven C# School Book

��������������� ���������

B�

for(int i=1; i<=10; i++)

{

if(i>5)

{

break;

}

Console.WriteLine("In the loop, value of i is {0}.", i);

}

������.'�������� ���� �����������!�������������� =��!��������� ����.��� ��!���0���(90���(���0�� ����"� "������� "���� 9�����'������� �!����'� �0���('���0������ ������0��� "������.����'����� ������'�� � �*� -���9��G�� ��8�����+� for(int i=3; i<10; i++)

{

break; // warning, Console.WriteLine (i); is unreachable code

Console.WriteLine(i);

}

�� �� ���� ����������� � �.����!�������� ��������� � "������� �8��������� � for(int i=1; i<=10; i++)

{

if(i==5)

{

continue;

}

Console.WriteLine("In the loop, value of i is {0}.", i);

}

�� ���������#� �'���0��8�����"!�������������� �8��.�'�� ���������!�0�����=������.�����.���!����0�����"��� In the loop, value of i is 1.

In the loop, value of i is 2.

In the loop, value of i is 3.

In the loop, value of i is 4.

In the loop, value of i is 6.

In the loop, value of i is 7.

In the loop, value of i is 8.

In the loop, value of i is 9.

Page 42: 3Programmer's Heaven C# School Book

��������������� ���������

B�

In the loop, value of i is 10.

8 �����6 ����$�

����� �������������!�"����'�������.� do

Statement or block of statements

while(boolean expression);

��������� �� "��"�'����8��������!�������� "��� ����� "���� �����(�"�������.'����� �� ��'��������� "���� ����� ����� ���.������!��.�� �� ����� ����� �� 2������� ����� ����"����'�������.�� int i=1;

do

{

Console.WriteLine("In the loop, value of i is {0}.", i);

i++;

} while(i<=10);

������.���� �.�� ���������

�� ��������� �� �"����'����*+���.��'���8������������ ��� �� �������������� P�!������'���������� ��

6 ����$�

���'�������.������������"����'�������.9�8��.�����������(����� "���� 0�!���� ���� ����!����������� *�8������ �!��"�� �"����0�"��!������.+������ ����!����!�'�������.�� while(Boolean expression)

statements or block of statements

A��.��������.�� ����� ����� �� 2������� ����� �'����'���0�� int i=1;

while(i<=10)

{

Console.WriteLine("In the loop, value of i is {0}.", i);

i++;

}

Page 43: 3Programmer's Heaven C# School Book

��������������� ���������

B/

-���#�� ����� �

-���#�2������� ��

% %��������������� �!������!�������"�����.������ ������9�������������!��� ����.��6��������� ���� �07���� "�� ������"!������������%��������%�������"������"�� <data type> [] <identifier> = new <data type>[<size of array>];

#��K"�!� �� ������!��.�� ������" 2� ������ int [] integers = new int[10];

����C��!� ������!�8�"� "���0�"�!� �"0�!����� ������'����9����� �������0����"�!� �����C��!��������� int size = 10;

int [] integers = new int[size];

:���� �.��� ����"�"��������� � "� ������C���� � �.�������.� int [] integers;

integers = new int[10];

������.��0����"�!� �������� �����������'������"0�� ���� ������� �����0���(��� "�.����� �� "���"��������'���������� int [] integers = {1, 2, 3, 4, 5};

���'���������� ������!�C�=9'���������������'���0� 919;9B� "=�

-����� ���� ��3����������� ���������#�

��������������� � %����9'������� "�8� ��.������M� �� "�8N���"����0�.�� �� � ���� "�����'����.���������� "�8�����'�'�����������G��.���� ��� �������� "�8������ ������!���2����!� ������� ��� =����� �9���!�������� �'���"0���� "�829������ "��� "�8 � "������*!�!��+��� "�8B����!����'� ��� �"��� �������'���������;�"����� ��!� ������ int [] intArray = {5, 10, 15, 20};

int j = intArray[2];

#��G��(��.������������� � ������������

Page 44: 3Programmer's Heaven C# School Book

��������������� ���������

=2

// demonstrates the use of arrays in C#

static void Main()

{

// declaring and initializing an array of type integer

int [] integers = {3, 7, 2, 14, 65};

// iterating through the array and printing each element

for(int i=0; i<5; i++)

{

Console.WriteLine(integers[i]);

}

}

����'���"���!�����.������������������������� "����� ���������#� �*+�����"��.�� ������ "���"�������� ��!���������$�����'���� "�8� ��.������MN���"� ����0���.�������J������.��� "�!!���� �90��'���"�����"E��"�����C��!��������� ���!�����.�%'��� ��� �"�������9������ �������!��� ����.�� "�����0E����!���������%��������������������!��!��.��.������ "�����"������ 0��..���"��� �� �� ���!� ���������'�"�!� �����.������������������(�������0� ���� �!������� "����������"� ����� A07���A��� ��"�� ���������.���������� ��8�� �����9'������� ����������.������ � E��0��������"� ��!�'������������"��������������������� � ����� �� ��8����'���"���.��.������ "������ ������� ���� �������%�������������!�����"E� ��.��.���� ���"#� ��������� 0���"��!� "����� ���9���C�9�!� �����.����������������<� ����#� ���.��.����9���!�����.� ����0���.�������� 0�'����� �� for(int i=0; i<integers.Length; i++)

{

Console.WriteLine(integers[i]);

}

�������� �!���.� ����������!��8�0��� "�� 0��..���"��� ������!� ��C�� "�!� �"���E��.��$�''��� � "���� "�������"����.��� �!,�� *+�,�� �������"������"�� static void Main(string [] args)

�������� "�� ������� �����'�.�'�� �8����� ����.���������������0��� ���.�������������� ������!��.���� ��"� ��!��"0���������� �������

.���� ��$�

������� �������.��!���.�����������.��� "��!������������������������ "��������� ��������!���������.����0������������!�!���������.��

Page 45: 3Programmer's Heaven C# School Book

��������������� ���������

=

foreach(<type of elements in collection> <identifier> in <array or collection>)

<statements or block of statements>

#��G �'��(����.������.������������������������������'����!���������.� // demonstrates the use of arrays in C#

static void Main()

{

// declaring and initializing an array of type integer

int [] integers = {3, 7, 2, 14, 65};

// iterating through the array and printing each element

foreach(int i in integers)

{

Console.WriteLine(i);

}

}

���.��� "�������"�0��9� G���Q� ��������� �� foreach(int i in integers)

��.���!��"�����.��!����� �� ������������ *� �� ������+���"������"��������0��*�+��0���"�����"���� "���"���������!��������G� �����G� ������������ � ��.���� �.�� ��� ��������

�� ��������0����"�����"���� "���"�������� ��!������ ������������ *�� ����0����8��.��+����"� ���:���� G���� ���������� �� ������������������������ ����!������'���� ������'�������������������������������������� � " ������� ������� �� ��!����!���'����.��!������'��(� ������������� ������ "���"�������� �9�������"���!�����.�

�� !�������� 0���"�������������������������������� �@����������� 9'���� � ����9�������� ���!���������.���� �����6 �����0��� ���!����*-��������������.�� �� "��E���"��� ��'����.���������� "����0� ������ "� ���!���+

�� ������ ������������������ �!���������*��.���� ��6 �����0��� ���!���� "����� ���������� ����� �.��.����+����!����'� ���"��8��.��"��� ��������� ".�� ����������������� ������ ��

static void Main()

{

string name = "Faraz Rasheed";

foreach(char ch in name)

Page 46: 3Programmer's Heaven C# School Book

��������������� ���������

=1

{

Console.WriteLine(ch);

}

}

���'���.�� ���������������!��� ���� ��.������� ��

Page 47: 3Programmer's Heaven C# School Book

��������������� ���������

=;

����������������������

������"����

��"��'�'�������A07���A��� ��"��������� �*AA�+� �����'�������'������� � �����9�07���9� "�����0������� '�'����������� �������9������"�!���9.��.�����9�����"�������"� �� "����������"�

����$��.���������

%������.��� �0�������"����"��"�!� �� �'"�����.��%�������� ��� � ����0� ���� �!� ��.�����""���*!���"�����0�������0��+9�.������ ������ 0�.��!����"� "���*�����"+� "��������"���*.��.�����+�&���8��.��9�������������� �� �������� ���.����!�$��&����'��(���#�0����*&�#+��������� ��� � ������!���������*"���+� ".����"�"�!!��� ��.������ *�����"+������ 0��..���"����"�����(���#�'�����*+9����*+9��0��� �*+9��������������.��.�������(�#� ���*��"��!� "����� ����!������ �+� %���� ���"������"�� ����(��'��"���� "�����0������ ����"� .��� ���� class MyClass

{

// fields, operations and properties go here

}

'����,�������� ����!����� �'"�����.�����'����"�!� � ������

��������

%�� ��� �"�0���9������ �0�������"���% �07��������� ����������C���� ��� �� ��0����� �����"��.���!��"0��������% �07����������"� ����������� ����(��'��"G �'G� "���!��� ��"0�� �"� ��!��������"�H��!��� ��H� MyClass myObjectReference = new MyClass();

� ����� ��0���9'���"�� �07����!��.�,����'�������!��� ��"0�� �"� ��!�����A07���)�!��� ��� ���"�!!��� ��0��'�� ����� "��.�����"�����.�������07��������!��� ����.�*.��"0���!��� ��+'������.�����"�����.�����������.�*.��"0���(� ����.�+�%��9�07������������"��������.'������.�����"�����.��������"� ���(�

Page 48: 3Programmer's Heaven C# School Book

��������������� ���������

=B

� ����

&���"������"����� ��� �"� �������&���"���0���.�����"�����.�9�07����!�����������9� �������� 9�������"��������� ����8��.��0���'9'�"�!� ����� ���"���"� ��� ��� � ����"� �G ���9���9���(� ����9���(� 6 ����9���(� ��� ��9��������(9�0��� �"���(� "�.���� ����� class Student

{

// fields contained in Student class

string name;

int age;

int marksInMaths;

int marksInEnglish;

int marksInScience;

int totalMarks = 300; // initialization

int obtainedMarks;

double percentage;

}

:���� ���� ������C����!���"'������� �����������'�"�"� �����,��(� ����8��.���0�����!���"� G�� ������C�������0���!������9����'���0�� ������C�"'��������"�!����������

&������������

������������� ����������������������

&��������������

������ ������ ��������������������

&��������������

������ ���������������������������

&��������������

������ ���������������������������

Page 49: 3Programmer's Heaven C# School Book

��������������� ���������

==

��!���������!��"�!!��� �"�����.������' 0���'� 2����8#$�� ��!����5����

� � 2�� � 2!���� 2�2"��0�� 2�20��� &������� Ga2G* ������������+��� � HH*��.����� �+A07��� ���

/�� �� �

,����"�������.������ .��!����"� ���"����%�����"�����(����� .����������������.��������� "�������� �������!�.���������"�����.������� ������!��������"��(����!��� <return type> <name of method>(<data type> <identifier>, <data type> <identifier>,...)

{

// body of the method

}

&���8��.��9 int FindSum(int num1, int num2)

{

int sum = num1 + num2;

return sum;

}

����9'�"�!� �"������" ���"&� "���'������(��'�.���������!� ���.�* �� � " ��1+� "����� �������!��.�� ��� ����(��'��"����� ��!������""�� ������� � ���� �9������� ��.�'���"0����"�%�����"�� ����.��� ������(� �.��������*�.���������������"+ void ShowCurrentTime()

{

Console.WriteLine("The current time is: " + DateTime.Now);

}

����0��������"��(� �.��������� "����� ���� ����� ��.�� ��������� ������ "����� ����� ����� ���������������� �������� ���.����

Page 50: 3Programmer's Heaven C# School Book

��������������� ���������

=?

������� �� ���� ��������

� �������� �� �����"*��(� ����07���+�� ���� �'(��'��"� Student theStudent = new Student();

:���� ���"�����������!��� ��� "��� � �07�������� "�!!��� ���.����!����'� ��'��� �����J������ �������0����� � Student theStudent;

theStudent = new Student();

$������������������������ ���.�����"�����.��8��.�!������07����������"'������ �'�.������'������.�����"�����.����������"�� �������� int i;

i = 4;

% �������.���� ���� ���� "���� "����"�!!��� ��0��'�� ��!��� ��� "�07��������� � Student theStudent;

� ��"�����������!��� ��������"� ��!��.����"� �'���������.�� �"�� ���� ��� � ��07���*� ".�� ������"�!���� ��������+��!������������������0���!���*���"� �+���������9��'�������'����.������������G<��!� ��� �"�����0��������"� �G���� '�'���� theStudent = new Student();

��� � �'�07����!��.����"� ��������"��������.� "����!��� ��*���� "��+����� ��������"� ��A �� �'��������������������0���!����������������

-����� ���� ����������.���������

������0���!����*!���"9�����"� ".��.�����+��������"�� �"��G�G�.���������� ������!��� ���!����07�����(����� Student theStudent = new Student();

theStudent.marksOfMaths = 93;

theStudent.CalculateTotal();

Console.WriteLine(theStudent.obtainedMarks);

#��G �'��(�������"� ����'������������"!���"9�����"� "��� � �� �������� ���,�� *+�����"�

Page 51: 3Programmer's Heaven C# School Book

��������������� ���������

=�

using System;

namespace CSharpSchool

{

// Defining a class to store and manipulate students information

class Student

{

// fields

string name;

int age;

int marksOfMaths;

int marksOfEnglish;

int marksOfScience;

int totalMarks = 300;

int obtainedMarks;

double percentage;

// methods

void CalculateTotalMarks()

{

obtainedMarks = marksOfMaths + marksOfEnglish + marksOfScience;

}

void CalculatePercentage()

{

percentage = (double) obtainedMarks / totalMarks * 100;

}

double GetPercentage()

{

return percentage;

}

// Main method or entry point of program

static void Main()

{

// creating new instance of Student

Student st1 = new Student();

// setting the values of fields

st1.name = "Einstein";

st1.age = 20;

st1.marksOfEnglish = 80;

st1.marksOfMaths = 99;

Page 52: 3Programmer's Heaven C# School Book

��������������� ���������

=�

st1.marksOfScience = 96;

// calling functions

st1.CalculateTotalMarks();

st1.CalculatePercentage();

double st1Percentage = st1.GetPercentage();

// calling and retrieving value

// returned by the function

Student st2 = new Student();

st2.name = "Newton";

st2.age = 23;

st2.marksOfEnglish = 77;

st2.marksOfMaths = 100;

st2.marksOfScience = 99;

st2.CalculateTotalMarks();

st2.CalculatePercentage();

double st2Percentage = st2.GetPercentage();

Console.WriteLine("{0} of {1} years age got {2}% marks", st1.name, st1.age, st1.percentage);

Console.WriteLine("{0} of {1} years age got {2}% marks", st2.name, st2.age, st2.percentage);

}

}

}

����9'������"0������� �� �07����!������"� ����*� +9'���� ��� �" ���9���� "���(�!�����"� ��#����9'������"�����"����������������,��(� ".���� ����9��� '���������"� "����"���.���� ����� ������0��� "!� ����.�� ��"����� ��� ���'� "�'� ����.����"��������.���� ��������� ������07����!��.����"� �9��� ".�� ��"�������0������ ��� ���'��9����� ��������� ��07����!������"� ��������'� �� ��� ������.���� "�� ���.��������'���"�.����Einstein of 20 years age got 91.6666666666667% marks

Newton of 23 years age got 92% marks

-������/ . ������-����� � � �#���3����

� ������"� ����9������ ��������������!���!���"� "�����"����!� �'� �9��I���� ��� ����������,��(!���;22����1229������ �� ���.���� ��������� �0��� " 22[9'����� ������'���(�������������.����"�������"�!����������0����������7��!�����.��.��9����9�������� ��������.������������0�����������=������"�!��������� 0��..���"��� ����0���!���������������� �������� �'�������"����.��� � �����"���!"������ ����������

Page 53: 3Programmer's Heaven C# School Book

��������������� ���������

=/

-������/ . ��� �����.��� .������ .���������0���� � ��0������"'���� ������������ ��� ����.�������"� ��� �� �����.��!���0���� 0������"!����������� �.��7�����!��������.�� ������"

!���������� ��� � ���.�� ��� �� �� � ��0������"!����������� �.��7���.�������" �� 0������"!������ ��� � ����� "��.�� ������"!�������� ��� � ����.�0��� .�0������0����� ����������"��� �� ��% �� �'���� �������� �������

����� � A07���A��� ��"��������� �*AA�+�����'���"���"� "������� "�"�����(�������!���"�.������� "����'�������!�������������� �������� �����"0���(� �����.�0����&���8��.��9'������� �������"� ����0����(� �������!���".������� "�������������"� ������.�0���� class Student

{

// fields

private string name;

private int age;

private int marksOfMaths;

private int marksOfEnglish;

private int marksOfScience;

private int totalMarks = 300;

private int obtainedMarks;

private double percentage;

// methods

public void CalculateTotalMarks()

{

obtainedMarks = marksOfMaths + marksOfEnglish + marksOfScience;

}

public void CalculatePercentage()

{

percentage = (double) obtainedMarks / totalMarks * 100;

}

public double GetPercentage()

{

return percentage;

Page 54: 3Programmer's Heaven C# School Book

��������������� ���������

?2

}

}

�!���"� G����(� ����0���!���'���� ������"�!���9��'���0�������"��.���������0��P������ ���"�!����������"�!���!��������0���!�����.������� :���� ����..��������"�!������������.�� ������������9� ���!���9�����9� ��9"�������� "��� ��&����.E�������.�*��.� ��0�� "0�� ��������.��8��.� ���.���+��(����9� ���!���9������ "� ������� � ����.�0���� "� ��� ��������"�!���'������������ � ��"����0�"�0����� !��������������"�!���"� G���(�� ���������.��&� ��������� ���..��������"�!����� ���.����

"�$��� ���

:�����0�'� "��� ��!'�"������������!���"� �������.������9��'�� '���� �������������������������!��� ���'�"�"� ������"� ����0�!���Q���� '�������������.������������!����� �������.����"�����..����!"�!� � �.��.������ ����� ����������� ���"���� ���� �������(�-���� "�449!������� ����.������!���"�!����9.�0��������"�����"������*������������������+� "�����*����� ��������+'���"�!� �"��(��!'������.������!���" ��� private string name;

��� 9���������� "�����'���"0���(� // getter to name field

public string GetName()

{

return name;

}

// setter to name field

public void SetName(string theName)

{

name = theName;

}

<� �����'�����"�����������������.������������0���&���8��.��'��� �.���� ��"�!� ����������!����������,��(!���"����(������"� ��� private int totalMarks;

public int GetTotalMarks()

{

Page 55: 3Programmer's Heaven C# School Book

��������������� ���������

?

return totalMarks;

}

�� ������"�������9� ��� � �����"���������!�����,��(� "�� ����"�!����:���� ���"���"�������(����� "���� 0�!������ � �������������!���" private int marksOfMaths;

public void SetMarksOfMaths(int marks)

{

if(marks >= 0 && marks <=100)

{

marksOfMaths = marks;

}

else

{

marksOfMaths = 0;

// or throw some exception informing user marks out of range

}

}

���.����"���������������!�� ����������'!���"�!������������"0������"� ""����� �.�������@��9���.��0���������� ��"��"�!� ��'������"� "������.��!�8��� ����!����!���"'���3����������.����"����0����� �..���!������������� "������ ���!����!.��.���������.���������� ��8�� ������� �������"�����"9'���������.���.������!���"�!���� "����������� ����������'���!���"�� 0������"�

B� ���"�$��� ���

����� ������ ��8!�����.������ <access modifier> <data type> <name of property>

{

get

{

// some optional statements

return <some private field>;

}

set

{

// some optional statements;

<some private field> = value;

}

Page 56: 3Programmer's Heaven C# School Book

��������������� ���������

?1

}

��" G�� "���� "��Q$�.��0����#��G�����!���'���� �8��.���'������.������!���" ��� private string name;

��"���"���"�!� ��.��.����!�����.����"� �0���������� "��������'�����.��'���� public string Name

{

get

{

return name;

}

set

{

name = value;

}

}

��"�!� �"�.��.���������"G$���G� ".����"�"0����������� "������� ���!����!���UV� "��UV0���(�$�������'������"���.��.����G$���G'���������� ����.������!���"G ���G����0����� ��� �� ��� �� ������.��.�������������������.� "� �!���"0��'���!���������� �..�����*!�� ���E]$���9!��.���� ����E]����� ����+�%.��.���������������������� !���"9���������������(�"�.�0���'������������.� "� �!���"�*� "����"0�+�����.�������&� ���� ���� �����UV0���(9'�'���� name = value;

����9�������(��'��"� "�� ��� ��������.��"'�� �.��.����������"�� ���.������'�'��������.��.����� Student theStudent = new Student();

theStudent.Name = "Faraz";

string myName = theString.Name;

theStudent.name = "Someone not Faraz"; // error

�����"�!� � �.��.�����9'���".��.���������� ��8�� ��������� '�'���� theStudent.Name = "Faraz";

������.�������������.��.����$����� �����!��� "�"��!��� �� ��.������9���'������������UV0���(�!���.��.�����.�� �H&���CH�������*'������(��'��"+�� ��� �8��� �'�� '�'����

Page 57: 3Programmer's Heaven C# School Book

��������������� ���������

?;

string myName = theString.Name;

������.���� �'���������.��.����$����� ���������� "�"��!������ �� ��.������9�� ����'�������������UV0���(�!.��.����$���'����'�������� ����� �� ��!���.������!���" ���*H&���CH� ������9�'���� �"� �� �1+'����'���0�����"� ����������� ������0�� ������ ��9'�� ���.����!� "������!�.��.����9������(� '������ ��8���������"� "��(��..��.���������� '�����.���������� ��8�� �������� � theStudent.name = "Someone not Faraz"; // error

'����� ���������.������������*�!�����"����"�������"� ����+���� ���!���"�"������".������� ���"��������� �!���� :���� �������"�!� ���� �!�������!���UV����UV0���(��!������ ��!����9� "���������������9��I��'���������.�������������&���8��.�����#� ���.��.����� ���� ��������"� ��P�����9�����.���� �������� ������ ���"�!� ���� �!���UV0���(�:���� '���������� �� ������UV9��UV0���(����"�� �����"� private int marksOfMaths;

public int MarksOfMaths

{

set

{

if(value >= 0 && value<=100)

{

marksOfMaths = value;

}

else

{

marksOfMaths = 0;

// or throw some exception informing user marks out of range

}

}

}

"������ ���6 ����� ���$�$��� ��� �

�� ���.�����"� G����������� ����P��9���� "��������(��'��"� �� �� ���"�����.��!������������������.��!.��.�������"������"'�� "������ ����.��.���� �� %#�%:���.��.�������0���(��UV� ".��.��� "� ����� '������ �.��.������

Page 58: 3Programmer's Heaven C# School Book

��������������� ���������

?B

�� �A$G������'���������UV�����UV0���(� �� ����� � �� <$#6������.��.����� ����� � "�������������!������.������!���"��(�

get { return name; }

set { name = value; }

6����07��������!��� �����'����.�� �������!���..��� ��������"����9����07��� ��"��.�����!9'���'���"'�"�Q��..��� ���������"� �9'�����������"�����*+������������ !������� �!���"� �� ���"�(�� ��������"9'������"� ����������"����*+�!&�����������'������(�����07������������.��������� class Student

{

string name = "Some Student";

int age;

public void Store()

{

FileSystem fs = new FileSystem();

fs.save(this);

}

}

��.��"�����.������������������"����*+'����.�� �������07�������!� class Test

{

public static void Main()

{

Student theStudent = new Student();

theStudent.Store();

}

}

����9'�� �����*+������"9�����!��� ��������"� �'���0�.��"��.�����������������*+�����"� �����*+��� �� ��� ����9���.������������ �������� "����������"��� ���"���������� ����!���!���"������!����� "���"��� �����"� ��0��� ������!��� ��� class Student

{

private string name;

Page 59: 3Programmer's Heaven C# School Book

��������������� ���������

?=

private int age;

public Student(string name, int age)

{

this.name = name;

this.age = age;

}

}

����� ����� �������'�� '��� ��������9'��������������������0��.��"� ��������"'����������"�'���� �� �����0��*!���"+'������ ������ ��9�������!���"9'���"���������"��� �������� �� �����0��*!���"+'���������0��.��"����������.��������� ����� �8���������!��9'�"���� "����� ����"�� �������������� "���.�������'���H���H!���������� ������!������!����0��'������

&��� ��/�������.�� ��������

%��������0���!�����������'������� �.���� �'���� �� �����0��9��� � �����0��� �������07���0�� �������"�&���8��.��9�!�������� � �� ��!���" ���� �������� ������ �����07����!������� ���'���������.�����!���" ����!���' �������� ���������!���0��'������������"���������������0��0��� ������'���������������� ��� "���"����07����&���8��.��9�!�������������.�� �$��0��!���"� �������"� ����9��� �����'���0����� ���� �� ���!���!���"� "�������07����!������'����������� ���!���"���� ����"�0�� ��07�����.�� �$��0��'���0������C�"0����������07�������������0�����"�!� �"�� �(��'��"����� class Student

{

public static int phoneNumber;

public int rollNumber;

}

���������0����������"'������ ����!������������ ��!��� �����07����#��K��(������������ ��� � �,�� �����" class Test

{

public static void Main()

{

Student st1 = new Student();

Student st2 = new Student();

st1.rollNumber = 3;

Page 60: 3Programmer's Heaven C# School Book

��������������� ���������

??

st2.rollNumber = 5;

Student.phoneNumber = 4929067;

}

}

��������� ���������.�� �$��0��������"'������� ���!��� ��������07���0��'������ ����!��������0��� �������������"���������!��'����.�������� ��� !���9��������#� �*+� ")��"#� �*+�����"����'������ �!�����������������������"�!�� ������������������� '��'���"����������'�����!��� ��������������������� ��(� �� �07����!����� ����������.� �'�������0����� "���� "���� ��8�!���,�� �����"� ��� !�������"������"�������#)������'��������(� �� �� �� ���!������������������0�������!��'�� ���'� ��������"�����������"0�������0��������07����!�������:���� �������!���"9�����"9.��.������ "��� �� �������'����'���0������"0�!���� �� �� ���!�������������"��������� ����������"������"��(� static Student()

{

name="unknown";

}

%����������"���0������"'������� ���!��� �����07���9����� ����� �� �����0��� �"�����������"��.��.�����9'�����������������������0��!���� � E������� ��8��������� !��0�� ��0����������������0��!��� � E������� ��8��������������0��0��� ���������� "���.��� �����.�������!����8��� ���!��� �� ����07�������"�!� ���� �!,�,����"*+� !����'� ���"�'��� �����.��� class Student

{

public static int phoneNumber;

public int rollNumber;

public void DoWork()

{

// legal, static method called in non-static context

MyMethod();

}

public static void MyMethod()

{

// legal, static field used in static context

phoneNumber++;

// illegal, non-static field used in static context

rollNumber++;

// illegal, non-static method used in static context

Page 61: 3Programmer's Heaven C# School Book

��������������� ���������

?�

DoWork();

}

}

����.�������� ���.�� �� ���� "

�� �� G�.������� �����������"� ��������������� �����07������� ��""��� .�� ��.��� "��(�����������8�� �0���

�� �� G��������(�����'���� ���������������"� ".��.������ ����������������"���� !��"�� �����

�� :���� "������� ��0���!�07������� ��"�"�� ����'������ �����������"9�����������"�� G�0�������""� '������� ���� ��0���".������.�������9������ �'�"�����"� ���A07���A��� ��"����"����!.�������� ��

&���/��������/�� �� �

���� ��� �"��������������������'�(� "�!G��.�G� ���5������.�� ")�!��� ����.��5������.�9������.�����"�����.�9���.��"�������"0�������)�!��� ����.�9��(��07���� "�����9���.��"0���!��� ���

�����������

�� �����������.�����(� "�!�����"�%�� ������������!����'� �.��.������

�� ���������� �������� ��� � ���� �� ���� ������ ��.� �� ���������������������"'�� � �'� �� �����07����!�����������"9�� ��'����G�����"�

�� �������� �� ����� ��������� ��� � ������C���� ��"�!�������07���9��(���� � �"�!��������������!���"�

#���������8��.��� using System;

class Person

{

// field

private string name;

// constructor

public Person()

{

name = "unknown";

Page 62: 3Programmer's Heaven C# School Book

��������������� ���������

?�

Console.WriteLine("Constructor called...");

}

// property

public string Name

{

get { return name; }

set { name = value; }

}

}

� ������� ����0���9'������.������!���" ���9�.�0����� �������'����� ������C���� ���!���"'������ �H� ( �' H� ".�� ���������0�� �����"9��� '������.�0���.��.���������"I'�������.������!���" ����#����(�� �����������'������ ��� ���,�� *+�����"� "'������������� ��� class Test

{

public static void Main()

{

Person thePerson = new Person();

Console.WriteLine("The name of person in object thePerson is " + thePerson.Name);

thePerson.Name = "Faraz";

Console.WriteLine("The name of person in object thePerson is " + thePerson.Name);

}

}

� ���������9'���"�� �07����!������� ���� ".�� ��"��� ����!.��� ������ ��� ��"���������!$���� ".�� ��"���$������� ����������!���.�������� Constructor called...

The name of person in object thePerson is unknown

The name of person in object thePerson is Faraz

$������������ �������������"7���'�������"� �'� �� ���!���� ���� "� ������C�"���!���" ���'������ �H� ( �' H�� !���9'�� '�������� �'�07���9'������������������ ��������!������� Person thePerson = new Person();

�����'���� ����������������"�.�0�����!�����(������� �������.������9 �� �'���"0��0������(�� �07����!�����������"��!��*������������"� �������!���������"+������9�!������� ����"�!� �"��

Page 63: 3Programmer's Heaven C# School Book

��������������� ���������

?/

class Person

{

private Person()

{

}

}

��� ��'���"����� �������'����� class Test

{

public static void Main()

{

Person thePerson = new Person(); // error

}

}

����� ���������' �!������0�� .��������E��9��������"� ����(�� �.������������� "�!� ��� �������'������(����.���������class Person

{

private string name;

public Person(string theName)

{

name = theName;

Console.WriteLine("Constructor called...");

}

}

$�'9����07����!������� �� � ��0�������"0�.�� ����� �� ������� �������� Person thePerson = new Person("Faraz");

�!���"� G�"�!� �� ��� �������!���������9������.����'����� ������ ��.��.��������E���� �������!�����������'��'�'����0������(�������"� ��07������ '�"�" ��.���!�� ��� �������!��������"� �����

Page 64: 3Programmer's Heaven C# School Book

��������������� ���������

�2

� ��� E�;<�/�� �.��������������

6������� ����������������*��.�������+� ������"!������A07������'������ ��� ������"&� ���C�*+���������"������ ���"��0������"'�� �����07�������0�����������"*������"!���������+�:���� ������"���������"� ".��������"�!��!���� ���������������������"'�� �� �����07����&���8��.��9 protected override void Finalize()

{

try

{

Console.WriteLine("Destructing object...");

// put some code here

}

finally

{

base.Finalize();

}

}

����������� �����������������$������������ ����������������%����������������� ��������������������� ����������� �

�������������������������� ��$������������������$������&������"������������������������� �

2����������

������������7������..�����!�� ��������������������"'������!����'� �.��.����� ���������� ��������� ��� � ����0��.��!�8���'������b*���"�+�� ���������"�������������'�� ����07�����0�����0�"�������"*'�� ���0��������������0�����"����������07���+����� ������ ��.����"���������"��������� class Person

{

// constructor

public Person()

{

}

// destructor

~Person()

{

// put resource freeing code here.

}

}

Page 65: 3Programmer's Heaven C# School Book

��������������� ���������

%��������!!���9��������.����� ��� ������ �������"�������������&� ���C�*+�����"9'�7���'�0���������������� ����"��������� ����� ��.�������� �.�������*�����'��-���"��..�"����"���!"��������+�� ���"���!�449.�����������"���� ������������������ � ""�E��������� ����������'�����"�������!��������������������"0�����07���"� ����������� ��9���.��0�0��'� G�� ��� ���"����������&� ���C�*+�����"�����!�� �

/�� ���������������3���� ���

���.��0��������������� � ������"'��������� ���� "����� ��.�0��'����"�!!��� � ��0��� "��.��!������ �*.��������+����������"�����"�������"� ��&���8��.�����.��!������������'����� class Checker

{

// 1st overloaded form

public bool isDefaultValue(bool val)

{

if(val == false)

return true;

else

return false;

}

// 2nd overloaded form

public bool isDefaultValue(int val)

{

if(val == 0)

return true;

else

return false;

}

// 3rd overloaded form

public bool isDefaultValue(int intVal, bool booleanVal)

{

if(intVal == 0 && booleanVal == false)

return true;

else

return false;

}

}

Page 66: 3Programmer's Heaven C# School Book

��������������� ���������

�1

� �������(������0���'�"�!� �"����������"'������ ������!����5����*+��������� ��.��!��������0���0�����"�!!��!������������� .���������������!����'�"�!!��� ���"�����.��!���.��������'�����������"� �"�!!��� ��� ��0���!.������������ ���!����5����*+������"9������.����'���"���"�*� ���0���!�����.�� " ��0���!.��������0�� �.��"+'����� ��!������������������������&���8��.��9� ���,�� *+�����"� Checker check = new Checker();

Console.WriteLine(check.isDefaultValue(5)); // calls the first one

Console.WriteLine(check.isDefaultValue(false)); // calls the second one

Console.WriteLine(check.isDefaultValue(0, true)); // calls the third one

)����0�����������"����������"�""�.� "� �� ���.������������ " ��� �������� ��.����������#� �*+�����"�!�� ������� �������� ���.����� /"�!!��� ��������"�"!���O�������$��&����'��(������ ����� ��,��$!������!�����

�3���� ��������������

�� ���� �����������.�������.��!�����"9'��� �������"�� ���������������� class Person

{

private string name;

public Person()

{

name = "uknown";

}

public Person(string theName)

{

name = theName;

}

}

$�'9�!'�������� �07�����(� Person thePerson = new Person();

���!����� �������'���0������"� ������C� � ���'���H� ( �' H��!'�������� �07�����(� Person thePerson = new Person("Faraz");

Page 67: 3Programmer's Heaven C# School Book

��������������� ���������

�;

������ "�� �������'���0������"� ������C� � ���'���H&���CH�%����� ��9�������"� ������"� "�� ���������������.�����������!!��8�0������ "��"��������!���.��8�������'���"�����'��0�.��"���"�!'���"����"�!!��� � ���!�����������"*-���� �"��'���'���"��..� ����.���� ����!�����#� �*+9'��'���"������"�������.'��� / ���O+

@������#$���;���!���.�D�#6��<�

��� '�.�������0���!� ��.�����"�����.���������"9�� ��.����������� ������ ��������.�� ".��������.�����������"���������������.��!��������0�������������0��� �"���������"��� ���!�����"�!���������.������0��*.��"��.��������+� ������"9���������������!��������0��'���" ��0���� ��"����"���������"�#�������� ����������,�� *+�����"� "������(*+�����"� class Test

{

public static void Main()

{

int a = 3;

DoWork(a);

Console.WriteLine("The value of a is " + a);

}

public static void DoWork(int i)

{

i++;

}

}

���.������'��������� The value of a is 3

@��������.��!��������0����.��"����������(*+�����"� " ����������0����%��9 ����������������������0��� �����(*+� "�������������0��� ,�� *+��� ��9������ 0������"'���� ������� ��� � ������"� ���� !���9'����"�!� �� ��P� "�!!��� ������"� "����'����������' �����0���� " � �'���"���������.� "� ��'���� ��������.�������� ��.����"��(��'��"9��!9'������� ��������������.�'���0�.��"0���!��� ��� ���"�!���"�!����0������0���������� ��9��� ��"� �� �"���������"'���"0���!�����"0��(�!�����������"��0�� �����"� "����� ���"�@�����������"�� ������ "�����"����� �����"0�"������"���!� ��"����������"����0���������������������0���!� class Test

Page 68: 3Programmer's Heaven C# School Book

��������������� ���������

�B

{

public static void Main()

{

int a = 3; // must be initialized

DoWork(ref a); // note ref

Console.WriteLine("The value of a is " + a);

}

public static void DoWork(ref int i) // note ref

{

i++;

}

}

���.������'����������!����'� ������� The value of a is 4

� �������!�����!(��'��"9��������0�����0�� ������C�"0�!���.�� �������������"0���!��� ��������.����"�������(��'��"�������"!��.�� �������0��!�����.��.��.������'������� 0�.��"0���!��� �����'����9'�� �� �������(��'��"9��� �� ��������� ������C���������0��� class Test

{

public static void Main()

{

int a; // may be left un-initialized

DoWork(out a); // note out

Console.WriteLine("The value of a is " + a);

}

public static void DoWork(out int i) // note out

{

i=4;

}

}

���.������'��������������� The value of a is 4

Page 69: 3Programmer's Heaven C# School Book

��������������� ���������

�=

9�.��������#$���

A07��������.�������.��"0���!��� ��������� ����� �����.��!�����!��� ���.��"����������""��� ������"� ������� ��� ��9�!'�� ������C�� �����*'������ �07���� ��+� ".�������������"'������������������ ��"9��� ������ ��"�!!���'���"0����0���!�����������"��0�� ����� ���"� �������� ������"� class Test

{

public static void Main()

{

int [] nums = { 2, 4, 8 } ;

DoWork(nums);

int count =0;

foreach(int num in nums)

Console.WriteLine("The value of a[{0}] is {1}", count++, num);

}

public static void DoWork(int [] numbers)

{

for(int i=0; i<numbers.Length; i++)

numbers[i]++;

}

}

���.������'��������� The value of a[0] is 3

The value of a[1] is 5

The value of a[2] is 9

����9'�� ������C�"� � ���.������* ��+'��������������.��"������������������(*+�����"9'����� ����� ��"*��"�!��"+����� �� ��!���������&� ����9'�.�� ��"�������� ��!������%������.�������9��������(*+�����""�"��� ���������� �� �����* ��+� "'��(�"� ������������ " ��� ����.�*��������� �������.�+�

&��������������.������������������

%��!��� ���7���.�� ������� "��������07���� ����������.��0����������� �07���'���������� ����� "����� ���!��� ��� new Student();

Page 70: 3Programmer's Heaven C# School Book

��������������� ���������

�?

����0����� �������"������ ����'���������� �07����!���"� ����'������� ���!��� ��.�� �� ������% �07�����������������0����0����0�����������"'�� ������ ���!��� ����.�� ������9� ����0������9��� �'���"� ��07���'���� �� ���0������0����0����0�����������"�!������������ �68.���� ��".����������!�� ���������"� ����� ��!��� ��"�07�����(� int pc = ( new Student(87, 94, 79) ).CalculatePercentage();

� ����0����� �9� �'�07����!������"�� ��������"� "����������������� ����*+�����"������"� ������ �'��������"9� ��!��� ��"�07���'���0������0����0����0�����������"7���!�����������"�������������� ����*+���.�������8������ ��.��� ����'� G�� ������������'������������� ������0����� ���������� Student theStduent = new Student(87, 94, 79);

int pc = theStudent.CalculatePercentage();

theStudent = null;

����� �" �����������"� ������07����0���'���0�"������"�!��������"��������� ����� �������!.�������8��.������ ���'����� Student student1 = new Student("Faraz");

� �'�07����!��.����"� ��������"��������.� "���� "������� �������!��� ����"� � �#�����(�� ������07���� "�������� "���������!��� ����"� �1� Student student2 = new Student("Newton");

$�'9�!'�'���� Student student3 = student2;

��� �'��!��� ����"� �;'����������.�� �� ������"� �*$�'�� +�����"�.�� ��"0���"� �1��� ��0�����"� �1� "��"� �;'���0�.�� �� ��������"� �� "0����� ��(���� ��������07����

����"� ��07������ ������� � ������"� ��07������ �������������

c�����07���c

1��$�;/���#<���"� �

��"� �;

���"� �1

Page 71: 3Programmer's Heaven C# School Book

��������������� ���������

��

$�'�!'�'����9 student1 = student3;

��'����������.�� �� ��������� "�07���*$�'�� +9����� ����!�����"� �*&���C+� ��!��� ��"������ ���!�����"� �� �'�����0����0����0�����������"� "�� 0�������"!���������� ������

�!���'� ����!��� ������!��� �� ���� �9����� ������ ���9'������(��'��"� ��� student1 = null;

����"� ��07������ ������� � ������"� ��07������ �������������

c�����07���c

1��$�;/���#<���"� �

��"� �1

��"� �;

��������������������������

Page 72: 3Programmer's Heaven C# School Book

��������������� ���������

��

����� �� ������!�"�#��$ ���

������"����

��"��'�'������� �0���!� "��� ����07������� ��"!��������(�� ������ ��� ".������.���� �����'�������0�0���"� �� ���� "���� "� ��!� ������ ��� "��� '�'���������'��"� "���� "� ���'���..���� ������ �����'���.� "��������8.���� �����07������� "��� '�'�������'��".������.���� "��'.������.������.���� ��"� �����'���!� �������� 0�� "���� "� ���'0�8� �9� E0�8� �� "��.�E���� ������ ��'��(� ���

�� �� ������

< ����������!����������07������� ��".�������� �9���'�����������"�����0���)���0������ "68�� �0������)���0���������.��.�����!���"���*����.� � �9�������� ������"+����� �0������0���"� "�!!��� ��..������� '������� ������������ ��� ���������"��68�� �0������!���"������G.��� ������0��8�� "�"*� �� ��"+� �' ��"�������)���0������ A07���A��� ��"��������� ��� �������������"0���"��� ����.�� �0��'�� "�!!��� �����9'�����8�� �0�������������"0��0E���� �����.�����!�0E���� ��������8�� "��!� ���� �����������"� ������ �����0E��.� �� �������� �����*��������������0E��.�"+������"���0��9.��� ����.����������������� ��������!� ���� ������!���0������ "�8�� "��� ���' '��������"����09����"9"�����"��� ������"����

7����������

2�� 3�������� 2�� 3��������2�� 3��������

Page 73: 3Programmer's Heaven C# School Book

��������������� ���������

�/

&���8��.���

� ���!������0���9'�������"�<,#*< �!��",�"��� �#� �����+���"����������'� ������ �������9���.�����0�����'����������9)���� ���� "�����������0E�����%0��������������� ����!� ���� �����9'�����0E����.��.���!��!� ���� ��������9'�� �0E���� ���� ������ �9'�������������������� ��� ��������� �� �!����@*�0���+� ���������%*0�����+9��� @'���"�������.��!������� �� �����0��*!���"9�����"9.��.�����+�!���%� "@�� �������������0��*�8��.�!�����.���������0��+�!���%�����������0���!�0�����"����� ������"� ��0E���90�������� ��0������"0�����0E����%��9� ������ �����"���������9�������������� ��.��� �����'������� �0E����������.��!0������*�!������ ������� �����"��9"� G�'����9��� �'�������������'�� '���.���� �� ������ ��� ���!����'� ������ +

�� �� ������ �����

@�!���'���� ����.���� ����� 9����������(��E.�� ��0���� ������ ��� ��

�� ��9��(�-���� "�� ��������449����'� ��� ������� ������ ���,����.��� ������ ���!����� ������'�"� ���

�� ���A07������"�!� �"� �������� ���.������.������������������0������!����������� ��*� "����$6�!����'��(+

�� � ���!���� ���� � ������������ � �� ���!������9�����.��� ������ ���!� ���!��������'�"� ��*���� ��������-���+���'������(��� ���!���� "������ ������� ���� ����������*�����+� ���� � ��� �����*����.���� �+� ���!���� "�� ��0�� ������"� ����������� '���������������������������������������������������������������������������������������������������������������

���������������������(����������������� �����%����������������� ���� ������������������������������!���� �����������������

��� �������� ������������� ���������������������������%���������� �

& �$�� ����!�"����� ����!�"����� �����#�����!�#�����

9���������$�%� ����� ����&'�$�����&������!�#����'

���3��$�%� �#�(�&'�$�)��"�����&'�!�����*+"�����

� �����$�%� �#�����&'�$�����&������!�#����'�$�#������,���-����.�

4��.��� ���0���E��.��� �����������.��� ���������"

Page 74: 3Programmer's Heaven C# School Book

��������������� ���������

�2

��$������ ��� � �� ������ �����

����������� G�G�.��������� "������ ������ �����..��'������������"� �'������!���"���������� $��0��9 ���� ""���A!@����9��� �'�����������.� "� �.��.�����������������������"3��%��*+'�������������� "����� �������!�����"� �� class Student

{

// private Fields

private int registrationNumber;

private string name;

private DateTime dateOfBirth;

// Student Constructor

public Student()

{

Console.WriteLine("New student created. Parameterless constructor called...");

}

public Student(int registrationNumber, string name, DateTime dateOfBirth)

{

this.registrationNumber = registrationNumber;

this.name = name;

this.dateOfBirth = dateOfBirth;

Console.WriteLine("New Student Created. Parameterized constructor called...");

}

// Public Properties

public int RegisterationNumber

{

get { return registrationNumber; }

}

public string Name

{

get { return name; }

set { name = value; }

}

public DateTime DateOfBirth

{

Page 75: 3Programmer's Heaven C# School Book

��������������� ���������

get { return dateOfBirth; }

set { dateOfBirth = value; }

}

// Public Method

public int GetAge()

{

int age = DateTime.Now.Year - dateOfBirth.Year;

return age;

}

}

������"� �����0����������.���������"�!� �"�����.������!���"9�����������.��.������ "� ������"������������������!�����"� ��������"�!� �"�'��� �����������!���� ���(� �.��������� "����������(������.���������$�������'������ ��"�!� �"������UV.��.�����!���������� $��0��� ��'�"� ��'� ��������!������"� ��������� ������������ $��0��� ������0�� ��� �"������������ ��������%�� �������'�"�" ����(����3��%��*+�.��.����90��������"�������� !����������.��.���������� �������..��"��0�������!������� �I���� ����������!!���"� " ��!����������� �I.����� ����"������ ��9����(�� ���"������3��%��*+�������"� $�'9����"������� �������� ���"���������"� ������ �����������"� ����90��'����""���� �����0����(����(�!"�!!��� ��07���� "�����"!����������� ���������(� ".���� ����� class SchoolStudent : Student

{

// Private Fields

private int totalMarks;

private int totalObtainedMarks;

private double percentage;

// Public Constructors

public SchoolStudent()

{

Console.WriteLine("New school student created. Parameterless constructor called...");

}

public SchoolStudent(int regNum, string name, DateTime dob, int totalMarks, int totalObtainedMarks)

: base(regNum, name, dob)

{

this.totalMarks = totalMarks;

Page 76: 3Programmer's Heaven C# School Book

��������������� ���������

�1

this.totalObtainedMarks = totalObtainedMarks;

Console.WriteLine("New school student created. Parameterized constructor called...");

}

// Public Properties

public int TotalMarks

{

get { return totalMarks; }

set { totalMarks = value; }

}

public int TotalObtainedMarks

{

get { return totalObtainedMarks; }

set { totalObtainedMarks = value; }

}

// Public Method

public double GetPercentage()

{

percentage = (double) totalObtainedMarks / totalMarks * 100;

return percentage;

}

}

������������"� ����� �����������"� ����0��� �������� �.������� class SchoolStudent : Student

������������"� ����� ��������������0���!������"� ������ �""���� 9�����"���������' ���0��������.������!���"*�����,��(9�����A0��� �",��(� ".���� ����+'�������������.� "� �.��.�����9�'��� �������*� �'������.��������� "� �'���.��������+� "� �� �� �������"*3������� ����*++�&�� �'9!������0��������� "*.���������C�"+�� ��������!������������"� �����#���'�������������� ",�� *+�����"� // Program to Demonstrate Inheritance

class Test

{

static void Main(string [] args)

{

Student st = new Student(1, "Fraz", new DateTime(1980, 12, 19));

Console.WriteLine("Age of student, {0}, is {1}\n", st.Name, st.GetAge());

Page 77: 3Programmer's Heaven C# School Book

��������������� ���������

�;

SchoolStudent schStd = new SchoolStudent();

schStd.Name = "Newton";

schStd.DateOfBirth = new DateTime(1981, 4, 1);

schStd.TotalMarks = 500;

schStd.TotalObtainedMarks = 476;

Console.WriteLine("Age of student, {0}, is {1}. {0} got {2}% marks.",

schStd.Name, schStd.GetAge(), schStd.GetPercentage());

}

}

� ���,�� *+�����"9!���'���"�� �07����!������"� ����*�+� ".�� ��"��� ���� "����!��"� ��9��� '���"�� �07����!������������"� ����*����"+��� ��'���"�.������������ ���������� �� ���������"9'������������!��!���"�������.��.������ "��� .�� ��"��� ���9���� ".���� �����!���������"� �*����"+�$�������'�����0�����������.��.�����$���� "����A!@����*"�!� �"� ���"� ����+0�����������������"� ������ ������"!���������"� ����9���� ������ ����.�0���.��.������������ '��8���������0���.������9'�������!����'� ����.��� New Student Created. Parameterized constructor called...

Age of student, Fraz, is 23

New student created. Parameterless constructor called...

New school student created. Parameterless constructor called...

Age of student, Newton, is 22. Newton got 95.2% marks.

Press any key to continue

������.���!���!����'��� ����8.����"90����������.��'�� '�������"������������"� ��07����&������.������������ ��������!���"� �������"� "��� ����� ��������!���������"� �������"� New student created. Parameterless constructor called...

New school student created. Parameterless constructor called...

���������������� ���� �� ������

��� '�� �� ���������0E���*���������"� �+9������.����!���� �� ��������0��E���*���"� �+0������ �� ��!���� �������� "��� ����� ������ ��������!����0E������..�� �''�'� ����������� "�� ��������!������������"� �����&�����9'� ��"������� ������ �'������0��(��'��"� ���������"� �G��� "�� ������� "��������� � "��(��������� ����(�� public SchoolStudent(int regNum, string name, DateTime dob, int totalMarks, int totalObtainedMarks)

// : base(regNum, name, dob)

Page 78: 3Programmer's Heaven C# School Book

��������������� ���������

�B

{

this.Name = name;

this.DateOfBirth = dob;

this.totalMarks = totalMarks;

this.totalObtainedMarks = totalObtainedMarks;

Console.WriteLine("New school student created. Parameterized constructor called...");

}

����� ���������(��.�����������!���""�!� �"0����������"� �� "����"�!� �"0���0�����*���"� �+� "���������������"� ����� ����.��.������$�'9'� ��"����� �����,�� �����"��� static void Main(string [] args)

{

SchoolStudent schStd = new SchoolStudent(2, "Newton", new DateTime(1983, 4, 1),500, 476);

Console.WriteLine("Age of student, {0}, is {1}. {0} got {2}% marks.",

schStd.Name, schStd.GetAge(), schStd.GetPercentage());

}

� ���,�� *+�����"9'�������"� �07����!���������"� ��� ����.���������C�"�� �������� "��� .�� ��"��� ���9���� ".���� �����!������������"� �� ������.���!���.�������� New student created. Parameterless constructor called...

New school student created. Parameterized constructor called...

Age of student, Newton, is 20. Newton got 95.2% marks.

Press any key to continue.

�� �"������� �������������' � ������.��P!������.������������ ��������!���0��E���*���"� �+������"� "��� ���.���������C�"�� ��������!����0E���*���������"� �+���������"�����'����������.����������� �07����!���0�����0�!������ �� ���������0E����$�'9��������� �������.������������ ��������!������"� ����� class Student

{

...

/*public Student()

{

Console.WriteLine("New student created. Parameterless constructor called...");

}*/

...

}

Page 79: 3Programmer's Heaven C# School Book

��������������� ���������

�=

$�''�� '���������.������.������9'������������� No overload for method 'Student' takes '0' arguments

������.������ �0����!� "���C��������� �*.����������+�� ��������!���0��E������������"0�"� �����Q

8 �������:�#6��F����� ��������������.�� ������F�������0$� � ��#�

���� �8.���������������� ��������!�0��E����� ����(��'��"0���0�����0���"'�������� ����������"��*���� �������"��������� +�!�������� G�G�.��������(�� class SubClass : BaseClass

{

SubClass(int id) : base() // explicit constructor call

{

// some code goes here

}

}

� �����"��0���9���.���������C�"�� ��������!����0E���*��0���+��8.������������ ����.������������ ��������!���0���������� ����������.���������C�"�� ��������!���0��E�������������0��(��'��"9��(�� class SubClass : BaseClass

{

SubClass(int id) : base(id)

{

// some code goes here

}

}

$�'9����� ��������!��0���*� �+'����8.���������������� ��������!���0��E���*@�����+������(�� � ������� ��#���.�������'���������������"� �������� �)���(������� ��'���"�������� "�� �������� "��(������(��(�� public SchoolStudent(int regNum, string name, DateTime dob, int totalMarks, int totalObtainedMarks)

: base(regNum, name, dob)

{

this.totalMarks = totalMarks;

this.totalObtainedMarks = totalObtainedMarks;

Page 80: 3Programmer's Heaven C# School Book

��������������� ���������

�?

Console.WriteLine("New school student created. Parameterized constructor called...");

}

����� ��������0����������.���������C�"�� ��������!��0��E���*���"� �+9"������� ����� ������C���� �!���� ������"!���"�����0��E���G.���������C�"�� �������� public Student(int registrationNumber, string name, DateTime dateOfBirth)

{

this.registrationNumber = registrationNumber;

this.name = name;

this.dateOfBirth = dateOfBirth;

Console.WriteLine("New Student Created. Parameterized constructor called...");

}

���������.���� �9����!���"���������� $��0���.������� ".����"�� ��������UV.�0���.��.�������9� �'���� '���� ���������� $��0���!���"� �9�8��.�'�� ����� ���� ��������0��!��0��� ����� ��������,�� *+�����"'���"0���(�� static void Main(string [] args)

{

SchoolStudent schStd = new SchoolStudent(2, "Newton", new DateTime(1983, 4, 1),500, 476);

Console.WriteLine("Age of student, {0}, is {1}. {0} got {2}% marks.",

schStd.Name, schStd.GetAge(), schStd.GetPercentage());

}

��� '��� ���.������9���!����'� ����.���"�.����"� ����� ���� New Student Created. Parameterized constructor called...

New school student created. Parameterized constructor called...

Age of student, Newton, is 20. Newton got 95.2% marks.

Press any key to continue

:���� ��!���������.���0�������E!������.���������C�"�� ��������!���"� �*0��E���+������"� "��� ���.���������C�"�� ��������!���������"� �*�0E���+������"9���� �����..��.����������9'������8�����'���'�'� ��"��"������� ��.��.�� ���� ���9���� ".���� �����!���������"� �� $���������������'�'����0���!�������0E���G�� �������"��������� 9����� ��������!���0��E������'�������"0�!�������� ��������!����0E���� ����������� �"#�������%���� ��������������)������������%���� �����"#������ ��� ����������$�����������"**��� �������

� ����������"**�������%���� ��

Page 81: 3Programmer's Heaven C# School Book

��������������� ���������

��

"�������-������/ . ���

� ������"� ����9���������� $��0�����"�.������� "��� ��������UV.��.��������� �����!������"� ������ ��"�!�������������� $��0������"���������������0�.��0������'� ��"����"�!����!���"*���������� $��0��+� ������������"� ����90��'��� ��"�������.����"����.�������"������"�!���7��!������.�������"���0���!�������� 0������"������� �"������ ��� � ������� �"����0E����<������'� G�0��0�����������.�������"���0����������07�����!��� ��� "�!� �������"��9������.����'���"���.��� � "�� ������ ������ ��..��'���������%'����.�������"�����"�����(*+ class A

{

protected void DoWork()

{

Console.WriteLine("DoWork called...");

}

}

�!'���������������(� ���,�� *+�����"�!���������9������.����'����� ������ ������ class Test

{

static void Main(string [] args)

{

A a = new A();

a.DoWork();

}

}

��� '���������.�����9������.������� 'CSharpSchool.A.DoWork()' is inaccessible due to its protection level

@��9�!'�"������� ��������9@'����� �����%��� �������� ������������(*+�����"� �"���0�"�� class B : A

{

public B()

{

DoWork();

}

}

Page 82: 3Programmer's Heaven C# School Book

��������������� ���������

��

����'�� ������"������@!���������%� "�����"��������(*+!�����0��E���*%+� ���� ��������$�'9'�� '�'���� static void Main(string [] args)

{

B b = new B();

}

��������'���0�� DoWork called...

Press any key to continue

����'����'��� ����.�������"���0���!����� �"����0E�����$��������������'�� ���'���� static void Main(string [] args)

{

B b = new B();

b.DoWork(); // error

}

���� ������.�������"���0����� '��������!��� ���!����0E����

8 ��"������� ��������-������/ . ���

� �������'��9���.�������"� ��� ��������"�!�������'����0��*!���"9.��.����� "�����"+��0������"�

�� � �"������ ��� � ����9�� �� � �"�������.��7���9�� �� � �"�����0E����!����� ��� � �����

�� ��9.�������"� ��� �������(�G.�������"A)� ��� ��G9����9������.�������"��� ��� ���

8 ��������:�#6��

&� ����9�!���"� G�'� ����������0�� ������"0�� ����9����� ���(��'����������"(��'��"�$������ � �����!��������"���� sealed class A

{

...

Page 83: 3Programmer's Heaven C# School Book

��������������� ���������

�/

}

�!� �������� ������ ��������@'������% class B : A

{

...

}

������.����'����� ��������!����'� ������� 'CSharpSchool.B' : cannot inherit from sealed class 'CSharpSchool.A'

����������� "#�������� �%���� ����� ����������)������������%���� ������������ ������������ �

�������������F�� �������.�������������

� ��*� "����$6�!����'��(+��������.�*����9��������� "� ���!���+�����.�������� ������"!������A07������"�!� �"� �������� ���.����������.����"������'E������������ "�� ����!� ���� ������������ "��������� ����$6�!����'��(����A07��������8���������!��'�� ��������.������.���*'����'�����0�������+9����!��� ���!��.�A07����� ���"� ���.��!�07�������A07��������!����'� ������"� ,����"$��� �����.��� 6J���*A07���+ ���.����'��07���!���J����������"�!������.���� ����� � ��

�..�����!��� ���J������9�����9��'�������� �����!0�����!��� ��.�� ����������07����&���������.�90��'������(� ��.��!����"�������"��������"������"���������"��"�!� ��J������!��������07����

������6J���*A07���9A07���+ ������0����8��.�9��������"�������3�������"�*+ )���� ��������"�!���������� ��07����3����.�*+ )���� �����.��07���������.��� �����8����� E������.��!���

����� ��07���������)�!��� ��6J���*A07���9A07���+ )���� �����!0��������!��� ��.��".�� ����������07���9

�����'������� !���������� �*+ )���� ������ ���.��� ����� �!����07����������"��������"

������"���������"��.����"������� ���.��� ����� �!�������� ��07����

.�������"&� ���C�*+ ���.�������"�����"����"0�������""� 0����"�����"������!���� �����������������"������"0��#)0�!����������� ��07������������"0�3��0�������������

.�������",��0��'����� �*+

�����"�������'��.��!�������� ��07����%�����'��.��� ��� ���.��!������� �� ��!���"*����+�!�������� ��07����

Page 84: 3Programmer's Heaven C# School Book

��������������� ���������

/2

�����.����"�� �07���(��'��"'������.�����������A07������� ����$6�!����'��(�����0����*&�#+�

"�#��$ ���

�����"�����.����������������������������'�� ���� � �� �07������� ��".�������� ��� �������!���.����'�� �.����������� � E�07������� ��"�� �����*��(�!��9�!������9'����������+�!����07������� ��".�������� ��� ������������ ".����'�� �.���������'��������9� ���������� "������������07�����������".����'�� �.�����������.������.����������������0� "� �� ,��$*,�����!�������.��$��'��(+�8.�� ���� �H�������.��������0�����!��������.����"�"�!!��� ���.���� ����� �!�����"������������"0������� �����������.�������'������"�!������0������"'�����������"��'���.���!����.���� ����� ��.����"��H

B� ���� ����.�������.�� ��������#$��.����.����� ���� ���������.�� ���#$���

@�!�������� �� �����"������!.������.���9'� ��"��� "���� "���������!��� ���!�0����.��� ���"����07����!�"�����"��.��#������%0�� ������"0�����@� class A

{

public void MethodA()

{

...

}

}

class B : A

{

public void MethodB()

{

...

}

}

��� ����������'����� A a = new B();

�������!��� ���!��.�%����"� �� �07����!��.�@�� ������'���������� �����07����!��.�@�� �07����!��.�%*'�����J����.��0���@���0E��.��!%+�$�'9���.��0����'�����

Page 85: 3Programmer's Heaven C# School Book

��������������� ���������

/

a.MethodA();

@������ ���������'����� a.MethodB(); // error

%�������'������ �07����!��.�@*�� ��� �"� �����!��� ���!��.�%+9'��� ������� ����0���!��.�@� ������..��� ���.������%� " ��@�

B� ������ ��6 � �� ������������ ��� ��7�������� ��&��F������

� ������.��������"�!� �������"���'*+����"��'���.�� ������� � class Shape

{

public void Draw()

{

Console.WriteLine("Drawing Shape...");

}

}

��� ������ ��������������!���������.����9'��������� ��� ������"���'*+� class Circle : Shape

{

public void Draw()

{

Console.WriteLine("Drawing Circle...");

}

}

����9'�����������'*+�����"'����������� ������ 0���������.�� "��������������$�'9�!� ���,�� *+�����"9'�'���� static void Main()

{

Circle theCircle = new Circle();

theCircle.Draw();

}

������G���'*+�����"������"� "���.������'���*�8.����"��+"�.����

Page 86: 3Programmer's Heaven C# School Book

��������������� ���������

/1

Drawing Circle...

$�������������.����'��������'�� � ��������'*+� ��������"����� ������"���'*+�����"�!���.������!� ���,�� *+�����"'�'���� Shape theShape = new Circle();

theShape.Draw();

���.�G���'*+�����"������"� "���.������'���"�.���� Drawing Shape...

�3��� ���� ����� ��F�3 ���������3��� ��:�#6���

$�'9�!'�'� ���������"�������'*+�����"�!������.����� ������������9'����������(������'*+�����"� ������.�*0��+������������ "������'*+�����"� ���������*�0+����������"�� class Shape

{

public virtual void Draw()

{

Console.WriteLine("Drawing Shape...");

}

}

class Circle : Shape

{

public override void Draw()

{

Console.WriteLine("Drawing Circle...");

}

}

$�'9� ���,�� *+�����"'�'���� static void Main()

{

Shape theShape = new Circle();

theShape.Draw();

}

Page 87: 3Programmer's Heaven C# School Book

��������������� ���������

/;

����'�������"�����!��� ���!���0����.�*���.�+����!����� �07����!����0��.�*������+� "����������'*+�����"����������%'�����������""� ������'*+�����"�!���.�� ������������� "� ��������'*+�����"����(�"�������� ���.�9������.����'��� ��� ���������..��� �*����!��� ��+��.���������������"*�����9����������.��������07���0� "� �+9������9��'����..����� ������������ ���������� �� ��� "'���������07�����.���G�� ����G��"���"�'�������'*+�����"������"��������.����"���������".������.���P'����'�����"�!!��� ���.���� ����� �!������"'��������� ���� "�� ������ ���0��� "�0E�������� ���������"������"�� ��0��E��.���!��� ��9������.����������������07�����.���!��� ��"0����0����.���!��� ����"���"�'�����!��������"���������� '����.�������0���.������9��������'���0�� Drawing Circle...

%�������9'������"������'*+�����"�� ������!��� ���!���.���.�9����#)'����� �"������07������"0�������.���!��� ��� "����������'*+�����"�!������������� �����(���������"� ���0������ '����'�'� ����������.������.������������@����(� �������"�������9'�����'������"��0�������""� � "0���".������.��������� ������'��9'����(��������"� ����0E����������"�'�� ���������"� ����������������"� ���0������@����(� ���������"�������"�9'�� �� ������'����"���0�������������"� ���������.� "� �������������"� ���0������ ����������� ������������� �����"#�������!���������� �����������%��)����+������������������ ������������������������,�����

��������$������������%������� ������������+��%��"**,��-���%��"**��� �)������������������� ���������������� �������� ����

������!�������������� ������ �������� �������������������� ��������������"#��������� �������������%���� ������%�

�������� ������!����� ����� �

:�������0�'� "��� �'��� ���.�������8��.��*�!���.�� "���������+9����0� "� �� �������������.������ "���"�!������.�������� �'�����07��������!��� ��*������.�+���"� Shape theShape = new Circle();

theShape.Draw();

:�9���.��0��!��������.�������� ���"���� ���.������������90��������������.����� ���0����"���"�����07����!'������������!��� ��'���"0���!��� �� ����� E�������..��'�"�!� ��'����������)���� ���� "�����'�������� �����������.����� "������"������'*+�����"������������' .���!����.���� ����� � class Rectangle : Shape

{

public override void Draw()

{

Page 88: 3Programmer's Heaven C# School Book

��������������� ���������

/B

Console.WriteLine("Drawing Rectangle...");

}

}

class Curve : Shape

{

public override void Draw()

{

Console.WriteLine("Drawing Curve...");

}

}

$�'9�!'�'�������,�� *+�����"�!����'� static void Main()

{

Shape [] shapes = {new Circle(), new Rectangle(), new Curve()};

Random random = new Random();

for(int i=0; i<5; i++)

{

int randNum = random.Next(0, 3);

shapes[randNum].Draw();

}

}

����9'�������"�� ������!��.����.�� "����"� �07����!���������9)���� ���� "���������� ���$�8�9'���"���)� "��������� �������� "�� ��0��0��'�� 2� "1*0���2� "1� ������+���������� "������ �����" ��0���� � "�8� �����.������������������'*+�����"�$�'9 ������'� ��������.��������'����.���������� "�8�!��.�'���0���"� "������'*+�����"�!'�����0E����!���.�'���"0������"���� ����� ������������ �!������.���� '����.���� "�� ����0���.������9'�'������"�!!��� ����.��'��������� �&���8��.��9'�� ��8���������0���.������9�������!����'� ����.��� Drawing Curve...

Drawing Rectangle...

Drawing Curve...

Drawing Circle...

Drawing Rectangle...

Press any key to continue

Page 89: 3Programmer's Heaven C# School Book

��������������� ���������

/=

8 ����6�:�#6��

��..��9� ������������9'�"� G�'� ���������"�������'*+�����"90��'� ��"����������'*+�����"9'���'��� "�Q� -���9'��� G�"������ �449��� �!'����(��������"� ���0�������������9�����.��0���@��9��� ���"����(��'��" �'�����(������"�� � E������"� ������"� "����� �'����'�"� G�'� �����.������.��������#���"�!� �������.�� "������������ class Shape

{

public virtual void Draw()

{

Console.WriteLine("Drawing Shape...");

}

}

class Circle : Shape

{

public new void Draw()

{

Console.WriteLine("Drawing Circle...");

}

}

$�������'����(�"������'*+�����"� ������'������ �'(��'��"������".������.����� ��������"'�'���� Shape theShape = new Circle();

theShape.Draw();

��� '����.���� "�� ����0�����"�9'�'��������!����'� ����.��� Drawing Shape...

�� ��9'����(�"������'*+�����"� ������������� �'9 �.������.�����..���"����� "������'*+�����"�!������.����������"��!'�"� G����(������'*+�����"'������ �'(��'��"9'�'���������!����'� �'�� � ������.�������� 'CSharpSchool.Circle.Draw()' hides inherited member

'CSharpSchool.Shape.Draw()'.

����(��������� ����0��������"�������.���� ����� 9�""���������"�(��'��"�A����'���""��� �'(��'��"�

Page 90: 3Programmer's Heaven C# School Book

��������������� ���������

/?

8#$������ ���� ���������F�B$F���� ������26�F���� ���

��.����� ���� G��(� �� �07���0�������(�G��G��� �� �����..��� ���.��!� �07���G�� ��9����� ���� �07���� � � ������ �����������������!������0�������'��"�����.*�.E���� �+��!��������.��'��"���0�����*"�' E���� �+� <.E���� ����.��9�!�� "��.�����9�'������� �����!��� ���!.��� ���.��� ��!��� ������07����!����"��.�� Parent theParent = new Child();

A ����� �����9"�' E���� ��� E�!�� "�8.������@�� E�!�9'���� ���������'� �8��.��� L�����9!������ �"�����!����'� ��� ��!��"�� Shape [] shapes = { new Circle(), new Rectangle(), new Curve() };

�����������9)���� ���� "���������0E�����!���.�����$�'9�!'�'� �����!��� �����)���� ����07���� �����.������'��������!��� ���!��.�)���� ���9'��� G�7��'���� Rectangle rect = shapes[1];

� ���"9'��������8.��������..������������� Rectangle rect = (Rectangle) shapes[1];

�� ������������ �� �����..��� ���.��!����07���!���.��� �������"*"�' '��""������� � � ������ �����������+9��������""�' E���� �� %��9 �������"�' E���� ��� 0�� ����!��� "�!'������.���'���� Rectangle rect = (Rectangle) shapes[2];

�� ����.�M1N�� ��� � �07����!��.������'������ ��0�����"�����)���� �����.�9����#)'���"�������!����'� ��8��.��� ���� E����� System.InvalidCastException: Specified cast is not valid.

8 �� ��������:�#6���

������(����� E������.��!� �07���9����� ������������������(��'��"�����.��������.��!����07���'���������� ��.�� "����� �����!������E�0��P�����'��9������� !����&���8��.��9 Console.WriteLine(shapes[1] is Rectangle);

Page 91: 3Programmer's Heaven C# School Book

��������������� ���������

/�

'���".�� ������ ����� ����� "�'9'���� Console.WriteLine(shapes[2] is Rectangle);

'���".�� �!���� ����� ���'� "�'���������������.������������(����� ������.��!� �07���0�!����..��� �"�' E���� �� Shape [] shapes = { new Circle(), new Rectangle(), new Curve() };

Rectangle rect=null;

if(shapes[1] is Rectangle)

{

rect = (Rectangle) shapes[1];

}

%���� �������9'��� ����������.������������(����� E������.��!����07���������.����������� ����!����07���� �����E�0�������'��9���������07��������.���!��"��.�� Shape [] shapes = { new Circle(), new Rectangle(), new Curve() };

Rectangle rect = shapes[1] as Rectangle;

if(rect != null)

Console.WriteLine("Cast successful");

else

Console.WriteLine("Cast unsuccessful");

%��������� "�.��!���������!� ���� �����9�7������(����� ������.�'������ �""���� �����9����������07��������.���!��"��.��

70 ������B�F�0 ���

��������.��!����"���0�8� �� "� E0�8� ��@�8� �����'�������.���0���.�������������"��(��07������..��9'������ � ����������0���"������"� int i = 5;

'�� '�'���� i.ToString();

������.������.�������������� � �� ���!���A07������� "0�8�*����+���.��!���� ������*=+� ����07��������� ������������� �*+�����"� ���� �� ���!���A07������'������0�8�"�����.��!���� ������������������'���� �

Page 92: 3Programmer's Heaven C# School Book

��������������� ���������

/�

int i = 5;

Object obj = i; // implicit boxing

obj.ToString();

:���� ��� ����0�����"�����0�8� ����.��������..���"� ��� "���"� G�������'���� Object obj = (Object) i; // unnecessary explicit boxing

A ���������� "9� E0�8� ���8.������� ����� !����07�����.����������.�����!����'� ��� ���'��'� E0�8� ����.���� ��"� �� int i = 5;

Object obj = i; // implicit boxing

int j = (int) obj; // explicit un-boxing

#�(�"�' E���� �9� E0�8� ��� 0�� E�!�� "����'� � ����"���68��.��� ���� ����� ����������� ���������$������ ���!$�����%������������������!���������� � ��!����������������������������������

������������������� � �

($������ �-�!$���������������������������������������������� �.�������������������.���������������������������

+���������,��������.������

� �

/������������������� �����������%��� �.������������� ��������������($������%��������������������������������������%���

�����������������!$������%��������������������%�����������%��0�������������� ���������� �����������������������

�������������.�����"������������������������������.��������������� ���������������������������������������������� �

Page 93: 3Programmer's Heaven C# School Book

��������������� ���������

//

���&���������'�(������� �'�)������������� ��!�*�������������

������"����

��"��G��� �� ���!!�����7����.������������96 �������� 93��0������������� "$���"�������'������������� �0�� ��

&����������;������<�

���������9"� ���"� ��0���������(��'��"9��������'������07��������������������������������� ��90��'������!����'� �.��.������

�� %��������!��!�������� ���.����������"�����""�����(���� �9)���� ���9�������.�� �� %�������!�������.�9�� �����������'��������!��!��� ����.�������� ���������������

��������"� ������(� ".��"�������"0������9�����90���(� ��������.��� �� %���������� ��� �� �������*�8��.�!����� �E������ ��� �������+9!���"9�����"� "

.��.�����7����(�� ����� �� #�(�����������.�9������� ������� ������ ��������9 ���� ����0�� ������"� �� %������� ��.���� �� ���!���� �� #�(�������������.�� ��9������������.�������� ������"!������������A07������� �� � �� ���!�������� 0�������"'���� "'�������� ���� �'(��'��"�

,���!����$��!����'��(��.���(�������� �;1*!��� �+9���������0��*!��"��0��+9������@����� *!��0���+9������@���*!��0���+9��������.���� ��"����������� (�.������ �C�9������������!!���� �����"0����������� �����

2�. � ������������

%������"�!� �"7����(�����9�� ���������(��'��"���..��9� �"��'� ��..������� 9'� ��"���� �"�����.���� ���������.��� "�����'�������.�9'���.���� ����������� struct Point

{

public double x;

public double y;

public Point(int x, int y)

{

this.x = x;

this.y = y;

Page 94: 3Programmer's Heaven C# School Book

��������������� ���������

22

}

public override string ToString()

{

return "(" + x + ", " + y + ")";

}

}

����9'�����"������"������ ���"��� ����� ��� ��� �'�.�0���!���"98� "�9������.��� ����������� �!���� �� �������"� ����������.����"��.�0����� ���������� ������C����������� �!���.�� �� "'����������"���������� �*+�����"!������A07������9��������.�� ��� 0�.�� ��"������� ������ ���������#� �*+�����"� ���,�� *+�����"�

������� �� ���� ���������

%������� 0�� �� �����"� �����'���

�� <� ���� �'(��'��"� "����� ����"�!���� �E������ ��� �������� �� <� ���� �'(��'��"� "����� ������������"�!� �"�� �������� �� ��������� ���� �'(��'��"�

%'��� ��� �"�������9'��� ��.����"���� �E������ ��� �������� ������� "�!'������"��9������.����'����� ������ ������������.������.�������.����"��"�!���� �E������ ��� �������!�����������'����� ������C����!���"�!������'��������"�!����������� ���!����'� �,�� *+�����"9'�� �� �������� ��� �����0����� ��� �"�����'�� class Test

{

static void Main()

{

Point pt = new Point();

Point pt1 = new Point(15, 20);

Point pt2; // instantiation without using the new keyword

pt2.x = 6;

pt2.y = 3;

Console.WriteLine("pt = {0}", pt);

Console.WriteLine("pt1 = {0}", pt1);

Console.WriteLine("pt2 = {0}", pt2);

}

}

Page 95: 3Programmer's Heaven C# School Book

��������������� ���������

2

������.���!���.�������� pt = (0, 0)

pt1 = (15, 20)

pt2 = (6, 3)

Press any key to continue

����9'������ �� �����"�������� ��07�������!���� �*��!��� ��"0�.�+�� �� �����"�� � �'� "�"�!���� �E������ ��� �������*��.���� ��"0�������.����+'����C����"������!���"����9���!������ �*.�+�.�� ��"�*292+������� "� �*��!��� ��"0�.� +�� �� �����"�� ���� �'(��'��"� "������*"��0��9"��0��+�� �����������.�� �*.� +�� ������C�"'������.���!��"������ "���.�� ��"�* =912+� ����� �����������"�07���*��!��� ��"0�.�1+�������"'�������� ���� �'(��'��"*��(���.�����"�����.�+�$�������'�!���� ������C�"������!���"�!.�10�!����� ���*� ����� ���������#� �*+�����"+�@�!����� �������������"'��������� �'(��'��"9�����!���"���0��8.�������� ������C�"��� ��9������ �*.�1+.�� ��"����*?9;+�$�������'�'����� Console.WriteLine("pt = {0}", pt);

� ���"�!� Console.WriteLine("pt = {0}", pt.ToString());

�� ���������#� �*+�8.������� �90��� ��'�����������""� ��������� �*+�����"� ������ ������9������.����'�����.�������������������� �*+�����"'�� ���8.������� �� ��� Console.WriteLine() method.

#���.���'������.��������� ��������� "���� "� ��!��������!'�"� G�� ������C�� ��!���!���"�!��� ���� �

Page 96: 3Programmer's Heaven C# School Book

��������������� ���������

21

static void Main()

{

Point pt2;

pt2.x = 6;

// pt2.y = 3; // line 1

Console.WriteLine("pt2 = {0}", pt2);

}

������.����'����� ������ ������ G�� � G Use of unassigned local variable 'pt2'

$�'9������(����!���"�!���.�� �.������� ".����"�.�0���.��.���������������� struct Point

{

private double x;

private double y;

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

public double X

{

get { return x; }

set { x = value; }

}

public double Y

{

get { return y; }

set { y = value; }

}

public override string ToString()

{

return "(" + x + ", " + y + ")";

Page 97: 3Programmer's Heaven C# School Book

��������������� ���������

2;

}

}

$�'9���������������� � �� ���!������ �'�������� ���� �'(��'��" static void Main()

{

Point pt2;

pt2.X = 6;

pt2.Y = 3;

Console.WriteLine("pt2 = {0}", pt2);

}

����9'�����������"� � �� ���!��� �*.�1+9� ������C�"��!���"������������������.��.������ "��� �����.��"������� ��� ���������#� �*+�����"���� '���������.������.������9������.������ ������ ������ Use of unassigned local variable 'pt2'

��"�"� ������C����!���"�������.��.�����90��'���������.�����������.��� � �Q� !���9'�� � �� ����� �������'��������� �'(��'��"9'����!���� ������C���!���"�8.�������9'�������� �� �.��.������������"������� ��������� G�� �� ������ "��������'��������� �'(��'��"� �������!���"���.�0���*�������0�������+� "�8.�������� ������C�"� &� ����9���������"�!� �� �E������ ��� �������� ������ struct Point

{

public double x;

public double y;

public Point()

{

x = 3;

y = 2;

}

public Point(int x, int y)

{

this.x = x;

this.y = y;

Page 98: 3Programmer's Heaven C# School Book

��������������� ���������

2B

}

}

��� '���������.�������0���.������9������.������� Structs cannot contain explicit parameterless constructors

����9������.����������������������"�!� �.����������* �E������ �+�� �������� ������� ����������� �������������������������������"**����������������������������� ����������������+���1,��2����������������

���������"**� �����������������������������������.������� ����.���� ���������"**��������� ������������������"#���������

����������������� �������������������������������������������� ����������%�����������������������)���� ���������� ���������

�������� �

�����������@�����8#$���

��'��� �'"��� �����������!������������E��.�����������.��.������'����.������������������"������"�!�������'����������� ����������'�"�!� �"������������������� ��� � ����,�� *+�����"�� class Test

{

static void Main()

{

Point pt = new Point(19, 12);

Console.WriteLine("Before calling ChangeIt(pt): pt = {0}", pt);

ChangeIt(pt);

Console.WriteLine("After calling ChangeIt(pt): pt = {0}", pt);

}

static void ChangeIt(Point point)

{

point.x = 6;

point.y = 11;

Console.WriteLine("In the method ChangeIt(pt): point = {0}", point);

}

}

��� ������C�"������ �*.�+'���* /9 1+� "�����"��������"��� ����*+9'������"�!������������ �!������ ������� .�� ��"������ �0�!���9�!���� "� �"������� ����*+�����"�������.���!���.�������� Before calling ChangeIt(pt): pt = (19, 12)

In the method ChangeIt(pt): point = (6, 11)

Page 99: 3Programmer's Heaven C# School Book

��������������� ���������

2=

After calling ChangeIt(pt): pt = (19, 12)

Press any key to continue

���� ���������!���������.���������������!��� �"�"������ ��"� �"���������"*��� ����*++90�������� ��"�" ����!����0��(� ���������� �������9'����.�� ��"��� E��� ��"� ���,�� *+�����"�!��������� ����*+�����"�����..������!���������������������"��������.�0����

(������� ��

6 �������� ���������"�� ��.������ ����!�� "� �4490�� ��� -�������..���� �������� �� ����� ��(��'��"�6 �������� 9��(������ "��������9�������'���"�!� � �'��.��6 �������� ��.��� ��� �����! ���"�� �� ��������������"� � ���������������'�������� �� ���������"� ���������"� ��!����6 �������� ����!� ��������.���(�� �9����90���9c*�8��.����������.�+�

8 ��*���.��(������� ��

@�!���'���� �����"������!��'��"�!� �� "��� �������� 9����!����� �"��'��'� ��"� �������� � ���!���.�������..��'����"�����.� ���� "�'68.�������.��!.��������������'��������"�"�!!��� ���('������� "����� �!��������� 5��'���.�����9'�����'�����������������"����!!���I!��"��� ���0���! ���9��.�9�C�9����"�!������ "����#���'�����!� ���� ������(����� �'����������.��� �� ��!����!������������ public void Sort(string criteria)

{

switch(criteria)

{

case "by name":

// code to sort by name

break;

case "by type":

// code to sort by type

break;

case "by size":

// code to sort by size

break;

case "by date":

// code to sort by modification date

break;

default:

throw new Exception("Invalid criteria for sorting passed...");

}

}

Page 100: 3Programmer's Heaven C# School Book

��������������� ���������

2?

�!���������������.��� �����!���I!��"��0� ���9'�.����!� ���� ���� �H0� ���H��(����� Explorer explorer = new Explorer();

// some code goes here

explorer.Sort("by name");

����68.��������� ����!�������� ��� � ��������*+�����"�%'�.������� ������H0� ���H���������*+�����"9�����.������ �"����'����c���0���(� "'�����(��..��.���������� *�������0� ���+�@��9�!���� �'����� explorer.Sort("by extention");

�� explorer.Sort("irrelevant text");

���.������'�������������.���"0����'�������'� �8��.��� ���� ����9'�����! ��������.��.����9������������.������*��'����������0����8��.��� � ������� ���� �)���� �'97���� �"���������.������'��� ���8�����.��.����� "����������!����������� �������.��"��.�����������������*+�����"+����������"0���������"������(�������������.���E����� ��"��������"��� E���������.��6 �������� .����"���������� !�������.��!�.��0����

B� ���(������� ��;����<�

6 �������� ���"�!� �"� ���� ����� ��(��'��"����� ���������"� ��!���* ���"�� �� �+����.�����"�� �������G9G� enum SortCriteria

{

ByName,

ByType,

BySize,

ByDate

}

����9'�����"�!� �"� � �������� ���"������������9'������ ��� !����� �� ��@�$���9@���.�9@���C�� "@������$�'9'��� ��"�!��������*+�����"������.����������������.�� �������� � ��� public void Sort(SortCriteria criteria)

{

switch(criteria)

Page 101: 3Programmer's Heaven C# School Book

��������������� ���������

2�

{

case SortCriteria.ByName:

// code to sort by name

Console.WriteLine("Files/Folders sorted by name");

break;

case SortCriteria.ByType:

// code to sort by type

Console.WriteLine("Files/Folders sorted by type");

break;

case SortCriteria.BySize:

// code to sort by size

Console.WriteLine("Files/Folders sorted by size");

break;

case SortCriteria.ByDate:

// code to sort by modification date

Console.WriteLine("Files/Folders sorted by modification date");

break;

}

}

$�������'�"�" �������"�!���������� ���'����c���90����� �'�����.��0����.�� E����'�"��������������0�����(���� �� ��� �����������.����'�������(����������������.��������$�'9� ��"���������������*+�����"9'� ��"��.�� � �� ���!� � �������� �!��.�������������9'������ � ����(�� .��!����!�������'�"!����������� 9�� ��.����"� �����.�����������(!�������������������� �'�����������*+�����"�� Explorer explorer = new Explorer();

explorer.Sort(SortCriteria.BySize);

%���� �������9'��� ������� � �� ���!������������� "��� .����� ������ �������*+�� Explorer explorer = new Explorer();

SortCriteria criteria = SortCriteria.BySize;

explorer.Sort(criteria);

��� '����.���� "�� ����0���.������9'������!����'� ����.��� Files/Folders sorted by size

Press any key to continue

Page 102: 3Programmer's Heaven C# School Book

��������������� ���������

2�

� ��������� ��9����� "�!� ��� ���!��� �������� ��(�"���!���'��(*�� "��9,� "��9c+9 ����!����� ��*-� ����9&�0�����9c+9�� ����� ��.�*�����9<��+9!����.� ��"�*)��"A ��9�����A ��9)��"�����9%..� "+9����

/��������(������� ���

6 �������� � ��� ������� ���������������.��� ����"�!!��� � ���"�� �� ������ "����� ���.��!� � �������� �� 0�� �� ��������.�*����� �90���9���+����"�!������.��� ��� !���9'�� '�"������� � �������� 9������������ �"�������� ��������������� �!���C������ ��9� ���������������� �������� 9���������!@�$����29���������!@���.�� 9���������!@���C��1� "���������!@������;����� �� �������6 �������� "���0��(��� � �����������0��..��� �� �..��.��������� static void Main()

{

SortCriteria criteria = SortCriteria.ByType;

int i = (int) criteria;

Console.WriteLine("the integral value of {0} is {1}", criteria, i);

}

����9'�������"� � �� ���!������������* ���"��������+9��.�E����"����� �� "��� .�� ��"���� ������ ���������#� �*+�����"�������.���� the integral value of ByType is 1

Press any key to continue

:���� ��!���������.���������� ������������!�������������@���.�� �%�� �������'�� '�.�� ��"���� �������� �"� ��!���*��������+9��.�� ��"��� ���"�� �� �*@���.�+� " ���������* +����� ���"�!� ���������!�����6 ���������"� ��!����8.�������'����"�!� � � �'� �������� 9��(�� enum Temperature

{

BoilingPoint = 100,

FreezingPoint = 0

}

$�'9'�� '������.�� ����������!���������.��������@��� ���� ������.��������&���C� ���� �9��'���"�.��������� �"������ " �����"�!���������*����� �!���C���� "� ����� �� �0�� ���������+� int i = (int) Temperature.BoilingPoint;

Console.WriteLine("the integral value of {0} is {1}", Temperature.BoilingPoint, i);

����"�.����

Page 103: 3Programmer's Heaven C# School Book

��������������� ���������

2/

the integral value of BoilingPoint is 100

Press any key to continue

�� �"�����!����'� �� �������� enum Days : byte

{

Monday = 1,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday,

Sunday

}

����'�����"�!� �"� � �������� !������$�������'���� �"���������! ��,� "����� ��9���� �����������!����������� �� �'���"0��������� ������!��� *�����91!�����"��9;!����" �"��� "�� +�%��9 �������'�.���!��"���� "����� ���.�!������ �������� *���+��0���� ���"�!���"�!����� ���.��$�'9������.����'��������0�����.�����.��� �"��� ��� ����� enum Days : byte

{

...

}

� ���,�� *+�����"9'��� .�� ����� ������������!"���� static void Main()

{

int i = (byte) Days.Tuesday;

Console.WriteLine("the integral value of {0} is {1}", Days.Tuesday, i);

}

������.���� the integral value of Tuesday is 2

Press any key to continue

&� ����9� �������� ����!�������.���������������"� "����"� ������(� ".��"����������"0���(� ����.��!��������*0������+�

Page 104: 3Programmer's Heaven C# School Book

��������������� ���������

2

)������������� �� ���*���

,������� ����� �� ��� ��������.� �0������!�.���������'�� '���� ��� ���"��"�� ����$��� ���� �� ���$��.����"����' 3��0���������������� ����������������3��0��������������.������������ � ���0��(���� "����'E.������������"� "(��.����(�!���� E��!��� ��"�07���*�07���������� ��� �����!��� ��"0�� ���!��� ��+0����(� ������G"�����07���G����3��0�������������� ��(�"0�����$��)� �������������� ������� "���������"�����07���!���������� @�!�����E������ �����������!� ��07���*0�!��������� �����07���!���������+9������0�������������������&� ���C�*+�����"��"��������*"����"� ��� B+�!����07���9��������'� �07�����!��������������� ��9�������".����"����"��������*'����� ��� ����������"����A07������G&� ���C�*+�����"+� ���������!���'� ���!������� E�� ���"����������"0������07������ �����&� ���C�*+�����"������"0����3��0������������0�!�����E������ �����07���G������9'� ����( �'�8�����'�� ��'���0������"������0������"� �� ���9�!���� �07����!�� "������ ���!��� ������9�������9'�� ����#) ��"����E���������������:������.��� ������.���� �������.��0��� ���!���� "������"�����.��*+�����"�!���'� �������'�������!���������.���!�*0������ ������.��*+�����"+'�� ��������������.��"0�����07�������"0�!���"�%�������'����� G����"����"� ���!���9�'�������� �8��.���!��.���� �� �������.��0��� ���!���9����'�������������������"J�����!�� � ����.��������������� "�����������0��(� "���"������� �!������0�����!�������'���� ���!���� using System;

namespace CSharpSchool

{

class Test

{

static void Main()

{

GarbageCollection garCol = new GarbageCollection();

garCol.DoSomething();

garCol.Dispose();

}

}

class GarbageCollection : IDisposable

{

public void DoSomething()

{

Console.WriteLine("Performing usual tasks...");

}

public void Dispose()

{

GC.SuppressFinalize(this);

Page 105: 3Programmer's Heaven C# School Book

��������������� ���������

Console.WriteLine("Disposing object...");

Console.WriteLine("Freeing resources captured by this object...");

}

~GarbageCollection()

{

Console.WriteLine("Destructing object...");

Console.WriteLine("Freeing resources captured by this object...");

}

}

}

� ����0����8��.��'�����"������"���� ���"3��0������������ '������.���� �������.��0��� ���!���� "������"�����.��*+�����"�$�'9������������ ��!���3��0������������ ����� "���"�'�� ��!���E�.�������������"0�����07���90������ ������.��*+�����"��������� �����!�������"��� �����"� ���������G,�� *+�����"�$�������'������"�����..��&� ���C�*+�����"�!���������3����� �����.��*+�����"� public void Dispose()

{

GC.SuppressFinalize(this);

Console.WriteLine("Disposing object...");

Console.WriteLine("Freeing resources captured by this object...");

}

���������3������ 0���"���� �������3��0��������������!'�.��������� ��07���� ���3����..��&� ���C�*+�����"9���&� ���C�*+�����"*��"��������+�!�������� ��07���'� G�0������"��!'��8���������0���.������9'�'���������!����'� ����.��� Performing usual tasks...

Disposing object...

Freeing resources captured by this object...

Press any key to continue

@��9�!'������ ������������������.��*+�����"� ���,�� *+�����"� static void Main()

{

GarbageCollection garCol = new GarbageCollection();

garCol.DoSomething();

// garCol.Dispose();

}

Page 106: 3Programmer's Heaven C# School Book

��������������� ���������

1

����� ����'���"����������������*��&� ���C�*+�����"+'�� ����07���� ��� �����!��� ��"� $�'������.��'���"0���(����� Performing usual tasks...

Destructing object...

Freeing resources captured by this object...

Press any key to continue

2�������������"��.��������3�� ���

���� ��"����������&� ���C�*+�����"� ����������������.��!���� ���������"9�����!���9���������� "�"�������"� ��"����������� ��������� �������������"� �� E�� ���"���������(�!����� "��9"���0���� ��'��(�� ����� �

&#�����)��������;<���� �

���������3����.����"������������" ���"�������*+'����� !�������3��0������������ ������������'�������������0���������������� ������� �!���3��0������������������$��)� ����� "��� ��'���������3��0������������.����7��!��!���� �����07���� �������@��9����������������"'�� ������ ��0���!�07���� ����.���������� E��!��� ��"*6���9� ���������������� +� "���'� ������������0���E������"� �� ����

*������������� �����

��!��9'������ ���� ��.E���������*����� ��0�� "0� ���.���+� �����.����������� ���"�!� � ���"����� ���$���"����*��������"� ������+���"�!� �"� �"�� ��������9��(����� using System;

namespace CSharpSchool

{

class Test

{

static void Main()

{

Outer.Inner inner = new Outer.Inner();

inner.InnerFun();

}

}

Page 107: 3Programmer's Heaven C# School Book

��������������� ���������

;

class Outer

{

private static string name="Faraz";

public Outer()

{

Console.WriteLine("Constructor of Outer called...");

}

public class Inner

{

public Inner()

{

Console.WriteLine("Constructor of Inner called...");

}

public void InnerFun()

{

Console.WriteLine("InnerFun() called...");

Console.WriteLine("Name in Outer is {0}", name);

}

}

}

}

����9'�����"�!� �"� ���"��� ���"� ��9�� ��� �"� �"������.E��������A�����@������A�������� "���� ��������� �E������ ��� ������������������.E����������� � ��0����(�"�.�0������ ��� ��9 ���"������ 0����(�"'���������������"�!����.������9.�0���9.�������"9� ��� ��9� ��� ��A).�������"9'������������ � ��"����0�"� ��� =�#�(������������0��9���"�!����������"�!���!�� ���"�����.������9'������� ������� ���"����� ��0���!��� ��"� "�����"����"������ ��� � ������ ��"������!��� ��� "� �� ��������� ������ ���,�� *+�����"�!���������9'����(���.�0���� ���.�������������'����E ��� ����� ���"���������'�������"� "� �� �����"'�����!��� ����������� ��� ����� ���� ����������������� '��'�� �� �����"���� ������ ���,�� *+�����"�� static void Main()

{

Outer.Inner inner = new Outer.Inner();

inner.InnerFun();

}

Page 108: 3Programmer's Heaven C# School Book

��������������� ���������

B

% �������.���� �.�� ��0��� ���"��������������� ����������*.������9� ��� ��9.�������"9.�0���+��������0���!���� ���� �����������"��� �����"���� ���� ��&� *+�����"�!���� �����9'��������������.���������0��* ���+�!���� ���� �*A����+���� public void InnerFun()

{

Console.WriteLine("InnerFun() called...");

Console.WriteLine("Name in Outer is {0}", name);

}

��� '����.���� "�� ����0���.������9'�������!����'� ����.��� Constructor of Inner called...

InnerFun() called...

Name in Outer is Faraz

Press any key to continue

����������� ������ ������������"#���������������)���������������������������������������������������%��)��������!�������������

�����������"#�� �

�� ����� ���"�������� �� �����"'�����!��� �������� ���� ����9���������.�������� �������'������� ����� ���J����������!���� ���� ������� ��9�!�������������!���� ���� ������ ��� ��� "�������������!��� ���"����.�0���9��� ���"���'���"� ��0������0��� �������� ����0��*.��7���+�

Page 109: 3Programmer's Heaven C# School Book

��������������� ���������

=

,��-����������������!������.�����

������"����

��"��'�'����8.�����0���������� "� ���!������'����8.��������"��0��� "�0����������"9�0���������9� ���!���� "��'���������.���� ��"� ���#����'�'�������'�������� "!���� ���!�����!��� ��0��� ������ "��.�������

-����������������

%0����������� 0���.��"�!� �"�� ���.���������%0����������� ��� � �������� ���.���������"�����"�0����������"�����0��������� ��.����"������ �������"��������� �!����0����������"� "����������.���� ����� �!���������"��"�����"���0E�����%0����������"� "�0���������������(�"'�������0�����(��'��"�% �0������������!���0����(�"'�������0�����(��'��"� �� ���0������������� ���.����9������ ��0�� �� �����"��������0��0E����"� ��"�����������!� ���� ����������������� '��� �0���������� G�0�����"�%���� ������ �� �0�������������.���� ��������0����������"� ����0��������9����������0�"������"�� �0��������� %���� ������ �� �0��������� "��.���� �� �������0����������"������"����� ���������!����0������������� "���������!��� ���!�����.��!�0��������� "���� .�� �������07����!���������������� ������"����0���������#���"������� �0��������'����'��� �����.��.������ "� � ���.����*�0�����+�����"� abstract class TaxCalculator

{

protected double itemPrice;

protected double tax;

public abstract double CalculateTax();

public double Tax

{

get { return tax; }

}

public double ItemPrice

{

get { return itemPrice; }

}

}

Page 110: 3Programmer's Heaven C# School Book

��������������� ���������

?

�����8��������������� ��� �'�!���"����������� "�..���"��8����� ��� � �0����������"�����������8*+'������������������8�..���"� ������������� "������� ���!���"��8���������������8*+�����"���"��0����������� ������0E������ .����"�������' ��������!���..��� ������8� ������������������������ ��� �'�.�0������"� ��.��.�������"����������'�.������!���"��!'������� �� ���������0��������� ���,�� *+�����" static void Main()

{

TaxCalculator taxCalc = new TaxCalculator();

}

������.����'������.��� �Cannot create an instance of the abstract class or interface

'CSharpSchool.TaxCalculator'

� ��"����������� � �� ���!�����8�������������9'� ��"���0E������#��� �'� ���������!�������0�������8������������������ ���������8����������� class SalesTaxCalculator : TaxCalculator

{

public SalesTaxCalculator(double itemPrice)

{

this.itemPrice = itemPrice;

}

public override double CalculateTax()

{

tax = 0.3 * itemPrice;

return itemPrice + tax;

}

}

���������8�������������� ����������8����������� "������"��������������8*+�����"����..���;2[��8� ���.�����!� ����*�0������O+� "����� ��� �'.�����!�����������������8����������������"�!� ���� �������������(�������������.����������!'�"� G�.����"������.���� ����� �!��������������8*+�����"� ������8���������� class SalesTaxCalculator : TaxCalculator

{

public SalesTaxCalculator(double itemPrice)

Page 111: 3Programmer's Heaven C# School Book

��������������� ���������

{

this.itemPrice = itemPrice;

}

/* public override double CalculateTax()

{

tax = 0.3 * itemPrice;

return itemPrice + tax;

}*/

}

��'����������.������������� 'CSharpSchool.SalesTaxCalculator' does not implement inherited abstract member

'CSharpSchool.TaxCalculator.CalculateTax()'

$�'9� E����� ����������""� �����������8*+�����"� ������8������������� ��'�����������""� �������������8*+�����"�!��8����������� ���������8�������������9'��� ��������� �� ��� ���,�� *+�����"� class Test

{

static void Main()

{

SalesTaxCalculator salesTaxCalc = new SalesTaxCalculator(225);

double newPrice = salesTaxCalc.CalculateTax();

Console.WriteLine("The item price has changed because of sales tax from {0} $ to {1} $",

salesTaxCalc.ItemPrice, newPrice);

Console.WriteLine("Tax applied = {0} $", salesTaxCalc.Tax);

}

}

����9'������ �� �����"���������8�������������7����(������������� "�����"�����0���������.���!����0���.������'���0�� The item price has changed because of sales tax from 225 $ to 292.5 $

Tax applied = 67.5 $

Press any key to continue

���� ������ �0����������.�*��8����������+��!��� ������ "��� �07����!���� ��������*������8����������+� ���,�� *+�����"�

Page 112: 3Programmer's Heaven C# School Book

��������������� ���������

static void Main()

{

TaxCalculator taxCalc = new SalesTaxCalculator(225);

double newPrice = taxCalc.CalculateTax();

Console.WriteLine("The item price has changed because of sales tax from {0} $ to {1} $",

taxCalc.ItemPrice, newPrice);

Console.WriteLine("Tax applied = {0} $", taxCalc.Tax);

}

���� "�������� ��� ����������'�'� �!�������0�������8�������������9��� ������.����"����"�!� ���� �!���0����������"������� ������� ��������*����������������������+�!����0�������8�������������� class WarSurchargeCalculator : TaxCalculator

{

public WarSurchargeCalculator(double itemPrice)

{

this.itemPrice = itemPrice;

}

public override double CalculateTax()

{

tax = 0.5 * itemPrice;

return itemPrice + tax;

}

}

��������������������������� 0���"��������� ���,�� *+�����"�

�����.�����

� ���!�������.�����(� "�!��.�� ��9��"��"�!� ����.���!������ *� �����!�����"�� �����+��������"0�!����'�"0����0E��.�� % � ���!����"������"�� ����� ���!���(��'��"�� ���!���9��(��0���������9�� ��0�� �� �����"�% � ���!����� �� ��� ��� ������!��������"9.��.������ "� "�8���% � ���!�������.�'������0��������.�0���� "�0�����0�"�!����� % � ���!������.���� ��"0������%�����.���� �� ����� ���!������.����"����0�"�!�����������0���!���� ���!��������.���� �� � ���!���9������������� ��8�������"!��� ������ ���%���� ����"����'�����������.���� �� ��.���������� ���!����

Page 113: 3Programmer's Heaven C# School Book

��������������� ���������

/

%����� ��.���� �������� � �� ���!���9�� ����������E� ������ ��'��������� � ������ ��� ����� % � ���!�������!�� � ����������� ���!�������� "�����������!��� ���!���� ���!�����.�� "���� .�� ���� ������.���� �� ����� ���!���������� �� ��� � ��9��.��!�8��� ����!� ���!���'����..�����G�G��(����.��0��9��������C�0��9���� ��0��9�6 ��������9���� #���"�!� �� � ���!��� ���"��� "�'�� interface IWindow

{

Point Position

{

get; set;

}

string Title

{

get; set;

}

void Draw();

void Minimize();

}

���� ���!����0���"�!� �� �'��.���� "�'����� ���!����� ��� �'�.��.�����'������� "��0���(!����������� � "������!����� "�'�������� ��� ����� ������!�'������"�����'*+� "��,� ���C�*+���'� "�'�$������������� �!����������0��"��������� ����'������ ���.���� ������$�'9��������������'������.���� �*�������C�+���� ���!������.����"����0�"�!���������0������.��.���������� ��!��.���� �9'�������������.�� ""�!� �"� ���.�������� struct Point

{

public int x;

public int y;

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

}

Page 114: 3Programmer's Heaven C# School Book

��������������� ���������

12

�����.���� ���� "���� "����� � ���!���"������� �0�������.�� ".����"����.���!������ '�������0�!����'�"0���������.���� �� ������������.����� !�������.���!������ � ""�� �����.���� ��� ��������'����� ��������� ���!���90��"�� ����.���� �*.����"����0�"��!+���������0���!���� ���!���� $�'����"�!� ��)���� ������� "�'���������.���� ������� "�'� ���!���� class RectangularWindow : IWindow

{

string title;

Point position;

public RectangularWindow(string title, Point position)

{

this.title = title;

this.position = position;

}

public Point Position

{

get { return position; }

set { position = value; }

}

public string Title

{

get { return title; }

set { title = value; }

}

public void Draw()

{

Console.WriteLine("Drawing Rectangular Window");

}

public void Minimize()

{

Console.WriteLine("Minimizing Rectangular Window");

}

}

���)���� ������� "�'�����.���� ������� "�'� ���!����� �������� �.������� class RectangularWindow : IWindow

Page 115: 3Programmer's Heaven C# School Book

��������������� ���������

1

�� ��)���� ������� "�'���.���� �� ������� "�'� ���!���� "� ��"������"�� �0��������9��.����"����0�"�!�����������0���!������ "�'� ���!�������� ��� �'�.������!���"������� ".����� ����.��.����������� "������ .���!��"� ������ "�'� ���!��������.���� ��"�� ����������������� ".����� !���"��.�������������'������"9���'*+� ",� ���C�*+9�����.���� ��"��.�� ����.�������� $�������'������������.���� �� ����0��� )���� ������� "�'���"������"�.�0���9������� ��"������".�0���� ������ "�'� ���!���� �!'�"� G����(�������0���.�0���9��(� void Draw()

{

Console.WriteLine("Drawing Rectangular Window");

}

��'������������.���E����������'CSharpSchool.RectangularWindow' does not implement interface member

'CSharpSchool.IWindow.Draw()'.

'CSharpSchool.RectangularWindow.Draw()' is either static, not public, or has

the wrong return type.

������.�����J�����..����������� "��������.��0������ !������������ !���9�'�����"�������9���������0���!���� ���!�������0������ ".�0���0�"�!����� "'��� ��"��������������0�����������!�������� �����0��"��� �.������.���9�� ��'����"���������������0���.�0���'�� ��.���� �� �� � ���!���� %�� ������������� �������"�(��'��"'�� ������"� �����0����������"�!���� ���!���*��� "�'+� �����.���� �� ����*)���� ������� "�'+9��(�'���"������� ��"��� �.������.�����!���'����������"�0�!�������0��9���'������������.������������� G�����.�������)���� ������� "�'����'*+G� �����0�������"!�� "��������"�� ������� !�� ���..��� ����������"�(��'��"���������'�"� ����������������"�� �"�!������.���� ����� 90��.����"�����' .���!����.���� ����� !��������0���

��$������ ���/���8 ������������.����

%����� ��.���� �������� � �� ���!����� �������9��������.����"������.���� ����� !�����������0���!�����!�����.���� �� �� ���!������..��'������'�� ���!����#����0��� "�&���9��(����� interface ILoggable

Page 116: 3Programmer's Heaven C# School Book

��������������� ���������

11

{

void Log(string filename);

}

interface IFile

{

string ReadLine();

void Write(string s);

}

����#����0��� ���!����� ��� �� ��������"#��*��� �+'�������������������� ���.���!��"!���'�������� ���!����&����� ��� �'������"�)��"#� �*+� "�����*��� �+���������")��"#� �*+���"��� �!������!���� "��������"�����*��� �+'��������..���"��� ������!����#���"�!� �����*,�&���+'������.���� ������ ���!���� class MyFile : ILoggable, IFile

{

private string filename;

public MyFile(string filename)

{

this.filename = filename;

}

public void Log(string filename)

{

Console.WriteLine("Logging activities in file: {0}", filename);

}

public string ReadLine()

{

return "A line from MyFile";

}

public void Write(string s)

{

Console.WriteLine("Writing `{0}' in the file", s);

}

}

,�&�����.���� �0�������#����0��� "����&���� ���!���� "��������G9G���.������������!� ���!����

Page 117: 3Programmer's Heaven C# School Book

��������������� ���������

1;

class MyFile : ILoggable, IFile

���������������.���� ����� �!��������".���!��"� ����'���.���� ��"� ���!���*�#����0��� "�&���+�,�&�������� ��� ��� ���������������.���� ����!���!�������)��"#� �*+� "�����*+�����"�.������ ���!���'��� �����..���"� ����� �����������#��*+�����"����������������!���.���������.���!��"�����"������������ ��� � ����,�� *+�����"���.���� ��"�� class Test

{

static void Main()

{

MyFile aFile = new MyFile("myfile.txt");

aFile.Log("c:\\csharp.txt");

aFile.Write("My name is Faraz");

Console.WriteLine(aFile.ReadLine());

}

}

����9'�����������"� � �� ���!,�&���� " ���"���&��������� ����"�!!��� ������"�!������,�&����� ����07���������.�����.���!���.�������� Logging activities in file: c:\csharp.txt

Writing `My name is Faraz' in the file: myfile.txt

A line from MyFile: myfile.txt

Press any key to continue

<.�����.�� �9���������"0� ��� !��� �0�����.���� �� ������.��� ���!���90��'����!��� ����!�����"� �#����0��'������*��� �+��������� #��*��� �+Q������,�&���'���"��� 0���.���� �� ��'�� ���!���*�#����0��� "�&���+� "0�������������"�����*��� �+'����������� ������ interface ILoggable

{

void Write(string filename);

}

interface IFile

{

string ReadLine();

void Write(string s);

}

Page 118: 3Programmer's Heaven C# School Book

��������������� ���������

1B

(0$� � �� �$�������� ��.���� ��

�!�������.���� �� �������� � �� ���!���� "�������'��!�������������"'����������� �����9��� '��� .����"��8.�������.���� ����� �!���.��������������"0�.��!�8� ��� ���'������ ����!���� ���!���� "�����.��������� �"��������"�!� �"�0���'����'������'�� ���!���*�#����0��� "�&���+� "0����� ��� ������*��� �+�����"'����"� ������� ������#��G �'��� ��������,�&��������"� �����.����"��8.�������.���� ����� �!��������*+�����"� class MyFile : ILoggable, IFile

{

private string filename;

public MyFile(string filename)

{

this.filename = filename;

}

void ILoggable.Write(string filename)

{

Console.WriteLine("Logging activities in file: {0}", filename);

}

public string ReadLine()

{

return "A line from MyFile: " + filename;

}

public void Write(string s)

{

Console.WriteLine("Writing `{0}' in the file: {1}", s, filename);

}

}

����'�����"�!� �"��������*+�����"�!�#����0���8.�������0�.��!�8� ���'������� ���!��� ���� void ILoggable.Write(string filename)

$�'!��������.����9����'������*+�����"���"��� �����0��� ������"�!� ���� �$�'9�!'�'�������,�� *+�����"� static void Main()

{

Page 119: 3Programmer's Heaven C# School Book

��������������� ���������

1=

MyFile aFile = new MyFile("myfile.txt");

aFile.Write("c:\\csharp.txt");

aFile.Write("My name is Faraz");

Console.WriteLine(aFile.ReadLine());

}

'�'���"������!����'� ����.��� Writing `c:\csharp.txt' in the file: myfile.txt

Writing `My name is Faraz' in the file: myfile.txt

A line from MyFile: myfile.txt

Press any key to continue

$�������� 0��������������������*+�����"9�����.���������� *�&���������*��� �++��8�����"�����������#����0��������*+�����"9'���������.�E�������07���� �����!��� ���!����#����0��� ���!���� ILoggable logger = aFile;

log.Write("c:\\csharp.txt");

�� ���#����0������.��� ���.��!,�&���*�,�&�����.���� ��#����0��+9� �07����!��.�,�&���*�&���+�� 0���.�����������"������#����0����!��� ��*������+������"�!��",�� *+�����"'���"��� 0�� static void Main()

{

MyFile aFile = new MyFile("myfile.txt");

aFile.Write("My name is Faraz");

ILoggable logger = aFile;

logger.Write("c:\\csharp.txt");

Console.WriteLine(aFile.ReadLine());

}

% "������.��'���0�� Writing `My name is Faraz' in the file: myfile.txt

Logging activities in file: c:\csharp.txt

A line from MyFile: myfile.txt

Press any key to continue

���� �������� ����.������ ��� ��������$�������

� �������8��.��9'�( �'��������07���*�&���+����E�0��������#����0��� ���!���9����� �'��������!��'��"�@��9� ����'����'���� �����'�������.����������07�������������E�0�������� ���!���

Page 120: 3Programmer's Heaven C# School Book

��������������� ���������

1?

��.����� E����9'��� ��(����!����� "�����.������*����� "��.���������"����"� ����"������ ��� =E� ������ ��� "�������.���+� �����.���������"������(�����.��!�.����������07���� "������� �0����� ������&���8��.��9 MyFile aFile = new MyFile("myfile.txt");

Console.WriteLine(aFile is ILoggable);

����.�� ������ ����� "��"���.��� ��,�&������0E��.��!�#����0��9'���� string s = "faraz";

Console.WriteLine(s is ILoggable);

����.�� �&���������.����������� ����!����07���� �����E�0���A����'��9���������07��������.���!��"��.��&���8��.��� MyFile aFile = new MyFile("myfile.txt");

ILoggable logger = aFile as ILoggable;

if(logger == null)

Console.WriteLine("un-successful cast");

else

Console.WriteLine("successful cast");

������.��'���0�� successful cast

�����!��� Test t = new Test();

ILoggable logger = t as ILoggable;

if(logger == null)

Console.WriteLine("un-successful cast");

else

Console.WriteLine("successful cast");

������.��'���0�� un-successful cast

&� ����9���'���� ��� ���������� ������.��!���0��������������0���!� ���!����� ����� ���!�����!��� ���

Page 121: 3Programmer's Heaven C# School Book

��������������� ���������

1�

-�� ����.���� � �� � ������������ ����.�����

% � ���!����� � �����!���������� � �� ���!������..������&���� ���!���� ������'������� ���!����)��"�0��� "������0�� interface IWritable

{

void Write(string s);

}

interface IReadable

{

string ReadLine();

}

interface IFile : IWritable, IReadable

{

void Open(string filename);

void Close();

}

���� ���!���������0���� ��� ������"�����*��� �+����'��������..���"��� ������!���9'�������� ���!����)��"�0���� ��� ������")��"#� �*+��������� ���� �!������!�������� ���!����&���� ���������'�� ���!���*������0��� "�)��"�0��+� "����� ��� �'������"*A.� *��� �+� "����*++���.� � "�������!����#��� �'"�!� ����� ���",�&���������.���� ����� ���!����&��������������.����"����0�"��!�����������".��� �*"���������� ������"+� ����&���� ���!���� class MyFile : IFile

{

private string filename;

public void Open(string filename)

{

this.filename = filename;

Console.WriteLine("Opening file: {0}", filename);

}

public string ReadLine()

{

return "Reading a line from MyFile: " + filename;

}

public void Write(string s)

Page 122: 3Programmer's Heaven C# School Book

��������������� ���������

1�

{

Console.WriteLine("Writing `{0}' in the file: {1}", s, filename);

}

public void Close()

{

Console.WriteLine("Closing the file: {0}", filename);

}

}

������.����"����.����.���� ����� �!�����������"0�.�� �� �������� ���0�"��!���������"� ����������� ��� � �,�� *+�����"�� class Test

{

static void Main()

{

MyFile aFile = new MyFile();

aFile.Open("c:\\csharp.txt");

aFile.Write("My name is Faraz");

Console.WriteLine(aFile.ReadLine());

aFile.Close();

}

}

����9'�����������"� � �� ���!,�&���� " ���"�����!��� ���&���������������"�!!��� ������"� ����07����� ������!��� ��� ������.���!���.�������� Opening file: c:\csharp.txt

Writing `My name is Faraz' in the file: c:\csharp.txt

Reading a line from MyFile: c:\csharp.txt

Closing the file: c:\csharp.txt

Press any key to continue

������.����'���������!��������"�������������"� ��"���

Page 123: 3Programmer's Heaven C# School Book

��������������� ���������

1/

���-���#�'������� ���!�&�� ���/�� $���� ��

������"����

��"��'�'����8.���������9��������� � "��� ��� �.������ � ���&����!���9'�'����8.���������"��� �� ������� ������ "7����"��������'�������8.������'!������������������������������ ���� '�'������������������ � "����'���������.���� ��"� ���#����9'�'����8.����"�!!��� ���������� ��(�%����#��9����(9F����� "������ �����&� ����9'�'�������'��� ������ �.�����"� �����'����8.����0��������� �� "������� �@���"����.��

-���#��9�3 � ���

%'������� �������9� ��������J�� ������������� �!����� ��!�������"�����.��� ��9� ������� �07���� "������!��� ����.�9� "�����!��������������"� ������.�������� ��������"� ���"��� �� ������ ���.��������� 9 �''�'����8.���������"��� �� ��������

/��� ���� ����-���#�� �

%�����"��� �� ��������� G������!�����G�%�����"��� �� ������������ �� '������������� ��!���������� ���������!��������������0��� �"���0��'��������.����������� �*��'+����������� �!��� "�������� �*����� +��!������ "�������� �"� ���� ��� ���������� �!���������� �9��������"�1E"��� �� �������*����������� ��.��!�����"��� �� �������+9�����'����������"� E"��� �� �������'���� ����"�.���!������� �!���������������'���.��!�����"��� �� �������� ���

�� )���� ����������*� �� '����������'�� ��� � �J��� ��0���!����� + �� -����"�����*� �� '����������'"�� �� ���������� ��� � �J��� ��0���!����� +

��������0���'��''������"�!!��� �(� "�!��������(��(�����!����������'���� "�8��!"�!!��� ������ ��!���������)����0��9���!�������� ��!� ��������'��C���*2+�

Page 124: 3Programmer's Heaven C# School Book

��������������� ���������

;2

��� �� � ��� ! �" ��

��� �� � ��� ! �" ��

��� � �� �!� ! �! ��

��� �! �� ! ��� �� ��

��#� �� "" " � �� �� �

��� �� � ��� ! �" ��

��� � �� �!� ! �! ��

��� �! �� ! ��� �� ��

��#� �� "" " � �� ��

" �## ��!

����� � ! � ��� ��"

���� �! �� �� !

#��!� ��� !# ��!

��������������� ����� �

�������������� ������ ���� ������ ����������

�������������� ���� �������� �

�������������� ����� ���������������

����$�������

����$��#�

����$�������

����$�������

����$�����#�

����$�����#�

����$�����#�

����$�����!�

����$���������� ����$����������

����$����������

����$��������������$����������

Page 125: 3Programmer's Heaven C# School Book

��������������� ���������

;

������� �� ������������ ���� �����������.����� ���� ��������#��

)���������'�� �� ��������� ���"��� �� ���������(����� int [] intArray = new int[5];

����0����� �'���"� �� �����*������+�� �"��� �� �������*� �%����+�!��.�� �9� "'���=����� ������ ������������ ��!����������(����� intArray[0] = 45; // set the first element to 45

intArray[2] = 21; // set the third element to 21

intArray[4] = 9; // set the fifth and last element to 9

� �� ����� �������"��� �� ��������������"� �����������0���.����"���9��� �����(��.������0���"�!� ���� �!��������"��� �� �������� �� "'������G������"��� �� ��������� ������!�����G���..��'�'������������'�"��� �� ������� ����������'���1��'� ";����� ����� � �� ��������������!����'� int [,] myTable = new int[2,3];

%���������� ��!���������������E� ������C�"�������"�!���������P�� ������������� ��!�������0�������'���"0�� ������C�"'���C��������� ������������������������ ��������!���������.���!�����.� foreach(int intVal in myTable)

{

Console.WriteLine(intVal);

}

��� ������.���"� "�8�����"9��'���.�� ��8*18;+C���� 0

0

0

0

0

0

$�'9������� �����������!���� "���"�������� ��!�������������� �����������!���!�������� ��!���!�����'��;19'��� '�������!����'� ���"�� myTable[0,0] = 32;

Page 126: 3Programmer's Heaven C# School Book

��������������� ���������

;1

� ������'��'��� ��� �����������!���������� ��!��������� myTable[0,1] = 2;

myTable[0,2] = 12;

myTable[1,0] = 18;

myTable[1,1] = 74;

myTable[1,2] = -13;

$�'9'��� ������.���! ���"!�����.���������������������� for(int row=0; row<myTable.GetLength(0); row++)

{

for(int col=0; col<myTable.GetLength(1); col++)

{

Console.WriteLine("Element at {0},{1} is {2}", row, col, myTable[row,col]);

}

}

����9'�������"�'�!�����.���������������������!����'�"��� �� �!�����������������"���3��#� ���*+�����"�!���������%�������*���� "����� ����!�������� �$��+��!� "����� ����!�.���������"��� �� �!� ������$����������#� ���.��.����'������������ ��0���!����� �� ����'�"��� �� �������9����9?�������.���!����0���.������'���0�� Element at 0,0 is 3

Element at 0,1 is 2

Element at 0,2 is 12

Element at 1,0 is 18

Element at 1,1 is 74

Element at 1,2 is -13

������� �� ������������ ���=�����-���#��

%7����"������� �� '��������� ����!������'� ���������&���8��.��'����'�������������0��'���;��''��������� ����!���!�����'�;9������ "��'�=� "�������"��'�1����� � �� ��������7����"�������(����� int [][] myTable = new int[3][];

myTable[0] = new int[3];

myTable[1] = new int[5];

myTable[2] = new int[2];

Page 127: 3Programmer's Heaven C# School Book

��������������� ���������

;;

��� '��� !�������������(����� myTable[0][0] = 3;

myTable[0][1] = -2;

myTable[0][2] = 16;

myTable[1][0] = 1;

myTable[1][1] = 9;

myTable[1][2] = 5;

myTable[1][3] = 6;

myTable[1][4] = 98;

myTable[2][0] = 19;

myTable[2][1] = 6;

$�'9'�'�����'��'�������!���������.�������������� ��!��������� foreach(int []row in myTable)

{

foreach(int col in row)

{

Console.WriteLine(col);

}

Console.WriteLine();

}

�����"��0����������.��� "������ "���� "�0�����.��(�"�.������'*'������ � ������+� "��� �������"������������''����.�� �� ������!������� �������.���!����0�����"�'���0�� 3

-2

16

1

9

5

6

98

19

6

Page 128: 3Programmer's Heaven C# School Book

��������������� ���������

;B

Press any key to continue

� ������'��9'��� ��������E"��� �� �������� int [,,] myTable = new int[3,2,4];

myTable[0,0,0] = 3;

myTable[1,1,1] = 6;

A�� 7����"�����!���� � int [][][] myTable = new int[2][][];

myTable[0] = new int[2][];

myTable[0][0] = new int[3];

myTable[0][1] = new int[4];

myTable[1] = new int[3][];

myTable[1][0] = new int[2];

myTable[1][1] = new int[4];

myTable[1][2] = new int[3];

myTable[0][0][0] = 34;

myTable[0][1][1] = 43;

myTable[1][2][2] = 76;

����9'�����������"������"��� �� ��7����"���������� ������!�'�1E"��� �� �����������!����!���1E"��� �� ��������� ��� 1��'������ ����!���!�����'�;9'��������� ����!��� "��'�B�� �������!���� 9������ "�'�"��� �� ������������ ������C�"�� ���� "9'����������"����!�������� ��!��������� "��� �"����"�!!��� �������%�������������"��� �� ��7����"��������J����"�!!�������.�������P�������0�������!��� ������ ���.��8.��0����%��� 9���(��������"�� !��� � �����"��� �� ����������.������������� G������!�����G�

&���� ��� �$������$ ������������� ���� ��������#��

� ����8��.��'�7���'9'�������"�����"��� �� ��������!� ������ �������.��@��9����� "������������!� �"�����.��&���8��.��9������"�!� �� ������!��� ������ � ������!�07����!�����' ���� string []names = new string[4];

:���� � ������C��������� ��!� ������ ���!��� ���!����'� �'��

Page 129: 3Programmer's Heaven C# School Book

��������������� ���������

;=

string []names = new string[4]{"Faraz", "Gates", "Hejlsberg", "Gosling"};

�� string []names = new string[]{"Faraz", "Gates", "Hejlsberg", "Gosling"};

�� string []names = {"Faraz", "Gates", "Hejlsberg", "Gosling"};

:���� ����.��������"��������� � "� ������C���� �!� �������!��� ��� "��������� string []names;

names = new string[4]{"Faraz", "Gates", "Hejlsberg", "Gosling"};

:���� ���� ������C��'�E"��� �� �������� ���!��*��� �'������"��������� + string [][]nameLists = {new string[]{"Faraz", "Gates"},

new string[]{"Hejlsberg", "Gosling", "Bjarne"}};

�����!���������.���� �.��.�����D�����"������ 0��..���"� ���������

�� #� ���������� ��0���!����� �� ���"��� �� �!� ����� �� 3��#� ���*� �+������� ��0���!����� �� �.���������"��� �� �!� ����� �� 3��<..��@�� "*+��������..��0�� "�!���.���!��""��� �� �!� ����� �� 3��#�'��@�� "*+���������'��0�� "�!���.���!��""��� �� �!� �����

8 ��.���� ��$�

������0�� �� ����!���������.!��J������� ����� �'�#�������'��'��(� "��'�� '�� �0����������0��������"����0����!���������.�&�����9'� ��"����.���� �����6 �����0��� ���!���'������ ��� � ���� ��������"93��6 ��������*+9��������� � �07����!��.��6 �������������6 ��������� ���!����� ��� � �.�0���.��.����*����� �+� "�'�.�0��������",���$�8�*+� ")���*+��������� �.��.�����"������"� ����6 ��������� ���!����� object Current { get; }

������� �������� ������ ��!������������ '������!�07���"�����.�����,���$�8�*+�����"�"������"�� bool MoveNext();

Page 130: 3Programmer's Heaven C# School Book

��������������� ���������

;?

���"�� ���������� �������� ����� �8������ ��!������������ � "����� �����!����"�� ���� ������!��9� "!����!������������ ��� "�"���� ,���$�8�*+������"!�����!�������9�������������� �����!�������� �� ������������ '������� ������������ �.��.����� ������"� ���,���$�8�*+��0�� �8�����"!�����!�������� &� ����9���)���*+�����"�"������"� void Reset();

��������"�������� ��������� "���������� ����������%!�������9,���$�8�*+'������� �"�� ������������ �����!�������� �� ������������ � $�''�'�����'��'����(����������� 0��������"������ ����!���������.90���.���� �� �����6 �����0��� "�6 ��������� ���!���� using System;

using System.Collections;

namespace CSharpSchool

{

class Test

{

static void Main()

{

MyList list = new MyList();

foreach(string name in list)

{

Console.WriteLine(name);

}

}

}

class MyList : IEnumerable

{

static string []names = {"Faraz", "Gates", "Hejlsberg", "Gosling", "Bjarne"};

public IEnumerator GetEnumerator()

{

return new MyEnumerator();

}

private class MyEnumerator : IEnumerator

{

Page 131: 3Programmer's Heaven C# School Book

��������������� ���������

;�

int index = -1;

public object Current

{

get { return names[index]; }

}

public bool MoveNext()

{

if(++index >= names.Length)

return false;

else

return true;

}

public void Reset()

{

index = -1;

}

}

}

}

����'�����"������"���������",�#��'������ ��� �.������ ���"��� ���",�6 ���������������,�6 ����������.���� ��6 ��������0�.����"� ������.���� ����� �!��.�0���.��.����� "�����"�������,�#����.���� �����6 �����0��� ���!���� "����� � �07����!��.�,�6 ��������� ��3��6 ��������*+�����"�������,�#���� ��� ������������!��� ������" �������,�6 ������������������������������������������ ����������0��� "�8��(��.����(�!�������� ������ ��!������������ ���������0��� "�8�� ������C�"'���E �6����������,���$�8�*+�����"������"9��� ����� ���0� � "����� ������!���9"�.� "� �� '��������������� G!� ������� ���0�� ������"��������� �.��.��������� ���� "�8������� ��!������������ '�������)���*+�����"������������0��� "�8��E ���� � � ���������'������ �� �����"���,�#������ "�������"����������� ��!���������.90�����'�������.���� ��"�6 �����0��� ���!���� ���,�#������������.���!����0���.�������� Faraz

Gates

Hejlsberg

Gosling

Bjarne

Press any key to continue

Page 132: 3Programmer's Heaven C# School Book

��������������� ���������

;�

������ ���

%�������'��� ��(���������� �!������"�07����� ������9�������������������� '�� �� ������!����������� �����C��!� ��������'��!�8�"� "���0�"�!� �"����������!� �� ������ �!� ���������� "��9� ������� � ���� ��� �07����!������"�����.�9'����'� ��"��"�!� �����������!��� �� ������ �%��9� �����"�� ����.��� �.�������������� ��!��� ���� �� "�������� ��������� ��!���������� �&�����.��.��9�����������!��� "����$��&����'��(���#�0����*&�#+����.����"�"� ��0���!��������������������� �!"�!!��� ���.�������������.��� �� ������������������ ���.���� �����!����������� ����!������ ���.������� ��� �����.��� %����#�� �����"����������� ��������� �����90���������'"� ������������ ��0���!

����� ���� �������( %��������� ����'��(� ���#��� &���A��*#�&A+.�� ��.��9����9����������� ����"�

���!�������������"!��������������� �F���� %��������� ����'��(� ���&���� &���A��*&�&A+.�� ��.��9����9���!�������� ����"

����!�������������"!��������������� ������0�� �����"����������� �!(��E�����.�������������� �C�"0��"� ��������"��!���(��������"#�� �����"����������� �!(��E�����.���'������������������"�����"� ������(������

������������0��0�0������(��� "���� "�8�%���!����0���������.���� �������������� � ���!���9'������ ��� �����.��.������ "� ������"�

�� ������ �.��.��������� ��� ��0���!����� �� ������������ *�����������#� ���.��.�����!� %����+

�� ������ ���� �C�".��.��������� �0����� �����"�.� "� �� '������������������������ ������"E�!��� ��

�� ����� �)���.��.��������� � �07��������� 0���"��� ���� �C������������������� � �� �����.���*%���������9� �� "�8+�����"��.���������� ��!������������ ����������9����� �

!������.���!��"� "�8� ��

%�������������� ���������.���� �����6 �����0��� ���!���9������� 0��������"������ ����!���������.�

8 ��-���#� ���������

������������������ �%����#�������������������90���� ��������� ��!� �"�����.����"� G� ��"��.���!�����C��!������������ '�� �� �� %����#��*�'���"��"�� �������!��.�������+�����C��!���%����#�����'"� ������������ ��0���!����� ����� ��� ��� ���% %����#����� ������ ��� ����� "� ������C����C�'����"�!��������������"��.������%��� ��0���!����� �� �������"������9%����#���"7�������.������!�������������"� ���0���(� �� �'������ "��.�� ������"�����

Page 133: 3Programmer's Heaven C# School Book

��������������� ���������

;/

� ����������C��!���%����#����������� ��0���!����� ����������������.��� �� ��'���������.��������� ��0���!����� ����%����#���� ���"'������� �� ����� �� �'������% %����#���� 0��� ������"��(����� ArrayList list = new ArrayList();

���� ���.���!����� �������.������!���%����#��0�.�� �� � ����������������� �������� ArrayList list = new ArrayList(20);

���� ���������� %����#��'�������������������� 0�.�� ������������� � ����� �������� ArrayList list = new ArrayList(someCollection);

���""����� ������%����#��0��� ���%""*+�����"����%""*+�����"��(�� �07����!��.��07������.��������� list.Add(45);

list.Add(87);

list.Add(12);

���'����""�������� ��0�������%����#���$�'9'��� ���������������������� ���%����#��*���+�� ��!���������.� static void Main()

{

ArrayList list = new ArrayList();

list.Add(45);

list.Add(87);

list.Add(12);

foreach(int num in list)

{

Console.WriteLine(num);

}

}

'����'���.�� ������������ �� ���%����#��� 45

87

12

Press any key to continue

Page 134: 3Programmer's Heaven C# School Book

��������������� ���������

B2

����������� �)���� �����������%�������������� � ����������������������+��������� ��������,�������������������������������

������� +,����� ���������$�������������������������.�����'����������������������������"#�$�������������� �

��������������� �������������$�������������������������.���������������� �

���%����#������������.���� ��"���� "�8��.��.����*��� "�8�.������+'��������'������� ���0������"�� ����MN�.������97������"�'������.�������*'�'�������'����.���� �� "�8��� ��� �8���� +����!����'� ���"��������������0�����"�0�������� "�8���������������� ��!���%����#��� static void Main()

{

ArrayList list = new ArrayList();

list.Add(45);

list.Add(87);

list.Add(12);

for(int i=0; i<list.Count; i++)

{

Console.WriteLine(list[i]);

}

}

������.���!�����"�'���0������������� �.��� ��".�������������0�����"������.��.������� ���!� "�������� � ��0���!����� �� ���%����#���)���������%����#��� ��������.��.����*��� �+!�����.��� �� ���!������������� � %����!����������.���� �.��.������ "�����"�!���%����#������.��� ��"� ���!����'� ���0��� ���.������,����" �����.��� ��.����� 3��������� ��0���!����� ����%����#���� �� ��� ���� � 3������8��� ��0���!����� �� ���%����#���%""*�07���+ %""� ����� ������� "�!� %����#���)�����*�07���+ )������ ����� �!������%����#���)�����%�*� �+ )������ ����� ������.���!��"� "�8!������%����#���� ���*� �9�07���+ � ���� �07���� ���%����#�������.���!��"� "�8������*+ )���������������� �!������%����#���� ��� *�07���+ )���� �0����� ������ "����� �'���������%����#���� ��� ����..���"

����� ��� �����.���*+ ��.���������� ��!���%����#�������������..���"��.�����������������"

��������"�"� "� ��� .���!������ ����0���.��"� "���!���'����� "�8�!����������.�����"�����

� "�8A!*�07���+ )���� ���C���0��"� "�8�!���!���������� ���!����07���� ���%����#����!

Page 135: 3Programmer's Heaven C# School Book

��������������� ���������

B

����07���� ��!�� "� ���%����#��9������� E �#��� "�8A!*�07���+ )���� ���C���0��"� "�8�!������������� ���!����07���� ���%����#�����%����*+ )���� � ������!��.��07��������� ��� ����������� ��!���%����#�����������C�*+ ��������.���������������� ��0���!����� �� ���%����#���

8 ��&���:�������

������������������ �����(�����(� "�!��������� ����.����"��� ������"������������� ��%���('��(� ���.�� ��.���!#��� &���A��*#�&A+9'������� ��������������� ����"� ��������('���0����!���������0�������"!����������(� "F���������������� "������������ ���.������� ��� "���������.���� ��"� 0������"'���� "�!�'�������� ����� �!� ����� ��������(������"��G���G'���������� �� ����!���������(������"�G��.G��!�������� ��������"0��� �����"!��������.�!������(9��� ���������"�G���(G�.������ ������������������� �����(���.����"����!� ���� ������!�����(� ����$��� ���� �� ���������(����� 0�� �� �����"� ��� �������������� �'���"!�����%����#��� Stack stack = new Stack();

����0���*"�!����+�� �������'���� ������C�� �'��.�����(����!����'� ��� �����������'���� ������C�������('�������..���"� �������.������ Stack stack = new Stack(12);

��������!����'� ��� �������'���� ������C��������('�������..���"��������� � Stack stack = new Stack(myCollection);

$�'9'��� .������� �� ���������(�� �������*+�����"9��(����� stack.Push(2);

stack.Push(4);

stack.Push(6);

� ��������� ��9'��� ������������� �!���������(�� ������.*+�����"�%���.����.����������.���;����� �� ��������(� "��� .�.�����!!� �0�� ��.��� ��"0���'� using System;

using System.Collections;

namespace CSharpSchool

{

class Test

{

Page 136: 3Programmer's Heaven C# School Book

��������������� ���������

B1

static void Main()

{

Stack stack = new Stack();

stack.Push(2);

stack.Push(4);

stack.Push(6);

while(stack.Count != 0)

{

Console.WriteLine(stack.Pop());

}

}

}

}

$�������'�������"�'����*+���.���������������������������� ��!������(�A ���� ��������0��� �������!����(����������.*+�.������ ��� ������� �������� ��������.�!���(90����������������.����� �������� ������'���"������'���������.*+�.������ �������.���!����0���.������'���0�� 6

4

2

Press any key to continue

�������������"� �������(�����������������������!� %����#���8��.�!��������(*+�����"�������(*+�����"����� �����.����� ��!������('������������ �������!����'� �.������"��� �����������!������(*+�.������ � static void Main()

{

Stack stack = new Stack();

stack.Push(2);

stack.Push(4);

stack.Push(6);

Console.WriteLine("The total number of elements on the stack before Peek() = {0}", stack.Count);

Console.WriteLine("The top element of stack is {0}",

stack.Peek());

Console.WriteLine("The total number of elements on the stack after Peek() = {0}", stack.Count);

}

Page 137: 3Programmer's Heaven C# School Book

��������������� ���������

B;

����0���.������.������������� �� ��������(� "��� .��(�������.����� �� ������(����.������.�� ���� ��0���!����� �� ������(0�!���� "�!���������(*+�.������ ����������!���.�������� The total number of elements on the stack before Peek() = 3

The top element of stack is 6

The total number of elements on the stack after Peek() = 3

Press any key to continue

������.���!���.��������'�������(*+"�� ���!!������ ��0���!����� �� ������(� ""�� �������������.����� �9�� ������������.*+�.������ �

8 ��G�����������

%F����'��(� ���.�� ��.���!&���� &���A��*&�&A+9'������� �������!�������� ����"� �����J����'���0����!�������������"!��������G6 J����G� �������� ������ �����J����9� "��������!� ����!������J����������"G��J����G�#�(�����(9�������������(�.������ 9'������������ ��������"0��� �����"!������!�� ��!���J����� ������������������ �F�������.����"����!� ���� ������!J����� ����$��� ���� �� �����F����G�� �����������������������!���%����#��� "�������(� // an empty queue

Queue queue = new Queue();

// a queue with initial capacity 16

Queue queue = new Queue(16);

// a queue containing elements from myCollection

Queue queue = new Queue(myCollection);

���!����'� �.������"��� �����������!F����� ��� static void Main()

{

Queue queue = new Queue();

queue.Enqueue(2);

queue.Enqueue(4);

queue.Enqueue(6);

while(queue.Count != 0)

{

Page 138: 3Programmer's Heaven C# School Book

��������������� ���������

BB

Console.WriteLine(queue.Dequeue());

}

}

���.������� J�������������� �� �����F����� "��� "�J���������� ��'�������.�������.���!���.�������� 2

4

6

Press any key to continue

������.����'�������J��������������� �����"������'���� ����"��������������"�!�F������������������������!���%����#��� "����(�����

2 �� ��� ���

������ ��������(� "�!��������� ������������� �(��E�����.���!���� �6��������� ������������ ��"� ��!��"0���(���%�����(��� ������������ ���� �J��� "������� ��0�������� � �(��'��������� �������������������6 ������ �����"����� �����(����A8!��"������ ���'��������'��"*(��+���������.� "� ���� � �*�����+�����'��������� ��.��!������ ����� ������������������ ���.��������������0��� "��������"#���

8 ��1�� ������������

�����0�����������(��E�����.����6�������*�������+� ��������0���� �J�����"� ��!��"0���(���%�����0���������(��� "��������� �07�����.��,���������� �������"����(��� ������0��90������� ��� �����������(�����'����90�!��������� �����!�����(��90������������"����6J���*+� "3�������"�*+�����"������� �����!�������07��������������

�� 6J���*+����(!��� �� ���J��������������� ���"�!������!��� ���J������� �� 3�������"�*+����� ������� �����!��������� �� ���!������� �� ������������� �"0�3�������"�*+������ ��"����0���"0��'�� ���,� 5����� "���,�85����

�!���� �������.��

�������� �����1�� ������

������ �� "����!������������.����"�"� ���@�����#�0����"��� �"���������9� "���������������0��!��������(��� �����0���������"����� ��������������� ��� �������������0����� �� �����������0��������.����� Hashtable ht = new Hashtable();

Page 139: 3Programmer's Heaven C# School Book

��������������� ���������

B=

�������"�!���� ������� ��� ��������%�����0���� ���0��� ������"0�.�� �� ���� �������.������ Hashtable ht = new Hashtable(20);

��������0���������� ��� ���������� �������'��������'������ ������C���������0��'�������������������� ��"����� ����

- ��� ���������1�� ������

A ���������� �� �����"������0���07���9��� ����� �""���������� ���%""*+�����"� ht.Add("st01", "Faraz");

ht.Add("sci01", "Newton");

ht.Add("sci02", "Einstein");

9��� �3 ��� �����.���� ��1�� ������

����'������ ����"���������� ����������0����� �'��������(���% �.��������������� 0���������"�� ���(��� Console.WriteLine("Size of Hashtable is {0}", ht.Count);

Console.WriteLine("Element with key = st01 is {0}", ht["st01"]);

Console.WriteLine("Size of Hashtable is {0}", ht.Count);

����'�������"���� "�8��*MN�.������+������������������!�����������0������'���!���������"�� ���������������� �!�����������0��0��7������� ����07���'������.���!��"(��������!���9����C�0�!���� "�!����������������.������ ���'�����*�����;+�������.���!�����"��0����� Size of Hashtable is 3

Element with key = st01 is Faraz

Size of Hashtable is 3

Press any key to continue

9��3 �����$��� ������ ����

�������� ��!��������0���� 0�������"0��� ����)�����*+�����"'������(����(���!���������0�������"��������� �� static void Main()

{

Hashtable ht = new Hashtable(20);

ht.Add("st01", "Faraz");

Page 140: 3Programmer's Heaven C# School Book

��������������� ���������

B?

ht.Add("sci01", "Newton");

ht.Add("sci02", "Einstein");

Console.WriteLine("Size of Hashtable is {0}", ht.Count);

Console.WriteLine("Removing element with key = st01");

ht.Remove("st01");

Console.WriteLine("Size of Hashtable is {0}", ht.Count);

}

������.���!���.�������� Size of Hashtable is 3

Removing element with key = st01

Size of Hashtable is 2

Press any key to continue

)��� ���� �������� ��.�:�#�����3������

������������ �!������(��� "������ ������0���� 0���������"�� ����>��� "5����.��.����9'��������� � ���������� �� ��� � �������(��� "�������.������������!����'� �.��������������������������(��� "������ ".�� �����9�� ��!���������.� static void Main()

{

Hashtable ht = new Hashtable(20);

ht.Add("st01", "Faraz");

ht.Add("sci01", "Newton");

ht.Add("sci02", "Einstein");

Console.WriteLine("Printing Keys...");

foreach(string key in ht.Keys)

{

Console.WriteLine(key);

}

Console.WriteLine("\nPrinting Values...");

foreach(string Value in ht.Values)

{

Console.WriteLine(Value);

}

}

Page 141: 3Programmer's Heaven C# School Book

��������������� ���������

B�

������.���!���.������'���0�� Printing Keys...

st01

sci02

sci01

Printing Values...

Faraz

Einstein

Newton

Press any key to continue

� ��: ���.��� ���0 �������.���$��� ������ ���� ���� �� ������

:���� ������� ��� >��*+� "����� ��� 5����*+�����"��!� "���'�������.�������������'������.���!��"(��� "������8��� ��������0���� ���@�����������"����� �0����� ������ static void Main()

{

Hashtable ht = new Hashtable(20);

ht.Add("st01", "Faraz");

ht.Add("sci01", "Newton");

ht.Add("sci02", "Einstein");

Console.WriteLine(ht.ContainsKey("sci01"));

Console.WriteLine(ht.ContainsKey("st00"));

Console.WriteLine(ht.ContainsValue("Einstein"));

}

������.���� True

False

True

� "����� �'�������������� �� J����� �8��� ���"����� ���*�����0��+�� ���

8 ��&���� ���������

�������"�����������������������0��9���"�!!��� ��0�� �������������������"�����"� ������(���A ��!����"�� �����!�� �������"#������������ ����������� ������������ �� �� � ������ "�897����(������ '���� ������� �������!�����"#��9�!���'� ����������' �����(����� 9� �""����

Page 142: 3Programmer's Heaven C# School Book

��������������� ���������

B�

������� �"������ "����0�"� �����0��9������ ��"����(�����������������.���� ��������.���0��� ���!���� �������.���0��� ���!������ ��� ������"�� ����.�����*�07����07+���������"��(�����07�����.������� �� "����� � � �������.��� �� �'����������..���"�07�����J�����9���������� ������� �������� ��07����

�� %����� ������!2� "�������������07�����J���������..���"�07� �� %����� ��������������� C���� "�������������07�������������� ����..���"�07 �� %����� ���������� C���� "�������������07��������� ����..���"�07�

������ ����� "�����.��������"�����.�.����"�� ��.���� ����� �!���� ���!���� "�� ���� 0���"�(��� ������"#��"�������� ��������"#��.����"��������� ��������.����"�"0���������0��� "�����.���� ���C��������� ��� �������� SortedList sl = new SortedList();

���!����'� ���0����������!��.��.������ "�����"�!��������"#����� ���.������,����" �����.��� ��� � 3����� ��0���!����� �������������"#���� ��� �>�� )���� � ���������� �!������(��� ��������"#���5���� )���� � ���������� �!������������ ��������"#���%""*�07���(��9�07��������+ %""� ����� �*(��E�����.���+����������"#���3��>��*� �� "�8+ )���� ���(�������.���!��"� "�8�3��@�� "�8*� �� "�8+ )���� �������������.���!��"� "�8�� "�8A!>��*�07���(��+ )���� ���C���0��"� "�8�!���.���!��"(���� "�8A!5����*�07��������+ )���� ���C���0��"� "�8�!���.���!��"������)�����*�07���(��+ )������������� �'������.���!��"(��!�����������"#���)�����%�*� �+ )������������� ������.���!��"� "�8!�����������"#��������*+ )���������������� �!�����������"#����� ��� >��*�07���(��+ )���� �0����� ������ "����� �'��������������"#���� ��� � ����� �

'�������..���"(����� ��� 5����*�07��������+ )���� �0����� ������ "����� �'��������������"#���� ��� � ����� �

'�������..���"������

Page 143: 3Programmer's Heaven C# School Book

��������������� ���������

B/

���!����'� �.������"��� �����������!������"#��� static void Main()

{

SortedList sl = new SortedList();

sl.Add(32, "Java");

sl.Add(21, "C#");

sl.Add(7, "VB.Net");

sl.Add(49, "C++");

Console.WriteLine("The items in the sorted order are...");

Console.WriteLine("\t Key \t\t Value");

Console.WriteLine("\t === \t\t =====");

for(int i=0; i<sl.Count; i++)

{

Console.WriteLine("\t {0} \t\t {1}", sl.GetKey(i), sl.GetByIndex(i));

}

}

���.������������� ����!"�!!��� �.�������� ��� �����*� ��� �!���+�� �� �����(������ ���!�����.���"�������������(��� "������� ��� �"� ��������"#��*�+��� �����������"���9����������� ��� ��������"� �����"��"��� "'�� '������������� ���0����3��>��*+�����3��@�� "�8*+�����"9'���������"����!�����������.���!���.������'���0�� The items in the sorted order are...

Key Value

== =====

7 VB.Net

21 C#

32 Java

49 C++

Press any key to continue

&�� ���1��� ��� �����

��� �8���.��� ��"��G��� ��0����� "�� ���� �� �����.�������� ��� ������� ��9���� ���0����� � ".��������"�����.�����.��������"�����.���� ���.��������������� ���������07����!������� ����*����� �+���������0��0� ������@�������0������� ������������!����07����� ��0���� ��"0�� ��.������ ������������ '��'�� '����������<..��*+�����"� ��� �9��"�� G���� ���������� ����� �0��������� "����� � �'��� ��07������������..�������.��� ����� �!�������� ���07�����������0������� �!��� �� ����$������!������������8������ �@���"����������07����!�������������0��9�����9����������� 0���� ��"0�������.������ ��� ��9�!����������%..� "*+�����"� �

Page 144: 3Programmer's Heaven C# School Book

��������������� ���������

=2

���� �@���"������07���9��� �'��� �'���0��..� "�"��*�""�"������ "�!+�������� ���07����#��G �'"�������'������ �0�� ��

8 ����� ������������ �����������

������0�� �� ������� ����� �����!������ � ���������������������� ����!��.��.�����*��(�#� ���+� "�����"*��(�6J���*++� .��������� �����9'�'���"����0�����!�������� .��.������ "�����"�!������� ����� "��� "��� ������������� ��"�� ���.������,����" �����.��� #� ��� 3����� ��0���!���������������� ��07����� ��� ����.�����*��� �+ ���.������� �� ���!������ �'�������..���"��� �� "����� �

� ������ ���.����� �!�������.���0��� ���!�������.���*��� � 9��� �1+ ���������������"� "���.�������'��..���"��� �� ���.����� �!

�������.���0��� ���!����6J���*��� �+ )���� �����!����..���"��� ���8������������������ �9��������

!������ ���*��� �+ )���� � �'��� ����������� ���� ���� *�""���� +�!���� "����..���"

��� ��� ���*� �� "�89��� �+ )���� � �'��� �0�� ���� �����..���"��� ������.���!��"� "�8�!

������ ����.�*��� �+ �������������"����� � �'��� �����������.��!����..���"��� ��� ��� *��� �+ �������������"����� �������G��!��� ��������..���"��� �����������*��� �+ )���� �����!������ �����'�������..���"��� ��6 "����*��� �+ )���� �����!������ �� "'�������..���"��� ��� "�8A!*��� �+� "�8A!*������+

)���� ���C���0��"� "�8�!���!���������� ���!����..���"��� ����..���"��������������������"��������"�"� "��������� ���������0���

#��� "�8A!*��� �+#��� "�8A!*������+

)���� ���C���0��"� "�8�!������������� ���!����..���"��� ����..���"��������������������"��������"�"� "��������� ���������0���

)�.����*����9����+)�.����*��� �9��� �+

)���� � �'��� �0���.���� ����������� ���!���!�������'��������� "����*��!������ �'��������� "��� �+�

�.���*.��������MN+ �"� ��!�������0��� �*� ���� �� ��+'�������"�������"0�� ����������������.���!��"� � �����9��� .��������0��� �� ������� ������� "����� ���

��0��� �*� �� +��0��� �*� ��19� ��;+

)���������0��� ��!������ ������ �!������� "�8.����� � �������� "� ���!����������"�"!����� ������ "�������"�"!���9��������������0��� ������ �!������� "�8�1� "'��������� ����!�;����������

������%����*+ )���� � ������!����������!������ ����<..��*+ )���� ���.��!������ �� �..������

Page 145: 3Programmer's Heaven C# School Book

��������������� ���������

=

��#�'��*+ )���� ���.��!������ �� ��'����������*+ )���� � �'��� �0������� ����������� ���!����!.���!��"���������

!������0��� � �� "��� "�!���� �� ������������*+ )���� � �'��� �0������� ����������� ���!����!.���!��"���������

!������0��� � ��!���� �� �������6 "*+ )���� � �'��� �0������� ����������� ���!����!.���!��"���������

!������� "�!���� �� ���� ���!����'� �.������'�����"��� �����"������!����!����0��������"�!������ ��������.�������J������!E�8.�� ������ "� ��� ���"���������"����� "����������� using System;

namespace CSharpSchool

{

class Test

{

static void Main()

{

string s1 = "faraz";

string s2 = "fraz";

string s3 = "Faraz";

string s4 = "C# is a great programming language!";

string s5 = " This is the target text ";

Console.WriteLine("Length of {0} is {1}", s1, s1.Length);

Console.WriteLine("Comparision result for {0} with {1} is {2}", s1, s2, s1.CompareTo(s2));

Console.WriteLine("Equality checking of {0} with {1} returns {2}", s1, s3, s1.Equals(s3));

Console.WriteLine("Equality checking of {0} with lowercase {1} ({2}) returns {3}",

s1, s3, s3.ToLower(), s1.Equals(s3.ToLower()));

Console.WriteLine("The index of a in {0} is {1}", s3, s3.IndexOf('a'));

Console.WriteLine("The last index of a in {0} is {1}", s3, s3.LastIndexOf('a'));

Console.WriteLine("The individual words of `{0}' are", s4);

string []words = s4.Split(' ');

foreach(string word in words)

{

Console.WriteLine("\t {0}", word);

}

Console.WriteLine("\nThe substring of \n\t`{0}' \nfrom index 3 of length 10 is \n\t`{1}'",

s4, s4.Substring(3, 10));

Page 146: 3Programmer's Heaven C# School Book

��������������� ���������

=1

Console.WriteLine("\nThe string \n\t`{0}'\nafter trimming is \n\t`{1}'", s5, s5.Trim());

}

}

}

������.���!���.������'��������� �����.���� "���� "���0��������!�������0��� Length of faraz is 5

Comparison result for faraz with fraz is -1

Equality checking of faraz with Faraz returns False

Equality checking of faraz with lowercase Faraz (faraz) returns True

The index of a in Faraz is 1

The last index of a in Faraz is 3

The individual words of `C# is a great programming language!' are

C#

is

a

great

programming

language!

The substring of

`C# is a great programming language!'

from index 3 of length 10 is

`is a great'

The string

` This is the target text '

after trimming is

`This is the target text'

Press any key to continue

8 ��&�� ��7� ����������

�����������8������ �@���"������������������������������� ����'������"�!!��� �������������0��P�����9���� ��� �������!���07����� 0���"�!��"0����.������ �< ��(�� ������ ����9������!������������ ��������!����� �@���"����� �� ��������07���� string s = "This is held by string";

StringBuilder sb = new StringBuilder("This is held by StringBuilder");

Page 147: 3Programmer's Heaven C# School Book

��������������� ���������

=;

���� �@���"������'�����������%����#��� "�������������� � ���'�����������'������������������C��!������ ����� ��� ��� ����� ��9�����.������!����� �@���"�����0�"�!!��� �!�����#� ���������!������������ .��.������ "�����"�!������� �@���"������������"� ���!����'� ���0��� ���.������,����" �����.��� #� ��� 3����� ��0���!�������������������� �@���"���07����� ��� ���.����� 3���������� ���.������!������� �@���"���07����%..� "*+ %..� "������ ���.��� ����� �!���.���!��"�07��������� "�!���

���� �@���"��� �� �����������"��� ��0���!�������"�"!����� ���*+ � ��������� ���.��� ����� �!���.���!��"�07��������.���!��"� "�8�!

������� �@���"���07����)�.����*����9����+)�.����*��� �9��� �+

)�.�������������� ���!���!����..���"���������*����� �+'��������� "�..���"���������*����� �+� ������� �@���"���07����

)�����*� ��9� ��� ���+ )�����������������!������� "�8.����� ��!.���!��"�� ���� �������� ����� �@���"���07����

6J���*���� �@���"��+ ����(����..���"���� �@���"���07���'������� �� ��� "����� �����!0�������"� �����P�����'��9������� !����

���!����'� �.������"��� �����������!����!���������"� using System;

using System.Text;

namespace CSharpSchool

{

class Test

{

static void Main()

{

StringBuilder sb = new StringBuilder("The text");

string s = " is complete";

Console.WriteLine("Length of StringBuilder `{0}' is {1}", sb, sb.Length);

Console.WriteLine("Capacity of StringBuilder `{0}' is {1}", sb, sb.Capacity);

Console.WriteLine("\nStringBuilder before appending is `{0}'", sb);

Console.WriteLine("StringBuilder after appending `{0}' is `{1}'", s, sb.Append(s));

Console.WriteLine("\nStringBuilder after inserting `now' is `{0}'", sb.Insert(11, "

now"));

Console.WriteLine("\nStringBuilder after removing 'is ' is `{0}'", sb.Remove(8, 3));

Console.WriteLine("\nStringBuilder after replacing all `e' with `x' is {0}",

sb.Replace('e', 'x'));

}

}

Page 148: 3Programmer's Heaven C# School Book

��������������� ���������

=B

}

% "������.���!���.�������� Length of StringBuilder `The text' is 8

Capacity of StringBuilder `The text' is 16

StringBuilder before appending is `The text'

StringBuilder after appending ` is complete' is `The text is complete'

StringBuilder after inserting `now' is `The text is now complete'

StringBuilder after removing 'is ' is `The text now complete'

StringBuilder after replacing all `e' with `x' is Thx txxt now complxtx

Press any key to continue

$�������� ������9�������� ���07����!������� �@���"�������� ���"�!��"9�� ������ �@���"���07����������0�����.���"�����������0������ ��07����

Page 149: 3Programmer's Heaven C# School Book

��������������� ���������

==

%��(0��$� ��1��� ���

������"����

��"��'�'����8.�����8��.��� �� "�� ������ ��� �����'����������0����(� �������"��0��� "�8��.��� � "'�������'"�!!��� ��8��.��� ����������#����9'�'����8.����"�!!��� �!������.����"�"� "�� ���� ��..���"0���������.����� �8��.��� �� "�� ��&� ����9'�'������� ��'��"�!� �����' ������8��.��� �

(0��$� ���7�� ���

�����.��9�8��.��� �� "�� �������� ������ "���� E���������� �$6�����'���"�����'�����������!�'���������� ���.�����������

8 ������.��(0��$� ���

�� �"�����!����'� ���.����"�� using System;

namespace CSharpSchool

{

class Test

{

static void Main()

{

double p = Divide(3, 5);

Console.WriteLine(p);

}

static double Divide(double a, double b)

{

double c = a/b;

return c;

}

}

}

�������"�*+�����"����� ���������!���"����� �!����'� ��0��.��"�.���������������.���!���.������'���0�� 0.6

Page 150: 3Programmer's Heaven C# School Book

��������������� ���������

=?

@��'����!��������"����"�*+������"� ���,�� *+�����"� double p = Divide(3, 0);

�����'���0� ����.�������������0������.��������������E�0����"��0���@��'�� ���.��������8�����"9������.����'��������.���"���"�;0�C���'����� ��.��0���%������9���.������'�������OA �'����"���'����������8.�����������(������ ".��������� "�!���C�������� ����������� "������������� static double Divide(double a, double b)

{

if(d == 0)

{

return 0;

}

double c = a/b;

return c;

}

�������� ���"�'���0�� static void Main()

{

double p = Divide(3, 5);

if(p == 0)

Console.WriteLine("Error in input");

else

Console.WriteLine(p);

}

����'������ ������GC���G��� "������ ������ ���� .���@�����.��0��������C����� ����!0����������!���"����� ��(�*2"���"�"0�B+� "� ����������������������� !��"'�������������� �"������� �������������������9���� static void Main()

{

double p = Divide(0, 3);

if(p == 0)

Console.WriteLine("Error in input");

else

Page 151: 3Programmer's Heaven C# School Book

��������������� ���������

=�

Console.WriteLine(p);

}

� ���������.������'���"�.�������������� ������ ���� .��9'������������ �������������'"�'��������.��0���Q

(0��$� ��� ���������*���

���.������'��(�"� ���.��0���� ""�����.�"������� � ���!����!G68��.��� G��������������"�!� �� "����'� �8��.��� � �������!� �8.����"��� ��@���'��������.���� �.�� ��0����8��.��� � "�8��.��� �� "�� ������ ��� ��� "�$��

�� %���8��.��� � �$������07�������������68��.��� �������0�����!������8��.��� � �$���% ������"�� ����������'� �8��.��� � �������!���� �8.����"��� �"��� ����8������ �� ��������'(��'��"� ����������' �8��.��� �� 0���������"����'���� �������� ������"�� ���������������0���(�

�� �����"������������'� �8��.��� '����'�'� ����� "���.��� ������0���(����������"� �����.��������� �8��.��� ������"����� "���������' �8��.��� �.����"� ��������0���(7���!���������0���(����������"������ �����8��.��� ����� "�!� �'����.�������������!�8��.��� '�'� ���"���� ��������0���(0��� ��� � ���� ����!�8��.��� �!�����������(��'��",����.�������0���(�� 0�"�!� �"!���� ������0���('�������������0���('���������.�������������!�8��.��� �

�� �����"����������'��0��8�����"�!����������������������0���(�.����"� ���!� ����0���(9.����"7���!����������������������0���(������"������� ���"����'��0��8�����"'����������8��.��� ������� ���

�� ��� � �8��.��� �����""��� �����8������ �!�����"�� �"�������0���(9�������� � ���"�� ������0���(� �������"� "����� �����!�8������ ���� !����"�������.�������������!� ����0���(�

�� �� ���8��.��� ���.��� �� �$�������� "�07���9����!����'���� ������ ������������������ �����!���'����������0���(���� "���0������8��.��� 9��'������������������ "������!���0E���G�8��.��� �%����.�� ��������� ��!����0E���G�8��.��� �8.��������!������.��� �����8��.��� '�����������.�������������

�� ���!� ����0���(��.��� ���68��.��� �� "�� ���J��������0� ���� �!��������������������9��������������!� ������������!� ����0���(�

�� �!���"� �������� �8��.��� 9����� ����� ���� �� �*����� #� �����)� �������#)+'����������� ����0����!9���� �����.������������� ����

����������� �3��)���� ����������������������������������%� ��$�����������"#�����������$�����������������������

������%� ��4�������������������������%���� ������������"#��'����������������%� ��$�����������"#����5�����������������

����������������������"#����������������������������� ���������%� ��$������������$��������� ��� �������� ����������

���������"#���������������������� �����������6��������������������������������������4�.�������� ������ �����������"#�

������������������������� � �����%� ��$�����������"#�� �

Page 152: 3Programmer's Heaven C# School Book

��������������� ���������

=�

������ ����������������������������������� ������ ���������������

1��� ���(0��$� ����� ���� ����#������� ���. ����#����:��

B���.�� ����#������� ����:�

%��.��"��� ������ �!������!��������������0���(����� 0���'� static void Main()

{

string s = null;

try

{

Console.WriteLine("In try block... before calling s.ToLower()");

Console.WriteLine(s.ToLower());

Console.WriteLine("In try block... after calling s.ToLower()");

}

catch(NullReferenceException e)

{

Console.WriteLine("In catch block...");

Console.WriteLine("NullReferenceException Caught");

}

Console.WriteLine("After try...catch block");

}

������ �GG� ,�� *+���� �"� ������������ '������.������������#�'��*+�����"� ��� �����!��� ��� ����� ���������#� �*+�����"9����#)*����� #� �����)� ����+'����������$���)�!��� ��68��.��� ��� ��'������ ����"��������������#�'��*+�����"� ����0���(9���)� ����'��������!��������0���('������ ���������8��.��� � "9�!� ��!�� "9����8������ '���7��.����������0���(����� ��8�!��������0���(���.���� ���� "���� "�� .��� �����!�����������9���!��� ��*G�G� ������+�!����������8��.��� ����"������"*$���)�!��� ��68��.��� � ������+���� ����0���.��������8�����"9������.���� In try block... before calling s.ToLower()

In catch block...

NullReferenceException Caught

After try...catch block

Press any key to continue

#��(����!������������.���!���.������� "���.�����'�������������"��������������#�'��*+�������$���)�!��� ��68��.��� �%������9����8������ �!�������� � �.����!������0���(��� ���"� "���.�������8������ ���� !����"����������0���(�)����0���������$���)�!��� ��68��.��� �����"'�� '�

Page 153: 3Programmer's Heaven C# School Book

��������������� ���������

=/

�����.�������������0���!������ �� �����!��� ���#����� �������"��0����0��� "��� � �07����������!��� ��GG string s = "Faraz";

try

{

Console.WriteLine("In try block... before calling s.ToLower()");

Console.WriteLine(s.ToLower());

Console.WriteLine("In try block... after calling s.ToLower()");

}

catch(NullReferenceException e)

{

Console.WriteLine("In catch block...");

Console.WriteLine("NullReferenceException Caught");

}

Console.WriteLine("After try...catch block");

�� �����.������"�� ������� ��8��.��� ��0�����"9����8������ �!.������'��������� ��� In try block... before calling s.ToLower()

Faraz

In try block... after calling s.ToLower()

After try...catch block

Press any key to continue

(0��$� �������H�/����������&���:8�����"�$��� ���

$������������"�� "����������0���("�" G�����8�����"0������!����0� ���!$���)�!��� ��68��.��� �$�'�����������������������0���(9��� �� ����'�"�"� ���!���.����9� "��'�����..� � static void Main()

{

string s = null;

Console.WriteLine("Before printing lower case string...");

Console.WriteLine(s.ToLower());

Console.WriteLine("After printing lower case string...");

}

Page 154: 3Programmer's Heaven C# School Book

��������������� ���������

?2

��� '����.���� "�8���������0���.������9'������!����'� ����.��� Before printing lower case string...

Unhandled Exception: System.NullReferenceException: Object reference not set

to an instance of an object.

at CSharpSchool.Test.Main() in

c:\documents and settings\administrator\my documents\visual studio

projects\myconsole\class1.cs:line 11

Press any key to continue

�� ��'�"�" ����������$���)�!��� ��68��.��� 9���.�������������� ���".����������'�������� ����*�#)+��.���� ��'���� �� �68��.��� ,��������"����0�����8��.��� 1�����(������!��������!�8������ ������������������!!� ���� ����'��������"����8��.��� �� ����������'��������8��.��� �����"0����,�� *+�����"�!����������� ��� �"� ��������.������ ���.���*�����.�����������,�� *++��������(��������.�� �������!��� ������ �'��������.����.���� "����� � ��0��'������ ��� �������!������8��.��� � @�����'�"� G�'� ����.������������'�� � �8��.��� �����9'������.���������������8��.��� ������ 0�����"0������"��#��G���������.��������"�'����'����������$���)�!��� ��68��.��� � ��������0���(����� ���.�� ����,����� "����(������!����8��.��� �� ����,����� "�������(�����.��.�����!���68��.��� ������ �"�����!����'� ���"�� static void Main()

{

string s = null;

try

{

Console.WriteLine("In try block... before calling s.ToLower()");

Console.WriteLine(s.ToLower());

Console.WriteLine("In try block... after calling s.ToLower()");

}

catch(NullReferenceException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("NullReferenceException Caught");

Console.WriteLine("\nException Message");

Console.WriteLine("=============");

Console.WriteLine(e.Message);

Console.WriteLine("\nException Stack Trace");

Page 155: 3Programmer's Heaven C# School Book

��������������� ���������

?

Console.WriteLine("==============");

Console.WriteLine(e.StackTrace);

}

Console.WriteLine("\nAfter try...catch block");

}

���"�!!��� ��0��'�� ���� �� "���.��������"����������'�����.�� ��"����8.�� ����������� "���(������!����8��.��� �8.�������0��� �����8��.��� ��!��� ���������.���!���.������'���0�� In try block... before calling s.ToLower()

In catch block...

NullReferenceException Caught

Exception Message

=================

Object reference not set to an instance of an object.

Exception Stack Trace

=================

at CSharpSchool.Test.Main() in c:\documents and settings\administrator\

my documents\visual studio projects\myconsole\class1.cs:line 14

After try...catch block

Press any key to continue

8 ��. ����#����:�

����.��� ��!� ����0���(����7���!����������������0���(������"�� ���!� ����0���(������ ���"����'��0��8�����"'������� �8��.��� ������� ��� ������0���(�<�����9���!� ����0���(���"��!���� ����������J����"'���� ������0���(��������" ��0�����"0������!����8��.��� �&���8��.��9���!� ����0���(�� 0���"���������!���9"���0��9��(���� ����� � "�������.���� ���������.� �"� ������0���(�#��G�""���!� ����0���(�����.�������8��.��� static void Main()

{

string s = "Faraz";

try

{

Console.WriteLine("In try block... before calling s.ToLower()");

Console.WriteLine(s.ToLower());

Console.WriteLine("In try block... after calling s.ToLower()");

Page 156: 3Programmer's Heaven C# School Book

��������������� ���������

?1

}

catch(NullReferenceException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("NullReferenceException Caught");

}

finally

{

Console.WriteLine("\nIn finally block...");

}

}

��� '��8��������.������'������!����'� ����.��� In try block... before calling s.ToLower()

faraz

In try block... after calling s.ToLower()

In finally block...

Press any key to continue

�� �� ��8��.��� �����"9�����"�� ���!� ����0���(��8�����"7���!��������"�� ������0���(�#������� �8��.��� �������0����� ������� �GG�� ���� ,�� *+ static void Main()

{

string s = null;

...

}

$�'������.��'���0�� In try block... before calling s.ToLower()

In catch block...

NullReferenceException Caught

In finally block...

Press any key to continue

������.����'���������"�� ���!� ����0���(���'���8�����"�!�������8������ �!������� "�����0���(�����"���!�!� �8��.��� �������"�� ���

Page 157: 3Programmer's Heaven C# School Book

��������������� ���������

?;

���.��0����'�������������!� ����0���('������� ������0���(9����� static void Main()

{

string s = "Faraz";

try

{

Console.WriteLine("In try block... before calling s.ToLower()");

Console.WriteLine(s.ToLower());

Console.WriteLine("In try block... after calling s.ToLower()");

}

finally

{

Console.WriteLine("\nIn finally block...");

}

}

������.���!���.������'���0�� In try block... before calling s.ToLower()

faraz

In try block... after calling s.ToLower()

In finally block...

Press any key to continue

������.���!���.��������'�������!� ����0���(���'���8�����"�!���������0���(�

���� ���/��� $���(0��$� ����� ������� $������� ����:��

���.��0��������������.��*"�!!��� �+�8��.��� �������0�����' � ����0���(�� ������.��*��������!+�����0���(�&���8��.��9'��� '��������������0���(P� �!�������� ����$���)�!��� ��68��.��� 9��� "!�������� ����� "�8A��A!)� ��68��.��� � "�������"�!��� �������8��.��� *68��.��� +�)����0���������� "�8A��A!)� ��68��.��� �����"'�� � ����� ��!� �����'���� "�8�����!����� ���!��������������"�% ����!�� ��� "�8�� 0������������ C���9������������ ���J������C��!��������������"�0���'"��� �����������!�����.�������0���(� static void Main()

{

string s = "Faraz";

int []i = new int[3];

Page 158: 3Programmer's Heaven C# School Book

��������������� ���������

?B

try

{

Console.WriteLine("Entering the try block...\n");

// can cause NullReferenceException

Console.WriteLine("Lower case name is: " + s.ToLower());

// can cause NullReferenceException or IndexOutOfRangeException

Console.WriteLine("First element of array is: " + i[0].ToString());

// can cause DivideByZeroException

i[0] = 3;

i[1] = 4/i[0];

Console.WriteLine("\nLeaving the try block...");

}

catch(NullReferenceException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("NullReferenceException Caught");

}

catch(IndexOutOfRangeException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("IndexOutOfRangeException Caught");

}

catch(Exception e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("Exception Caught");

Console.WriteLine(e.Message);

}

}

����'�������"���� �GG� "� � ����������������C��!���� �������"������"�;��������������.����� ���.������'����'�'���� ���"������������� ���!�8��.��� �

�� GG� "G�G�� 0� ���9���� ��$���)�!��� ��68��.��� � �� %�������������G�G�������� � "�8A��A!)� ��68��.��� �� ���"����� �!B0��M2N������������"�@�d���68��.��� �!���������!�M2N�C����

Page 159: 3Programmer's Heaven C# School Book

��������������� ���������

?=

������"������"�������������0���(� �����"�!�������!������.��!�8��.��� �$������������������0���(�"��� �"�������� �������8��.��� �8��.�$���)�!��� ��68��.��� � "� "�8A��A!)� ��68��.��� 9'�������������"�0�� �������0������A ��� ��!�����8��.��� �� 0�����"9'����'�������� �������8������ �!������0���(� "'������ !������8������ �������.�����������0���(���� ����0�����"���8�����"'�'��������!����'� ����.��� Entering the try block...

Lower case name is: faraz

First element of array is: 0

Leaving the try block...

Press any key to continue

��!������"��� ��8��.��� ���������"�#���!�����(������� ���!��� �� ���� "������!!���� static void Main()

{

string s = null;

...

}

������.��'���0�� Entering the try block...

In catch block...

NullReferenceException Caught

Press any key to continue

�����(������� "�����������8.����"�$�'��� ����������� "�8���������!0�� "�����*����������� C��������������� ���J�����;+�%����� �������� �GG��.�� ��������� �������"���$���)�!��� ��68��.��� � Console.WriteLine("Sixth element of array is: " + i[5].ToString());

������.��'����8.����"��0�� Entering the try block...

Page 160: 3Programmer's Heaven C# School Book

��������������� ���������

??

Lower case name is: faraz

In catch block...

IndexOutOfRangeException Caught

Press any key to continue

&� ������������������������������ ������"� "�8� "��(����������!�M2N�J�����C����������������"�@�d���68��.��� ��� i[0] = 0;

���������.���!���.������'���0�� Entering the try block...

Lower case name is: faraz

First element of array is: 0

In catch block...

Exception Caught

Attempted to divide by zero.

Press any key to continue

����8������ �!;I�M2N������"�����"�@�d���68��.��� ������ ��������(�"!�����.��� ���!������0���(�� �������"�����0���(��!�� "� 68��.��� 9'���������.����.��!�������"�@�d���68��.��� *� "� �������8��.��� � �$��+�����8������ '���� ��� !����"�����������.� "� ������0���(�

-�� �$������$ �������������� ������ $������� ����:�� �

�� ���8��.��� ���.��� �� �$�������� "�07���9����!����'���� ������ ������������������ �����!���'����������0���(���� "���0������8��.��� 9��'������������������ "������!���0E���G�8��.��� �%����.�� ��������� ��!����0E����8��.��� �8.��������!������.��� �����8��.��� 9'�����������.�������������&���8��.��9�� �"�����!����'� ���"�� static void Main()

{

string s = null;

try

{

Console.WriteLine("Entering the try block...\n");

// can cause NullReferenceException

Console.WriteLine("Lower case name is: " + s.ToLower());

Page 161: 3Programmer's Heaven C# School Book

��������������� ���������

?�

Console.WriteLine("\nLeaving the try block...");

}

catch(Exception e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("Exception Caught");

Console.WriteLine(e.Message);

}

catch(NullReferenceException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("NullReferenceException Caught");

}

}

����9��������0���(���� "�����68��.��� ��.��8��.��� �.����".���������0E����8��.��� *$���)�!��� ��68��.��� +�$�'9 �������'����8��.��� �����"� ������0���(9��'�����'��0�������0����!��������0���(9��(� ������� "�����0���(H"��"��"�HE���� ����0�������"�������.����"��������� ".��"���� �����'�� '������.������.������.������� A previous catch clause already catches all exceptions of this or a super type

('System.Exception')

�� ��9� ������ �"������ ������ ������������!"�!!��� ��8��.��� '�� �� ������.�������0���(�

�� ��� �$������$ ���������(0��$� ��1��� ��� �����

���.��0����'������� ����!����8��.��� ���� ��� ��������.��� ����'�������� ��� � �� ���!��� ��9��(����� catch(NullReferenceException)

{

...

}

��'����9'�'���"������������"� ��"����� ������.�����0����'����� ����������(��'��"'������.��� ����9'������������������� ����68��.��� ��.��8��.��� �������� �� ��8��.��� � catch

Page 162: 3Programmer's Heaven C# School Book

��������������� ���������

?�

{

...

}

��'���"���� � "��� �������� ��������90�������������68��.��� �����������������8��.��� 9��(����� catch(Exception e)

{

...

}

2�. � ���#���6���������0��$� ���

���.��0��� ����"�!� ������' ������8��.��� ���"� ��!����������� ���!� �8.����"��� �� ������"��%����"�������9�8��.��� �����.���� ��"� �������� "�07������� ��"����"�!� �� �'������8��.��� 9� ����"�!� �� �'���!������@��0�!���� "���� "� ���''��� "�!� �����' �8��.��� 9�����.���� ���� "���� "���� ������ ������������!0����8��.��� � ����$��!����'��(�

(0��$� ��1 ����� #� ��� ���*��������6�:�

���������'���.��!�8��.��� � �$���

�� 68��.��� �� �����"0������ ����*����� #� �����)� ����+��������"�����68��.��� � �� 68��.��� �� �����"0�������G.��������������"%..������� 68��.��� �

�����.������������!�8��.��� � ����$��!����'��(���' � ���!����'� �"�������

%$��&����

'(���)%$��&����

%$��&�������*�+��,(�-.���)��/01-2

3&& �������%$��&����

%$��&�������*�+��,(�.���&��)�

��������� ����������������������������

�� ��9���������"�!� �"�8��.��� ����"0�"�����"!������%..������� 68��.��� ����� ���!����'� ���"�9'�'���"��� �������'��"�!� �����' ������8��.��� ���"� ����"%����� �68��.��� ���'���

Page 163: 3Programmer's Heaven C# School Book

��������������� ���������

?/

������8��.��� � �������"�*+�����"9��' �������� ������ 9'�� ������ "������ ��C���������"���"�!� ����������8��.��� 9� ����"%����� �68��.��� 9�� class InvalidArgumentException : ApplicationException

{

public InvalidArgumentException() : base("Divide By Zero Error")

{

}

public InvalidArgumentException(string message) : base(message)

{

}

}

A��������8��.��� ���� ����"%����� �68��.��� ��0�� "�����"!���%..������� 68��.��� *���0���!������"�!� �"�8��.��� +�� ���0�"��!������9'�����"�!� �"� ���'��� ��������A ���(� ������� �9'��������������(����� �G�����G�� ������ ��@�������� �������.��������������0������� �������9'����� ������C����,����.��.����*����� ����.��� �� ���68��.��� ���+'�������..���"��� ��$�'����'�����"�!� �"����' ������8��.��� 9��������������

8 �6 �������0��$� �C�� ��� �6�:�#6��

%�����"�� ����'� �8��.��� �� ��������'(��'��"���'��� �'"��� �������'�������"�*+�����"�� ����'���� ����"%����� �68��.��� '�� ������ "������ ��C���� static double Divide(double a, double b)

{

if(b==0)

throw new InvalidArgumentException();

double c = a/b;

return c;

}

����9�������"�*+�����"'�������'���� ����"%����� �68��.��� '�� ������ "������ ��!�� "��0�C����$��������������"�*+�����"������� "����'� �'�07����!��.�� ����"%����� �68��.��� �%���� �������9���� ���"�!� ����' �����'�� ����'� �����8��.��� 9��(�� throw new InvalidArgumentException("Error: The second argument of Divide is zero");

Page 164: 3Programmer's Heaven C# School Book

��������������� ���������

�2

$�'���,�� *+�����"'�������������"� "���������8��.��� �� ���������������0���(� static void Main()

{

try

{

Console.WriteLine("In try block...");

double d = Divide(3, 0);

Console.WriteLine("\tResult of division: {0}",d);

}

catch(InvalidArgumentException e)

{

Console.WriteLine("\nIn catch block...");

Console.WriteLine("\tSystem.InvalidArgumentException caught...");

Console.WriteLine("\tMessage: " + e.Message);

}

}

�����"�������������������������8��.��9�8��.����� �''���������� ��8��.��� ����'�����"�!� �"���� ���.��������8�����"9���!����'� ����.����� �����"� In try block...

In catch block...

System.InvalidArgumentException caught...

Message: Divide By Zero Error

Press any key to continue

$��������������,�������.��������� ����' �����������������.���� �.�� ��0���"�!� � ������' �8��.��� �

�� ������ �� ��� � ����$6�!����'��(������� ����!����8��.��� � "'������'��"G68��.��� G��(������68��.��� 9$���)�!��� ��68��.��� 9� "�8A��A!)� ��68��.��� 9������ ��9'�'���"��� ���������� "!����'� ���� ��� ��� �� ��� �

�� %�'��"���������������8��.��� !������%..������� 68��.��� ���� �� ������ �� "����'� ��8��.��� �����.��!���� ���������"9���� ������".�������� �

.�������������'� �������8��.��� �

Page 165: 3Programmer's Heaven C# School Book

��������������� ���������

+��2���������!�(3�����

������"����

��"��'�'����8.��������� ��.��!"�������� "��� ����'����������0����(� �������"��0��� ""�������� "'�������'"������������"� ���#����9'�'����8.������������"�������� "������� �!��� ���&� ����9'�'������� �0������ �� "��� ��� "�� ������ ��� ���������"��������

2���������7�� ���

�������������!��� ���������"���!��'�������"��!��� �����07���9��������(�\ �'����(*+P ���������!��� ����� �07����!�������(�����.���� ��9������!��� �����'�.��.������ ������.��!�07���*���+�����!��� ���� .�� ����1�����������07�����!��� ��"*��.�� ��"��+0������!��� ��� ��������������������07�����!��� ��90�������"����!��� �������"� ���"�!�07���������.��!�"�������������.����� ������!��������"��������� ��������� ���"��������������.��.������ ������.����� ������!��������"�������"��������� .�� ���1����"���������!��� ��'������ 0���"����!��� ��������" ;���������������"��!��� ��"0����"������� ����������� �'������������ ��������������������������������������������� ����"**�� �

3��4,�����-�5�����

3���,������5�&����. ���(&�

3�6� ���� 3�)��*����5�&����. ���(&�

7��������

7��������

8 ���#$����� ��������.�� ����� �� ��������������$ �����

@�!����� ��"�������9'� ��"��.���!������.����� ������!��������"���"��������� ��!��� �������� ������!������"� ���"�������� ��.�� "�����.��!.��������'��������J������0�.��"�

Page 166: 3Programmer's Heaven C# School Book

��������������� ���������

�1

&���8��.��� int someMethod(string [] args)

��������� �� ������!���,�� *+�����"�!���.������"�!� �"�� int Main(string [] args)

{

...

}

% "!�����!����'� �%""*+�����"� int Add(int a, int b)

{

return a+b;

}

����� �����'���0�� int aMethod(int p, int q)

�������������� ������!!����'� ���0�����*+�����"� int Subtract(int c, int d)

{

return c-d;

}

������"0� �����"!�������0����8��.��������� ����!������"� �����.����!���� �����P����� ������ ��� ������������ ��.�� ".��������� � ����!"�������9'�"�!� ������.��!�"��������� ����"�������(��'��"9���� delegate int MyDelegate(int p, int q);

����'�����"�!� �"�"���������.�'������ ���G,���������G������!��� ���!���"���������.��� 0���"��.�� ���� ������"'������(��'�� ������.��������� "����� � � �����������

Page 167: 3Programmer's Heaven C# School Book

��������������� ���������

�;

8 ������������.������'�� ������������������.������������ �

A ��'�����"�!� �"�"���������.�9'��� ��������!��� �������������"'��������� ��� ������%"���������!��� ���� 0�"������"7����(�� �07�����!��� ���&���8��.��9���!��� ���!��.�,���������*"�!� �"�0���+�� 0�"������"�� MyDelegate arithMethod;

���"���������!��� �������,����"�� �'��!��� ��� ������"'����� �������"� ������������ ������!,����������

��8 ������������ ���.��������#�� ����������

���"���������!��� ���� 0���"�����!��� ��� ������"'���������� ��� �����0�.�� ��� �������.���������!"�������� arithMethod = new MyDelegate(Add);

����9��������,����""���������!��� �����"���.�� ������%""*+�����"0�.�� ��� �����.�������������"���������.�*,���������+��������'���.�� 0������"��������� �� ��������� �9��(�����E MyDelegate arithMethod = new MyDelegate(Add);

���� ���� ������������ �� ��� � �����������

A �����"���������!��� ��G�����,����"G��0�� ��"���.�� ������%""*+�����"9���� 0���"��������������������"��(�����E int r = arithMethod(3, 4);

������.�����������"��!���.�������.��� ��"0���'� using System;

namespace CSharpSchool

{

class Test

{

delegate int MyDelegate(int p, int q);

static void Main()

{

MyDelegate arithMethod = new MyDelegate(Add);

Page 168: 3Programmer's Heaven C# School Book

��������������� ���������

�B

int r = arithMethod(3, 4);

Console.WriteLine("The result of arithmetic operation `+' on 3 and 4 is: {0}", r);

}

static int Add(int a, int b)

{

return a + b;

}

}

}

���������!����0���.������'���0� The result of arithmetic operation `+' on 3 and 4 is: 7

Press any key to continue

����0���.�������� 0���� ��"�������������������.������ �� 0�������"0�������� using System;

namespace CSharpSchool

{

class Test

{

delegate int MyDelegate(int p, int q);

static void Main()

{

MyDelegate arithMethod = null;

Console.WriteLine("Which arithmetic operation you like to perform on 3 and 4?");

Console.WriteLine("Press + for Add ");

Console.WriteLine("Press - for Subtract ");

Console.Write("Press m for Maximum Number ");

char choice = (char) Console.Read();

switch(choice)

{

case '+':

arithMethod = new MyDelegate(Add);

break;

case '-':

arithMethod = new MyDelegate(Subtract);

Page 169: 3Programmer's Heaven C# School Book

��������������� ���������

�=

break;

case 'm':

arithMethod = new MyDelegate(Max);

break;

}

int r = arithMethod(3, 4);

Console.WriteLine("\nThe result of arithmetic operation {0} on 3 and 4 is: {1}", choice, r);

}

static int Add(int a, int b)

{

return a + b;

}

static int Subtract(int a, int b)

{

return a-b;

}

static int Max(int c, int d)

{

if(c>d)

return c;

else

return d;

}

}

}

����'�����"�!� �"����������"'����������� �����P%""*+9��0�����*+� ",�8*+�%"���������.������",����������"�!� �"���������!��� ����������������� .�� ���� ������"'���������� ��� ���������"���������!��� ��G�������������G���"��.�� �������.��������������"0��"� ������� .������ ����������.�����.���!�����"��� Which arithmetic operation you like to perform on 3 and 4?

Press + for Add

Press - for Subtract

Press m for Maximum Number -

The result of arithmetic operation - on 3 and 4 is: -1

Press any key to continue

Page 170: 3Programmer's Heaven C# School Book

��������������� ���������

�?

�� ��9� ������.���0���9������.���"GEG9���"���������!��� �����"�����!��� ��� "���������0�����*+�����"�����0���.��������'����������"���������!��� ���� 0���"��.�� ��������������"��� ��������� ��������������� �����.���!��"0����"���������.��

��.�� �� ������ ���#�

< !���� �����9����������G"�������G���"!��0���G"���������.�G� "G"���������!��� ��G9'������������������� !��� � ������"��G�� "�&������(��!�������9'������ �� ������� ��������G"���������.�G� "G"���������!��� ��G� "'���������� "������"��������������

2��������� ��� ���*��������6�:�

%���������.��� �"���������(��'��"� "��!�������� ������� �����9� �$��"����������.��� �����!��� ����.�9� "���"�������� �����!����������������������.���� ��9���� ������9���.����"�!� ���� ������"G�"�����������!��� ����������"G� ��J�����..��.������%"�����������!��� ����.�"�����"!������������������ "��� �� ���� 0���"�����������"'��������� ��� ������% �������.���� ���� ��� ������������� ��"�!� � ��"���������� ������ �� �'�0E��.��!��������������9���"��������� ��0�"�!� �"'���� ������"*'������������!����"� �����.�+������������ '��'�����"�!� �"���"�������,�������������"����,�� *+�����"� ����8��.����"��!������ �class Test

{

delegate int MyDelegate(int p, int q);

static void Main()

{

MyDelegate arithMethod = null;

...

}

}

"��� ����������������� ��

-����(����!��� ����� �07����� 0�.��"��������07���9���"���������!��� ���!� ������"�� 0�.��"��� ����������"�&���8��.��9�����(�������"�����"G���!���%����A.������ *+G9'������(��'�� ������ "�"���������!��� ���!��.�,���������9� "�������� ��.�����"�����"�� �����'�� ������ static void PerformArithOperation(int a, int b, MyDelegate arithOperation)

{

int r = arithOperation(a, b);

Console.WriteLine("\nThe result of arithmetic operation on 3 and 4 is: {0}", r);

}

Page 171: 3Programmer's Heaven C# School Book

��������������� ���������

��

$�'� ���,�� *+�����"9'�'���������������"� PerformArithOperation(3, 4, arithMethod);

�� ��9�����(�!�������� �� ".�� �� �����������0�� "�������"*����� !����"+��������!���%����A.������ *+�����"�������.�����������"��!���.���������' 0���'� using System;

namespace CSharpSchool

{

class Test

{

delegate int MyDelegate(int p, int q);

static void Main()

{

MyDelegate arithMethod = null;

Console.WriteLine("Which arithmetic operation you like to perform on 3 and 4?");

Console.WriteLine("Press + for Add ");

Console.WriteLine("Press - for Subtract ");

Console.Write("Press m for Maximum Number ");

char choice = (char) Console.Read();

switch(choice)

{

case '+':

arithMethod = new MyDelegate(Add);

break;

case '-':

arithMethod = new MyDelegate(Subtract);

break;

case 'm':

arithMethod = new MyDelegate(Max);

break;

}

PerformArithOperation(3, 4, arithMethod);

}

static void PerformArithOperation(int a, int b, MyDelegate arithOperation)

{

int r = arithOperation(a, b);

Console.WriteLine("\nThe result of arithmetic operation on 3 and 4 is: {0}", r);

Page 172: 3Programmer's Heaven C# School Book

��������������� ���������

��

}

static int Add(int a, int b)

{

return a + b;

}

static int Subtract(int a, int b)

{

return a-b;

}

static int Max(int c, int d)

{

if(c>d)

return c;

else

return d;

}

}

}

/��� �����2���������

%.�����!�������!"�������������� ���"��������� � ��.������������ � ������"�!������� ��� ����������(� "�!"���������������"G,���������������G�� ��� ����9��������"�����������0E��.��!������,���������������9'��������!���0����!�����������������������.���� �.�� ��������0���0�����������"������������H�������� ��.��!���������"���������.����0����"H�������� !�������������� ��������������"�������������������.�������"� ��� ������� ������ ���� ���"�������*�������"+� ������� �� ����� � ���� ��������9���������"���������.��������������"����� ��.��

��$������ �����/��� �����2��������

%��������"��������"�!� �"� �8�����������'������.��"�������9'�������8��.��� ������������ ��.��!���������"������������������"� delegate void MyMulticastDelegate(int p, int q);

���"�!!��� ������"����""�"����������"�������G� ������� ���0��� �G4\G��� �� ��.������9��(����� MyMulticastDelegate arithMethod = null;

arithMethod = new MyMulticastDelegate(Add);

Page 173: 3Programmer's Heaven C# School Book

��������������� ���������

�/

arithMethod += new MyMulticastDelegate(Subtract);

arithMethod += new MyMulticastDelegate(Max);

���� ������� �!���������"������������ �������������! �����"�������� "�����"�8��.�������� ��� ����������� ��.�����"�����"� arithMethod(3, 4);

������.�����������"��!����8��.�����' 0���'� using System;

namespace CSharpSchool

{

class Test

{

delegate void MyMulticastDelegate(int p, int q);

static void Main()

{

MyMulticastDelegate arithMethod = null;

arithMethod = new MyDelegate(Add);

arithMethod += new MyDelegate(Subtract);

arithMethod += new MyDelegate(Max);

arithMethod(3, 4);

}

static void Add(int a, int b)

{

Console.WriteLine("The sum of 3 and 4 is: {0}", a+b);

}

static void Subtract(int a, int b)

{

Console.WriteLine("The difference of 3 and 4 is: {0}", a-b);

}

static void Max(int c, int d)

{

if(c>d)

Console.WriteLine("The Maximum of 3 and 4 is: {0}", c);

else

Console.WriteLine("The Maximum of 3 and 4 is: {0}", d);

Page 174: 3Programmer's Heaven C# School Book

��������������� ���������

�2

}

}

}

$�������'�������� ��"���%""*+9��0�����*+� ",�8*+�����"�����������������"����� ��.�� ".�� �������������!�������.�������.������ '���� ���0�"��!��������"�������.���!���.�������� The sum of 3 and 4 is: 7

The difference of 3 and 4 is: -1

The Maximum of 3 and 4 is: 4

Press any key to continue

$����������� ���"�������� ������� ��� ��(�"����!���� ��.�����"�����"������ ��.����"� ������ ��� "�� ������ ���!�$��9"����0�"������ ������ 9'����������� ��� "��������"������"*'�� ������ ��!���"+������������������"��������

9��3 �������� �.���� ������ ������������H�� �3��� ��� ���

-���'��� �""�����"�������������"�������G� ������� ����� �G4\G�.������9'��� ������������"!��������������"�������G� ������� ����� ����GE\G�.��������� �"����������",�� *+�����"�!���.������.��������' 0���'� static void Main()

{

Console.WriteLine("Adding 3 methods to the multicast delegate...");

Console.WriteLine("==================");

MyMulticastDelegate arithMethod = null;

arithMethod = new MyMulticastDelegate(Add);

arithMethod += new MyMulticastDelegate(Subtract);

arithMethod += new MyMulticastDelegate(Max);

arithMethod(3, 4);

Console.WriteLine("\nRemoving Subtract() method from the multicast delegate...");

Console.WriteLine ("==================================);

arithMethod -= new MyMulticastDelegate(Subtract);

arithMethod(3, 4);

}

Page 175: 3Programmer's Heaven C# School Book

��������������� ���������

&���'������""�"�������������"*%""*+9��0�����*+� ",�8*++�������������"�������G,�,���������������G� "� ��(�"���"��������#����9'�������"�����0�����*+�����"!��������������"�������� "� ��(�"������ �������.���!�����"�'���0�� Adding 3 methods to the multicast delegate...

=============================================

The sum of 3 and 4 is: 7

The difference of 3 and 4 is: -1

The Maximum of 3 and 4 is: 4

Removing Subtract() method from the multicast delegate...

=========================================================

The sum of 3 and 4 is: 7

The Maximum of 3 and 4 is: 4

Press any key to continue

������.����'���������0�����*+�����"��0�� ������"!������"�������G� ������� ���� "� �������"'�� ���"��������� ��(�"������ "�����

(3��������(3����1��� ���

6�� ���������� ����� ������..� "��� �����8������ �!�.��������������..������� '�����0� ���!��"�0���9����� ��.� "�% ��� ��� 0����������(9�(�����(���������� ��!������� ����*�����+�% ��� ��0�������������'�������"��0�!���"����������"'�� �����.����������� ������%������������ ��� �������"� G��� �� "��G9���������������� ��� �������"� "G��� ��� ����G� "������"'�������"���� "���.������������ �������"� G��� ��� "���G� ����������� �2�������� �����������������������7�������!6���������� �0�������8������7���������'���������%�����������

����������/������6�� ��������� �������������"#�������������+��������������� ,����� ������� ��������������������

��������������������� ���������������������������� � �������������������������/������6�� ���������82��(������

'����(���������������������������������������������%����������������������%���93����������%������������������� �

���������:�������������������������������������� ��� �� �������������������������;9� �

(3����1��� ��� �����

� �$��9��� ������.���� ��"���������"��������� ����� �����!������*0���+�� ������� �����9� "���"�!� �"�� ������� �(��'��"������.!����.���� �� ���� �� "��� ��� "�� ����� ���!� ��.�0���"�������!�������� �����"�� ����0�� "��������� �� ��� ���� ������!�"�������!��� ��� ���

Page 176: 3Programmer's Heaven C# School Book

��������������� ���������

�1

public void EventDelegate(object sender, EventArgs e)

1���!� ��������� ����������������� ����!� ��.�0������ �� �������� ������� �(��'��"� "���.�0���"�������� public event EventDelegate MyEvent

������������������������� ����� ���� �� ��� �9���!��������� ������������ "��������� �����!������ �������� "������ ����0E��.��!������6�� �%��9'�������"� ��""���� ��"�����0�.��"�������� ��� "���� class SomeEventArgs : EventArgs

{

...

}

% ��� ���� ����������"��(����� SomeEventArgs someData = new SomeEventArgs(/*some necessary arguments*/);

MyEvent(this, someData);

A��! �"��� ��"��0�� �9������ ������"��(����� MyEvent(this, null);

;���!� �������������������� ��������������������� �..������� ����� ��� � ����,�� *+�����"������ ��� ��� "��������"� ������������ ������!������ ��� "������0��"� ������������!���.�0���"�������������"� ��. ���� ����!������ ��� "��������"�� �� ��� ��������'������'��"HA H9���� public void OnMyEvent(object sender, EventArgs e)

{

// handle the event

}

� �� ����������� ��� ���������������"� ��.1��(����� EventClass eventObj = new EventClass();

%""������ ��� "���'����� � �������� ������������ ��� ���������G��� ��

Page 177: 3Programmer's Heaven C# School Book

��������������� ���������

�;

eventObj.MyEvent += new EventDelegate(OnMyEvent);

$�'������ ��� "���GA ,�6�� �*+G'���0������"�������������'�� ���������� �G,�6�� �G���������"�

-����:�8 ����(0��$���

�����.� "���� "��'��� ������.���� ��"� "�������"9���G���(��������"���� ��H����(�����H�8��.����������(������� ������ ��� �������� "� " ���!������ ������"���� ����������� ��&���'�"�!� ��.�0���"�������!�������� �9����� ���G�����6�� �G� public delegate void TimerEvent(object sender, EventArgs e);

$�''�"�!� ����� ���"G����(�����G���� ����������� �� class ClockTimer

{

public event TimerEvent Timer;

public void Start()

{

for(int i=0; i<5; i++)

{

Timer(this, null);

Thread.Sleep(1000);

}

}

}

�������� ��� � ��� �9G�����G9�!��.������6�� �"��������� ��������*+�����"9������ �G�����G�����"������� "!���������!=���������9'�������"�������.*+�����"�!��������������"� �������"���9'������(���� ��0���!�������� "�������� ������"'���0��.� "�"��������� ����'����8.���������"� �� "������ ���� ���� � $�8�'� ��"��"�!� ���������'����������� "�� ��������� �9'�����"�!� �"�� class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer();

clockTimer.Timer += new TimerEvent(OnClockTick);

clockTimer.Start();

Page 178: 3Programmer's Heaven C# School Book

��������������� ���������

�B

}

public static void OnClockTick(object sender, EventArgs e)

{

Console.WriteLine("Received a clock tick event!");

}

}

�������� ��� � ��� ��� "��������"9GA ����(���(*+G9'����!����'�������(6�� �"�������G�� ������� ���,�� *+�����"�!������9'�����������"� � �� ���!������ ��� ���������G����(�����G�#����'���������"*���0���0�"+���A ����(���(*+��� ��� "��������G�����G��� ��!�������(���������&� ����9'����������"��������*+�����"9'����'����������.�����!�� ����� ���� �� �������(���������������.�����������"��!���.���������' 0���'� using System;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer();

clockTimer.Timer += new TimerEvent(OnClockTick);

clockTimer.Start();

}

public static void OnClockTick(object sender, EventArgs e)

{

Console.WriteLine("Received a clock tick event!");

}

}

public delegate void TimerEvent(object sender, EventArgs e);

class ClockTimer

{

public event TimerEvent Timer;

public void Start()

{

Page 179: 3Programmer's Heaven C# School Book

��������������� ���������

�=

for(int i=0; i<5; i++)

{

Timer(this, null);

Thread.Sleep(1000);

}

}

}

}

$�������'��������� ���"�"��������������"� � ���.�������������!���.������9�'������ ��������"���� �����"��������.���!���.�������� Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Press any key to continue

6���������.�� ��"'����"�����!� ���� "� "!�����������.�� ��"� ������

/��� ������3�����

�� ����� ������.���� ��"���������"�������� ��9'��� �0���0������.����� ��� "������� ������ ��&���8��.��9�� �"����������"������� class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer();

clockTimer.Timer += new TimerEvent(OnClockTick);

clockTimer.Timer += new TimerEvent(OnClockTick2);

clockTimer.Start();

}

public static void OnClockTick(object sender, EventArgs e)

{

Console.WriteLine("Received a clock tick event!");

}

public static void OnClockTick2(object sender, EventArgs e)

Page 180: 3Programmer's Heaven C# School Book

��������������� ���������

�?

{

Console.WriteLine("Received a clock tick event in OnClockTick2!");

}

}

����'������ ���"���"� �������� ��� "���9GA ����(���(1G9� "�����0���0�"������������������ �� ���,�� *+�����"�� ����G4\G�.�������������.���!���.�������� Received a clock tick event!

Received a clock tick event in OnClockTick2!

Received a clock tick event!

Received a clock tick event in OnClockTick2!

Received a clock tick event!

Received a clock tick event in OnClockTick2!

Received a clock tick event!

Received a clock tick event in OnClockTick2!

Received a clock tick event!

Received a clock tick event in OnClockTick2!

Press any key to continue

%�� 0��� � ������.���0���9 �'0������A ����(���(*+� "A ����(���(1*+���� ��(�"�������������� ������"�

"��� �����������6 � �� ��(3���C�&��F����� ���&#�����(3���-����

&� ����9'��� .�����""���� ��� !������� '�������� �� ��� ��&�����9'� ��"��.��!������!����'� ��������.� ���!� ���������� �����!���������6�� �%�� 1�6 ��.��������"�����0�.��"'��������� �'���� ������*.��!���0���� �.��.�����+ ;�������� � �� ���!������� ������ ��� ���������� ".���'��������� � #��G �'��� �����.����������(������8��.������������� �����"����� ��� ����J�� �� ��0���!�������(���(�&���'� ��"��"�!� �� �'���G����(�����%��G9'����� �����!������������6�� �%������ public class ClockTimerArgs : EventArgs

{

private int tickCount;

public ClockTimerArgs(int tickCount)

{

this.tickCount = tickCount;

Page 181: 3Programmer's Heaven C# School Book

��������������� ���������

��

}

public int TickCount

{

get { return tickCount; }

}

}

�������(�����%������� ��� �.�����������0�� ���"G���(��� �G�����"�������� ����( ��0������������.��"������07�����������.�0����� �������� "������0���������� ��� "�������������.�0���.��.�����$�8�'� ��"����� �����"�������"�!� ���� !�������� ���� public delegate void TimerEvent(object sender, ClockTimerArgs e);

��������� ���.�� ���"����������� ��"!���6�� �%��������(�����%����������.�0�����*��� ��� ������+�� .����.�����������.��!������ �������0���0��*��� ��� "���+������� ��� ����������"�!� �"�� class ClockTimer

{

public event TimerEvent Timer;

public void Start()

{

for(int i=0; i<5; i++)

{

Timer(this, new ClockTimerArgs(i+1));

Thread.Sleep(1000);

}

}

}

���� ����� ��� ������������ ���"�!.�� � ���������� "������ �9'����.�� �� �'�07����!�������(�����%����.�'�������J�� �� ��0���!�������� �����(���(�&� ����9������ ��� "����'����� � public static void OnClockTick(object sender, ClockTimerArgs e)

{

Console.WriteLine("Received a clock tick event. This is clock tick number {0}", e.TickCount);

}

����'�������.��.�� ��"�������(���( ��0���� ��������(�����%��G���(��� ����.�����������.�����������"����' 0���'�

Page 182: 3Programmer's Heaven C# School Book

��������������� ���������

��

using System;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer();

clockTimer.Timer += new TimerEvent(OnClockTick);

clockTimer.Start();

}

public static void OnClockTick(object sender, ClockTimerArgs e)

{

Console.WriteLine("Received a clock tick event. This is clock tick number {0}", e.TickCount);

}

}

public class ClockTimerArgs : EventArgs

{

private int tickCount;

public ClockTimerArgs(int tickCount)

{

this.tickCount = tickCount;

}

public int TickCount

{

get { return tickCount; }

}

}

public delegate void TimerEvent(object sender, ClockTimerArgs e);

class ClockTimer

{

public event TimerEvent Timer;

public void Start()

{

for(int i=0; i<5; i++)

{

Page 183: 3Programmer's Heaven C# School Book

��������������� ���������

�/

Timer(this, new ClockTimerArgs(i+1));

Thread.Sleep(1000);

}

}

}

}

��� ����0���.����������.���"� "�8�����"9'�'��������!����'� ����.��� Received a clock tick event. This is clock tick number 1

Received a clock tick event. This is clock tick number 2

Received a clock tick event. This is clock tick number 3

Received a clock tick event. This is clock tick number 4

Received a clock tick event. This is clock tick number 5

Press any key to continue

%������.���!���.���������������9 �''�������������� ��������(���( ��0����� �'���������� ��

Page 184: 3Programmer's Heaven C# School Book

��������������� ���������

/2

�4��5 ������!�5 �6��-$$� ��� ���

������"����

��"��'�'�������0���"� ��� "�'%..������� � �����'�������0����(� �������������������!�� "�'%..������� � "������..���� �$���#����9'�'���"��� ���!���H������� &���H%..������� � "���� �0���������'� "�'!����� �����&� ����9'�'������(����'5�������"���$��������������� �!�� "�'%..������� �

5 �6��-$$� ��� �������*���

��� "�$��.����"��8�� ����..���!��0���"� ��� "�'%..������� ���������.���� �.�� ��0���'� "�'�..������� ������������G��� �"���� G�%��'� "�'�..������� .��� �����.������ ���!�������������� "��.� "������ �������� �������.��������� ���!���������"�G�� "�'&���G9��G�� &���G!�������%'� "�'!�������� ��� ��8���0��9.��0���� 9��8�0�8�9���0�8�9�����9�� �� "����� ���!������� ������ !���9��� &��������'� "�'�� ����7����(����8�0�89��0��9����� �$��9���'� "�'�� ���������.��� ��"0�0������07����� ��� �"� ����������� "�'�&��� ���.����

5 �����7�� ���

%����"�������9�$��.����"������ &���� "������� �����������0������� ����������� "�'�&��� ���.������������������ "�'�&����&�������0������!����� &���� �$���� ��"����"��� �'� "�'�..������� 9'� ��"��� ���������� "�'%..������� .��7���� 5�������"���$��9���""��!��� ������������ "�'�&���� "���������'� ������������ �.��7�����!������ ���� �5�������"�������9�����I��!��� ���.��� �!�������� "�� ����.�������""�������0����1������� �'�������.��� ������ &���� ""�������!�������������� "�'�&����&������� class MyForm : System.Windows.Form

{

...

}

;�� �� ������������� ����9��������..��.�����.��.������ "�""������,�&���G�� ������������� � B������� ���������� ��� � ����,�� *+�����"�� ���,�� *+�����"9�������������%..������� �)� *+�����"9�..��� ���'���� � �� ���!,�&����

Page 185: 3Programmer's Heaven C# School Book

��������������� ���������

/

class Test

{

static void Main()

{

Application.Run(new MyForm());

}

}

���%..������� �)� *+�����"�����������!�����'� "�'�..������� � ����.����� ������������������������� ������!�������� "�'A.����� �������

7� � ���� ��I1����5 ����I�-$$� ��� ��

#��G0���"���!���'� "�'�..������� 9'����'�'�������H������� &���H�����..������� '���.��� ����.��'� "�''����H������� &���H������ �������� ��������������"��!���.�������� using System;

using System.Windows.Forms;

using System.Drawing;

namespace CSharpSchool

{

class Test

{

static void Main()

{

Application.Run(new MyWindow());

}

}

class MyWindow : Form

{

public MyWindow() : base()

{

this.Text = "My First Windows Application";

this.Size = new Size(300, 300);

Label lblGreeting = new Label();

lblGreeting.Text = "Hello WinForm";

lblGreeting.Location = new Point(100, 100);

this.Controls.Add(lblGreeting);

Page 186: 3Programmer's Heaven C# School Book

��������������� ���������

/1

}

}

}

B������� ���� �����

%��������9'�� ���"�"����� ���.���� ����..������� � using System;

using System.Windows.Forms;

using System.Drawing;

�������� ���.���9�'�����"� ���!������ 9���� ������� ���"�� ��!������..������� �� !���9���%..������� �������'���"������ ���,�� *+�����"�"�!� �"� ��� ���.��������������� "�'�&��� ���.����� ��� ���0������!��'� "�'�� ����9����&���9#�0��� "@���� �&� ����9� ���"� �������������'� � ���.���� ����������� ��� �������������"�����"��'� ��!�� ����������C�� "��� �������"������ ���.�����������������"�!� �"� ������������'� � ���.���� #����9'�"�����"� �'���9G,��� "�'G9!������&������"�!� �"� �������� "�'�&���� class MyWindow : Form

{

...

}

� ����� ��������!,��� "�'9'�.���!��"����C�� "������!���!���*0����� �����C�� "��8�.��.�����+�����C��"�!� �"�� �������������'� � ���.���G��C�������.��"�'�� ������������ ��������!��C���.���!����'�"��� "����������!���!���� public MyWindow() : base()

{

this.Text = "My First Windows Application";

this.Size = new Size(300, 300);

$�8�� ����� �������9'�������"���8���0��� "�""�"��������� ������������� �!���&����%��8���0�����"��'���������8�� ���!��������������� "�'�&����#�0�����"�!� ����8���0��� ��� "�'�..������� ����������8��!���#�0���� �����8�.��.����9'������!������ ���.��%������� ������ ��� �"0��!������0��""�"������ ������������� P�� ��'���������""�"�����0���������������� � public MyWindow() : base()

{

Page 187: 3Programmer's Heaven C# School Book

��������������� ���������

/;

this.Text = "My First Windows Application";

this.Size = new Size(300, 300);

Label lblGreeting = new Label();

lblGreeting.Text = "Hello WinForm";

lblGreeting.Location = new Point(100, 100);

this.Controls.Add(lblGreeting);

}

&� ����9'�����������"��������� ��� � ����,�� *+�����"�� ���,�� *+�����"9'������ �� �����"���,��� "�'���� ".��"����!��� �������%..������� �)� *+�����"������������������!�������� "�'A.����� ������� ��� '��8���������0�����"�9���!����'� ����� �"�.����"�

����������..������� 9.���������0���� � ��������0���

- ���(3����1��� ���

#��G �'�""�0���� ��0���"G68��G�����!�������G68��G0���� '�����������..������� '�� �������(�"�� �$��9���@���� ���� �� ���!����������� "�'�&����@���� ��������������������� '������0���� ����(9'� ��"��������� ��� ��� "���� "�������*���""+�������@���� G����(��� ��@���'������"�!������..������� �

Page 188: 3Programmer's Heaven C# School Book

��������������� ���������

/B

using System;

using System.Windows.Forms;

using System.Drawing;

namespace CSharpSchool

{

class Test

{

static void Main()

{

Application.Run(new MyWindow());

}

}

class MyWindow : Form

{

public MyWindow() : base()

{

// Form

this.Text = "My First Windows Application";

this.Size = new Size(300, 300);

this.StartPosition = FormStartPosition.CenterScreen;

// Label

Label lblGreeting = new Label();

lblGreeting.Text = "Hello WinForm";

lblGreeting.Location = new Point(100, 100);

// Button

Button btnExit = new Button();

btnExit.Text = "Exit";

btnExit.Location = new Point(180, 180);

btnExit.Size = new Size(80, 30);

btnExit.Click += new EventHandler(BtnExitOnClick);

// Adding controls to Form

this.Controls.AddRange(new Control[] {lblGreeting, btnExit});

}

public void BtnExitOnClick(object sender, EventArgs e)

{

Application.Exit();

Page 189: 3Programmer's Heaven C# School Book

��������������� ���������

/=

}

}

}

� ����� ��������!,��� "�'9!���'������������� .��.������!���&����� �����"�9'����������"�������������� .��.�����!���&���9'���������.����� �!���!���� ������� '�� ����..������� ����������.��!���.��.������ � �������� �����"G&�������������� G����������������.����� �!���!���������� ����!������� � ��� �'� ����� � �����"�����68��0���� �����"G0� 68��G�������������"���0���� �� ����0������������� "�'�&����@���� �#����9'�������������.��.������!���0���� 9.���!����������8���0��*��8�+9��#������ � "����C��&� ����9'�����������"� ��� ��� "��������"!�����0���� �����"@� 68��A ����(*+�� ���@� 68��A ����(*+�����"9'�����'����� �����"����8������..������� �����������0���0�"������ ��� "��������0� 68��G����(��� �*��� "���� "������ ��� "�� �� ��9����� 2�!����������+�� ���� "9'������""�"0��������0��� "���0���� �����!���G�� ������������� �$��������������'�������"���%"")� ��*+�����"�!!���������""� ������!�� ����������� ������������� �!!������������"��(�� ������!��.��� �������.��������� ��� �����"���� 9���!����'� �'� "�''���0�"�.����"�

$�'����� .�����������68��@���� ���������0���� �������0�����8������..������� �

Page 190: 3Programmer's Heaven C# School Book

��������������� ���������

/?

@ �����&�� �*���!� ����2(�;����������2�3��$�����(�3 ������<�

,���!�������9���'���0��� �5�������"���$����"�����.�� "�'�..������� � ���5�������"���$��.����"������!���������."�����.�..������� � "�����������!'��(!�����.����������5�������"���$��.����"���� "��"��"��"����� "��6!������$���..������� 9��� �'������ "��""�0�����9.��7���� "������ ���� �9!���"��� ��9� �������"���.����� "����!�������!�������

������ &��������1����$ ����

���5�������"���$����6.����"���� "��"��8��"������'�����$���..������� ������8��"��������"�"'���� ������� �� "�������.������ ������� ����������8��"��������0������������"�!!��� ��.��� � ���.�������� ��� ��8��&���8��.��9'�� ���.�����"���!������ ����!� �07���9�����6�������������.����"���������!���������0��*.��.�����9�����"9���+�!����07�������!����'� �!�������'� ������� ���'��(� ���5�������"���$����6�

���������.����������������� ��8������ ����.������������.������"�����!����'� �!�������'� ���������� �!���������.������'��(� 5�������"���$���

Page 191: 3Programmer's Heaven C# School Book

��������������� ���������

/�

����� ���

A ��!���.���� � �'!������� ���"���"� 5�������"���$�����"�!��"� ��������"�!��"� �9����� !��"I� !��"�����"��� ����4� "E��0���<����������"��� 0�!��"�"I� !��"�"��������.�0�� "���*�����"9���9 ���.���9.��.����9���+�:���� ���"�!� ������ '���� ������"�� "�� !��"I� !��"�����"�'���� �������� ��������� �"�!� �"�� ���������� ����� "����� .��.������"���������

������������$ ���'�&��� ���� �������2��������

5�������"���$��.����"�� � �������"���.���������.���� "�8����������..������� "��� �"�����.�� ��:���� ���������.����� ��������!�����������.����.��7���� "������ *�����.�!!���������(��.� �..������� +�A ������������.���"�����..������� 9����� "�0������ ����5�������"���$��"�0������:���� ��� ������� � ������!�������..������� �� �5�������"���$��O

����2�� �����

�����.��������!��!�������!���5�������"���$����6���!���"��� ������!���"��� ������'�����"��� ������.��������� ���!���7��0�.���� ������ ����� ���!���!����������0�8�:���� �������!.��.������!���!���� "���� ������ �������.�����'� "�'����5�������"���$����6�������������'���������"�� ��������!�������.��������� ����� ���!���� "��� �������.��.������:���� ����������6��������� "���.������ ��� "���!�������� ��������!����'� �!�����.��� �� � ���"���������'�!���5�������"���$��&������� ��� "��"�!!��� ��..���� �'� "�'�

Page 192: 3Programmer's Heaven C# School Book

��������������� ���������

/�

:���� ���������0�8'� "�'�������!��� "�"�*� +� "���.��.�����'� "�'������������ "�"�*�1+�!����0��� �.�����������0�8����'������"""�!!��� ��� ����������!����A ������� �����.����"� ���!���9����� ��� ����������.��.�����!���������.�����'� "�'�:���� ������ ������������ � "�C��!����� ������ ���������6�� �.��.������� 0���� ��"0�'����� ������6�� ���.�����.� �*�;+� ������.������� "�'� �������0�89���.������� "�'9���.�� "�'9������� 68.������� "�'9���5��'�� "�'9A��.���� "�'� "��������.� �'� "�'� 5�������"����6�� 0���!�����(� �� "%�����"� ���� "�'���������!��������"��..���� ��'�� �������!���*����������������.�� �����������������������������(+9� "��"�'�� �������!����%'� "�'�� 0���!��������"�0����0���� ���(�"�B� ����0���!�����������""� '� "�'�����'�������0��������������!�� "������� ".� ��!���!���"��� ��'� "�'����������� ".� �����(�"'����=� ����0���!������ "��������������'9���.� "������ �8.�����'� "�'� �����""� ������!����!����'� "�'��� �����0��� �����������"����69����� ��(��������0��!������5��'�� �� ����� "��"�� �0���

&��� ��(0$�����

���������� 68.�������������!��'� "�'���.��� ����!���������(��.��������� � ��������������%������ ����������� �!������.��7���� "������������������(��.��$���..������� �%������ ����� ��� .��7���������"� "�!!��� ��$��0��"�� �������(�5@�$��95���$��� "5�44�$������!����'� �!�����.��� �� �.����!���5�������"���$��������� 68.������

Page 193: 3Programmer's Heaven C# School Book

��������������� ���������

//

���)�!��� �� �"��� ��� ���������0�����!��� ��"� �����.������.��7����G%..����G������� !���!������..������� �%��0��� !�������!��������� ��� � !������� �0����������� ����0���&��� ��� ����0���!��������� ����!��������!����!���.������� %�$�������� ����"� ��� !���9���.��7�������"� ���.��7!���� "���������"�����"� ���!���������.���� ���� "���� "�����������7���� "������� ����� "��"�!5�������"���$��� "��� �������J������ ��!� ��$���� ������� !���9����� ��������.����� ����� �'����!� �.��7����������� �

/����� ��� ��@ �����&�� ��*����2(�

&���,� ��<�"��������9�.� 9���� "�������.��7���9������ ��� "���"��������!���� 6"��,� ��<�"!����8��"��� �� "������ �� ���5�������"���������"��"����� 5��',� �������"��.��� !������ �������0������!"�!!��� �5�������"��'� "�'� "��'����0��'�� ��"�� ""��� �����'� ���7���,� ��<�"!������ �"�!!��� �.��.������!���5�������"�����7����%5�������"��.��7�������������� �!!���������(��.�� ������0������ ����07���!���*'�'����8.��������� ��.��!���0���� ���� ���� +� @���",� ������� ����"�����.���� "0���"��������!���9.��7����������� ����������!�0���"�� �8�����0��!��������"���0����� ��0��,� ������� �.����"��������.��� ������"�����5�������"���$����0��������0���� �����.�����!!� "� �������������� ���.������9� "�"�0��������.��(����.���������� ����,� �������"��������.��� !������%���� �$��

Page 194: 3Programmer's Heaven C# School Book

��������������� ���������

122

&�����,� �������"�����������!��!���.������ !��!������� ������ ����� "������������ ���&������� �����'� ����,� �������"������������������!��5�������"���$�������

B� ���@ �����&�� �*������� ��� ��I1����5 ����I�-$$� ��� ��

$�''�G����"�J���(�����!5�������"���$��9���G�����5�������"���$����6��0���"���H������� &���H�..������� '����'�������"�������� ������ �

����� �������6�"������

&����!���9'� ��"��������� �'���� "�'%..������� ���7����&�����9����5�������"���$��� "����(&���G$�'G���7������'�����'���!����'� ����� �

&�������0������� 9�����G5���������7���G� ���7�����.�� "G�� "�'%..������� G� ���.������������� ����!��� �'.��7���*G#��� � ��� &���G� ����0���!�����+� �����8�0�8��0���"$�������������������� '�������'�����������.��7����� ����@��'����@���� � "����(A>���'�����'���� ��.��!���� ���"��� �����'�����������!�����0���'�

Page 195: 3Programmer's Heaven C# School Book

��������������� ���������

12

����������!���!���"��� ������ ���0����'���"�!!��� �!������� ���' �0����:�������0�8� ".��.�����'� "�'���0����0��� "��������'� "�'��� ��0����0���:���� ��� ���������� ��� ����5��'�� ��"����0�"�������� ������ �

&��� ���3�� ���$�$��� ���.�� ��.���

:���� ��� �����"�!����.��.������!���!����� �������.�����'� "�'�&�����9�����*����(+���!���� "��������.��.�����'� "�'*�!���.��.�����'� "�'� �����0��� ���������� ".� �9�����5��'G���.������� "�'+�$�'��� �����������!���!���� "��� ����!���!���G���� �����"�0���� �� ������8�� "$���.��.�����9���' � ���!����'� �!������

Page 196: 3Programmer's Heaven C# School Book

��������������� ���������

121

- ������������� ������

$�'��������#�0���� ����!����������0�89.������� ���!���� "���C�����..��.������������*����(+�����0��� ���!���� "!������.��.�����'� "�'9������8�.��.������H������� &���H� "���$���.��.������G�0�3����� �G���� ����!��� �������� ����!�������.� "� �� �� ��� ����������"��$�'��������@���� �� ����!����������0�89.������� ���!���� "���C����..��.��������������*����(+���0���� � "����$���.��.������G0� 68��G� "����8�.��.������G68��G�� ����.��.�����'� "�'����!�������" �'���(��(�����

Page 197: 3Programmer's Heaven C# School Book

��������������� ���������

12;

- ���(3����1��� ���

$�''� ��"���""������ ��� "�� ���"�!�����68��0���� �&�����9��.��"��0������(���68��0���� � ���"��� ������'���������� �'��� ��� "���!�����68��0���� G����(��� �� "��(����������������"����' � ���!����'� �!������

����������"�����������..������� *%..������� �68��*++� ������ ��� "���������6�� ��� ��������"������ ��� "���0���������������"��'������68��0���� G����(��� ��

(0���� ���� ���$$� ��� ��

�������O���G������� &���G�..������� ����.����������.���� "�8���������..������� 9�����@���"G@���"������� *��.������4���!�4@+� "��� �������0��G��������������0���� �*��.������4&=+� ���'������.���� "�������G������� &���G�..������� � � �''� "�'���' 0���'�

Page 198: 3Programmer's Heaven C# School Book

��������������� ���������

12B

����������..������� 9����(���68��0���� ���������0���� � ��������0���!���'� "�'� ����������� �-�������������������� ����/������6�� ������ ������������������������������+����3��<�����3���

����������,������������������������������=��+,����� ������������+,����� ��>��������������$������������������������+,�

���� ���%������ � �

static void Main()

{

Application.Run(new MyForm());

}

,�(��������� ����� ����!���!���� ,�� *+�����"'�� ���������� ���� ���� ������.������� "�'� ��"��������"����� �����.������ ������

8 ���������������#�� ������2�� �����

:���� ������0��'�� ���&������� ��� "��"��� �5��'G���� ��� "5��'G��"��%!���'����� ��������"�9���'���!� "�����"��� �����"0����!���"��� ����0�����������������'�����'����� �������� ������ ���� "���� "�����"�0�����9'�������� "������ ������������ �� "����� 0�� "�����

B� ���/����������

$�''��������� �!�������'������5�������"���$����6� "���� ����9���G���� �0������������ �����$�������'�'��� �����.������"�!� �� ��� ����� "'���"��� ������ ������!������������ .��.������ "��� ��%�������!����!������ �����.��� ��"0���'�

Page 199: 3Programmer's Heaven C# School Book

��������������� ���������

12=

�� ���� �����.��� #�0�� <�"��"�.��������8�� ���!����� �� ���!�������� "�'�&����#�0�����.���� �

.��.����������8�9$���� "&� ��<�������� ���� ���� "��"!�����#�0���� �����@���� <�"��"�.�������@���� � ���!����� �� ���!�������� "�'�&����@���� �

��.���� �.��.����������8�9$���� "&� ��<������������(��� ���� "��"!�����@���� �

��8�@�8 �����"�������'���� ������'����I�"����8��� �� ���!�������� "�'�&������8�@�8���.���� �.��.����������8�*���������.��8�� "���'������������ ����"+9$���9%��� �� �9,������ �*0����� +9������@��*����!�����0���������"'��������8�0�8+9)��"A ��*0����� +9���"���.*0����� +� "��'��"����*�����������"!��.�'��"��(� �+���.���� ���� ������8���� ��"*"�!����+� ">������

3���.@�8 <�"������.������� ������ �� ���!�������� "�'�&����3���.@�8���.���� �.��.����������8�95��0��*0����� +� "6 �0��"*0����� +�<�������� ���� ���� "��"!�����3���.@�8�

)�"��@���� %���'������������ �����!�� �������0���.��� �)�"��@���� �����������"� �����.�� ��� �"� �3���.@�8�A ��� ��!���)�"��@���� �� 0�������"� �����.��������� �� ���!�������� "�'�&����)�"��@���� ���.���� �.��.����������8�9$���� "����(�"*0����� +���.���� ���� ������(�"��� ��"�<����������� ��!�)�"��@���� ��� ���� "��"P���������������"��������"� ��!��"� �������(�!���.��0���� ������� � ������� �����

����(@�8 %���'���������(��� ���(�0�8�����������.��!��� ��!�������� ������(@�8������������"� �����.�� ��� �"� �3���.@�8�% �9����� � ��!�������(0�8�� �����.�� 0�������"�� �� ���!�������� "�'�&��������(@�8���.���� �.��.����������8�9$���9����(�"*0����� +� "����(��������.���� ���� ��������(�"��� ��"� "����(�������� ��"�

B� ���3�� ���������� ������$$� ��� �C�"��������H��& $$ ��������

$�'���G�������G����������G���..� �����G�..������� ��������������G���..� ������� � �� �0��(��������0����� ��0���������������0��(� 0���!���.���� �� "� � ������ ������..������� '���!� �������(��(�����

Page 200: 3Programmer's Heaven C# School Book

��������������� ���������

12?

%����� ��9'�������"���3���.@�8*� +9����(@�8�*�1+9)�"��@���� *�;+9��8�@�8*�B+9#�0��� "@���� �� ����� ����0����..������� �

2�� �� ���� ��.������$��� ���� ���������

&����!�������5�������"���$��� "������� �'�� "�'%..������� ���7����������.��.������!���!����� �������.������� "�'�����!����'� �������E ��C�\B?29;B2����������� \�� �������� $���\,�&��� ��8�\����������G���..� ����� ���������������� ���������..����� �!���!���� ������� 9$������� ����!!�������� �����"0����"��� ��� "��8�����������!���%..������� G�� "�'� $�'�""�#�0�������!������' � ���!������0������������8�.��.�����!���#�0����G����������G���..� �����G� $�8�9!����������0�8'� "�'9�""�3���.@�8�����!����,�"�!����C��..��.����������������"������������(0�8�� "���0�����������8�.��.�����!���3���.@�8��G%�����0��@��(G� "���$���.��.������G�08%��0�@��(G &����������0�8'� "�'9�""���������(0�8������3���.@�8���7����"����������8�.��.�����!������������(0�8���G��������� ���*e12+G9G���!��� ����*e;2+G� "G��������*e=2+G�%�������$���.��.�����!�������(0�8���G�08������G9G�08���!��G� "G�08��������G�%""�#�0��� ���3���.@�8� "������8�.��.������G���������0��(���'����.������G9���' � ���!������

Page 201: 3Programmer's Heaven C# School Book

��������������� ���������

12�

%""� �����3���.@�89������8�.��.������G,�"��!����� �G9��$���.��.������G�08����� �,�"�G� "��6 �0��".��.������G&���G�,�(� ����6 �0��".��.����G&���G'���"��0���������.0�8� "������� �� ��������.9� "��'���� ��0�� �0��"*���������"� �+'�� ��������������"���0��(�%""�'�)�"��@���� � ���3���.@�8���' � ���!��������������$���.��.�������G�0 &�������� �G� "G�0 � ������ �G���������8�.��.�����!�����"��0���� ��G&�������� �G� "G� ������ �G�����(����!����.��� *&�������� �+���"�!�����.��� 9��������(�".��.�����������%""�#�0��� ���3���.@�8� "������8�.��.������G�����������"��!.���� �G9���' � ���!������ %""����������0���� � ���!���������$���.��.������G0� �������G9����8�.��.������G�������G� "��6 �0��".��.������G&���G�%��� ����������0���� '���0�� �0��"� ��'�� ��������������"���0��(� %""���8�@�8� "�#�0�������!������������8�.��.�����!���#�0����G����� �G���������8�.��.�����!�����8�@�8��HH*��.��+9���$���.��.������G�8������ �G� "���,����#� �.��.��������������.��.���!���� ����,����#� �.��.�����������������'�����8�@�8��������8�����.� ������� � ��� �����"�!����������!���,����#� �.��.�����&���� &� ����9�""� 68��@���� �����!���������$���.��.������G0� 68��G� "����8�.��.������G68��G�

5� � ������.��(3����1��� ���

&����!����""� ��� ��� "���!�����68��@���� G����(��� �0�"��0������(� �� ��� ���"��� �����������!����'� ���"����8������..������� � private void btnExit_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

��� ��������������"� �0��(9���G,�"��!����� �G����.0�8� "����������0���� ����"0�� �0��"9� "�!������0��(���� ������"9��������"0�"��0��"�����"� �0�'���� �� ��� ��� "���!������(�"��� ��"��� ��!�������(0�8�����""� ��� ��� "���!�������(0�89������"��0������(�������(0�8� ���"��� ����"��0������(���G����(�"��� ��"G��� ��!����(0�8� ���6�� �.��.����'� "�'��������(�"��� ��"��� ��!�����(0�8���������"'�� �����������(0�8�����(�"��� ����(�"��������(�".��.�������� ��"���������!����'� ���"�� ������ ��� "����!!�������(0�8� private void cbxProgCS_CheckedChanged(object sender, System.EventArgs e)

{

if(cbxProgCS.Checked == false && cbxProfCS.Checked == false && cbxCSSchool.Checked == false)

{

gbxPaymentMode.Enabled = false;

btnPurchase.Enabled = false;

Page 202: 3Programmer's Heaven C# School Book

��������������� ���������

12�

}

else

{

gbxPaymentMode.Enabled = true;

btnPurchase.Enabled = true;

}

}

� �����"��0����!����������(0�8����� ����(�"9'�"��0�����G,�"��!����� �G����.0�8� "����������0���� P�����'��9'�� �0������� ��.�� ".�����������"�!���������(�"��� ��"��� ��!���������'�����(0�8�*�08���!��� "�08��������+ $�'�8��������.������� "����(I� ����("�!!��� �����(0�8��:��'��� ������!� ��!�������(0�8��������(�"9���,�"��!����� �����.0�8� "�������0���� ���� �0��"9� "�! � ��!�������(0�8��������(�"9�������"��0��"� &� ����� �������(��� ��!����������@���� 9���.����������""�.������������ ��� � ������!0��(������"9������������!���.������9�����"��!.���� �������"� "� ������ ���������.����"�"�%""�����(��� ��� "���!������������0���� 0�"��0������(� ���� ���"��� ��� "'�������!����'� ���"�� private void btnPurchase_Click(object sender, System.EventArgs e)

{

string message = "You purchased:\r\n\t";

int amount=0;

if(cbxProgCS.Checked)

{

amount+=20;

message+=cbxProgCS.Text + "\r\n\t";

}

if(cbxProfCS.Checked)

{

amount+=30;

message+=cbxProfCS.Text + "\r\n\t";

}

if(cbxCSSchool.Checked)

{

amount+=50;

message+=cbxCSSchool.Text + "\r\n\t";

}

string paymentMode="";

Page 203: 3Programmer's Heaven C# School Book

��������������� ���������

12/

if(rbnFullPayment.Checked)

{

paymentMode = rbnFullPayment.Text;

}

else

{

paymentMode = rbnInstallments.Text;

}

message+="\r\nThe total payment due is $" + amount.ToString();

message+="\r\nThe selected mode of payment is: " + paymentMode;

if(txtComments.Text != "")

{

message+="\r\nYour comments about us are: " + txtComments.Text;

}

MessageBox.Show(message, "Summary");

}

��������"����������0��� ����0�����"������ ����������0������ ����"�����"������������ ��!.������9������ ������0��.���� �,�"����"�����"���������"��"��!.���� �� "������ ������0�������'������"������������������'���!� ����0�"�.����"� ���,����@�8�&���'�����(�"�����!�������(0�8�� "���������"������������ �P������������'����0���"�������!0��(������"� ������ ������0������������� !�� "�����"��!.���� �� "�� ���� ���"*�""�"+�����������������0���$�8�'��� ���� ���"��������.���� �� "����� �.����"�"�������� ������0��������&� ����9'�"�.����"��������������� �,����@�8�%�����0�8��"�����'� "�'����.�.�.'����������%�����0�8����"��(� "�!"�����0�89'������� '�� ���"�.����"����� ������� ������'� "�'�!�����..������� � ��������������"��������0�8����,����@�8���G��������'�����"���� ��������"�"!���P���� �'���"��(����� ���"�.���� ��������0�8� "���������!��������0�8� $�''�� '��8��������.������� ".�����.������0���� �!��������� ����0��(9'������!����'� ����.���

Page 204: 3Programmer's Heaven C# School Book

��������������� ���������

1 2

����.�������������"���������"� "���� "� ��!�� "�'�..������� � "�� &���� ������ �������������(��������� '� "�'.�������� ����������..������� ���"��� � "��������8.����� ����.��!���9���0������ "��� �������� "���� "� �'���0��$�� ��� ��������������.��.������ "��� ��!����� �����:������8.����� �� ""������������!����!��������!�

&�����$������" ����.���� �� ���5 �6��-$$� ��� ���

�� ,�(�����!�����������.��� "������ "���� "������.���� ������������!�����..������� !� "��!�����������0�����������"0��8.����"� "����" ����.����������

�� ���&������� ��!���5�������"���$����6�������!��'�� "��� � ����!������������.����"�� ��0���!��!���.��� !������ �� �� "�C��!����� �����

�� %������������ �����������������.��.�������(�#������ 9��C�96 �0��"95��0��9��0� "�89���������0� "�8.��.�����������.���� ����"����0�����J�� ��!����'�"0����'� "�'!���'�� ������.��������00���� � ���(��0���"�

�� ����� ��������"0� ���"����������.��.���� 0������ �C�"9����9'����� ���"����������0���� G0� �������G� ���.�������8��.���

�� %������� �'��� ����� "��"�� �� ��� 9�����!�����""������������.��!�8����� ����!������ ��������������������� �C�0��0������ �������������������� 9'�����!����'�"����� �� ��� 0�.��!�8� ��#�0���� ����G ���'����0�*�0�3����� �+9��8�@�8'����8�*�8������ �+9@���� '���0� *0� �������+9����(@�8'����08*�08������+9)�"��@���� '����0 *�0 &�������� �+� "3���.@�8'����08*�08����� �,�"�+�

Page 205: 3Programmer's Heaven C# School Book

��������������� ���������

1

����/���5 �6���������!�&������2 ����70���

������"����

��"��'�'������� �0����������'� "�'�� ����� "�� "��""�����0�8����'����������0����(� ��������������� �� ����9�������#��0�89���0�0�89����5��'� "#��5��'�� �����#����9'�'������� �0������������� �� ����9� ���"� ������� �� �9��������9����0��� "�����������(���&� ����9'�'����8.��������!����� "��""�����0�8�9�������A.� &���9����&���9&� �� "�����������@�8��

������ ���������

�����!���'� "�'�� ������ ��� �����.��!��������� 9��(� ����!0��(� "������ �!��"���������������"��������� �� �����68��.���!��������� �� ����� ���"����#��@�89���0�@�89����5��'9#��5��'9����0��9�����#��� ",�� ,� ��� ������ '�'����8.���������� ����� �0�� ��

� ���70�������

%���0�8�� ������ ��� �����!����'������ 0�������"0��������%���0�8�� 0���������'�������������� ������������� �$��9������0�8���.��� ��"0������������ "�'�&����#��@�8����6������0�8� �� ���� ��� ���������� �����"G����G�������"�������.��� �� �������% ����*���������� �+�� 0��""�"��������"!���������0�8� ���!����'� ����� ���.��� ��'� "�'�� ��� � �����0�8*� +� "����0�0�8*�1+�

%���0�8�� 0��""�"�����!���!������5�������"���$������0���<��������0�������""�"�0���������0�8����������������!������0�8*����9�����0��G%�����0��@��(G� ����0���.������+����$���.��.�����!������0�8���"������� ����!������0�8�07���� �����"���������� "�'�&����#��@�8�� ��� �.��.���������"G����G'����.����"���������������� ��� �"� ������0�8����������������� �� 0���"�����"9�""� "����������!���������0�8�

Page 206: 3Programmer's Heaven C# School Book

��������������� ���������

1 1

- ��� �������� ��� ����0�

%���0�8�� 0�.�.�����"��������"��� ������ ����5�������"����6������ ������ ���"�����""������������0�8�� ����5�������"����69�����������0�8�� ����� ���"��� ��� "� ���.��.�����'� "�'����(�������.��.�������'�����'���� � ���!���'��������� � ����������!������0��""�"��������0�8� %���� �������9����� '������"����""������������0�8�% ������ 0��""�"��������0�8�� ����%""*+�����"�!���������������� �!���#��@�8�07����%�������'�������"�����0�8��������� ����!0��(� "����'����������$���.��.�����!������0�8��G�08@��(G����!����'� ���"��� 0���"���""������������0�8� lbxBooks.Items.Add("Programming C#");

lbxBooks.Items.Add("Professional C#");

A��!���'� ����""������!����9����� �����%"")� ��*+�����"�!���������������� lbxBooks.Items.AddRange(new String[] {

"Beginning C# by Wrox",

"Professional C# by Wrox",

"Programming C# by O' Reilly",

"Professional ASP.Net",

"Beginning Java 2 by Wrox",

"C++ - The complete Reference",

"Java Servlets Programming",

"Java Server Pages - JSP)"});

-����� ��� ����� ��� ��� ����0�

���������������� �!������0�8.����"����� "�8��.��.������������'����� ������0�8��0������".���������������*������������"�+� lbxBooks.Items[0] = "Program Development in Java"; // changing list box item

string book1 = (string) lbxBooks.Items[1]; // reading list box item

� ����0�����"�9���!��������� ������� "�8��.��.�����!���������������� ����� ������0�8����� "������ "������ ����"� ����� ������0�8�$�������'������..���"��������������0�8������������� ��.��!�������� "�8��*����MN+��07���� :�����������9���� ����9�������� ���������"������ �����������"����.��.�����!������0�8� MessageBox.Show(lbxBooks.SelectedItem.ToString());

Page 207: 3Programmer's Heaven C# School Book

��������������� ���������

1 ;

9��3 ��� �����.���� ��� ����0�

� "���"��������� 0�������"!���������0�8������0������ ����)�����*+��)�����%�*+�����"�!���������������� �!������0�8����)�����*+�����"����.�� �07�����0�������"!���������0�8� lbxBooks.Items.Remove("Programming C#");

����0����� �'����������������G��������� ���G!���������0�8�:���� ��������)�����%�*+�����"'��������.����� "�8�!���������0�������"!���������0�8� lbxBooks.Items.RemoveAt(0);

����� �'����������������� ���� "�82*���!�������� �+!���������0�8�&� ����9�������������������� ��� �"� ����0�89����� ������������*+�����"� lbxBooks.Items.Clear();

� ���70�(3�����

��������.���� �� "!��J�� �����"��� ��!������0�8��������"� "�8��� ��"������� ����������"'�� � �����������"� ������0�8����""� ��� ��� "���!�������� �9"��0������(������0�8� ���!���"��� ���

Page 208: 3Programmer's Heaven C# School Book

��������������� ���������

1 B

���!����'� ���"�'�����'���������"����� ��������0�8'�� �������������� � ������0�8���� ��"� private void lbxBooks_SelectedIndexChanged(object sender, System.EventArgs e)

{

MessageBox.Show("The selected item is " + lbxBooks.SelectedItem.ToString(), "Selected Item");

}

����70�������

������0�0�8���������������0�8� ���������"��"�.��������!�����������0�0�8�.��� ��"� �$�������������������� "�'�&�������0�@�8����������0�0�8������������"��� '������ 0�������"�� �������0�@�8���GG���.��' �����G.��.�������������"��� ��� ���"G���.��G9G���.��' G� "G���.��' #��G����!����'� ����� ���"��� ��������������"��."�' �����

������.��������'�������!����� "���������"����������������� �.���������������.��"��."�' �������0�0�8���.��������"� ���!� �"�����0�8*"����"������ ������ +�������.��' ������'�������� �"��."�' ������������ '����� �'����� ����8������������.��' #������������������"��."�' ����0�������������� � �������� ��!���������0��������� "�� ��.����"�� �'���������!� ������ 0�� ����"9������"� "�����"� 7��������'������'���� ������0�8�

8����@ �6�

�������5��'�� �������"!����������������.��� ����� �!����������.��� ��"� �$��0����������� "�'�&��������5��'�������!����'� ����� �������� �8��.���!��'����������'�� ������ ���(�

Page 209: 3Programmer's Heaven C# School Book

��������������� ���������

1 =

8 ��8���*��( ���

��������'�����""� "������������� "!�������������'�� ��������������G$�"�G.��.����� ���!���"��� ������ �������(���$�"�.��.����� ���.��.�����'� "�'�!���!���"��� ��9��'�����'���!����'� ����� 9'�����( �' ��������$�"�6"�����

Page 210: 3Programmer's Heaven C# School Book

��������������� ���������

1 ?

G%"")���G'����""���������� �*� �'��� �.��� �����9����"� ����0���'� "�'+�G%""����"G'����""�����" �"������������"����� �������0���!��� �"��� 0���� ��"�� ������8�0�8��0���"G#�0��G�% �"��� 0�"�����"�� ����G������G0���� �

- ��J9��3 ��� ������������ ���

������ 0��""�"����������"!�������������'�� ��������$�"��"������"��� ����90������!�������'� ��"���""I�������������� ������ ������"���������5��'����� ��� �.��.���������"G$�"�G'����.����"����������� "���"��� �"��!����� ��������%""*+�����"�!���$�"���������� �� 0���"���""���8������������$�"��07���'��������!�����������"���� �"�����!����'� ���"��""���.���������������� ���!���G#��"��� �� private void MyForm_Load(object sender, System.EventArgs e)

{

treeView1.Nodes.Clear();

treeView1.Nodes.Add("Programming Languages");

TreeNode node = new TreeNode("Object Oriented Programming Languages");

node.Nodes.Add("C++");

TreeNode subnode = node.Nodes.Add("Framework and runtime based languages");

subnode.Nodes.Add("Java");

Page 211: 3Programmer's Heaven C# School Book

��������������� ���������

1 �

subnode.Nodes.Add("C#");

treeView1.Nodes[0].Nodes.Add(node);

}

&���'������""�"����� �"���0���"G��������� �#� �����G������ ������"� �'����$�"�� "�""�"�0 �"������&� ����'��""�"��� �"���������� �"����� '��8��������.������9���!����'� ����� �.��"���"�

Page 212: 3Programmer's Heaven C# School Book

��������������� ���������

1 �

8����@ �6�(3�����

����5��'��� ��0���!��.���� ���� �9����!'�����������"0���'� 6�� � �����.��� %!��������� &���"'�� � ����* �"�+�������"� ����������'�� ���������5��'6�� �%�����

.��"'��������� �'������ ��� � +����5��'%���� � �������� '����"����0��������� ����"���������� ��(�@�>��0���"9@�,���9�����.�9��� 1+$�"��07���'������.��� ����������" �"�����������" �"��� ���0������"�� �����������"$�"�.��.�����!����������'�� �����

@�!���68.� " &���"7��0�!������ �"���8.� "�"�@�!��������.�� &���"7��0�!������ �"�������.�"�%!���68.� " &���"7���!������ �"���8.� "�"�%!��������.� &���"7���!������ �"�������.�"�@�!���#�0��6"�� &���"7��0�!���� �����.����"����"�����#�0���!��� �"��:�� ��"�������

#�0��6"��.��.�����!����������'�������!���'��������'���������� �������0���!� �"��

%!���#�0��6"�� &���"7���!��������0���!� �"���0�� �"���"����!����'� ���� ��� "���'�����'�����0���!��� �"�� �,����0�8'�� �������������"� private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)

{

MessageBox.Show("'" + e.Node.Text + "' node selected", "Selected Node");

}

Page 213: 3Programmer's Heaven C# School Book

��������������� ���������

1 /

���.������'������(��(����'�� �� �

������� ���������

������������� � ���0���� ����������������"����������������0���"� ������� ����9��������������'��������'�� �����% ��������*��(����� �� �9�� ��8��� �9����0������ "��""�����+��""�"� ����.���������������� "��"�� ������� ����.�����.��� ����� � �����..������� ������.��� ��"� �$��0������������ "�'�&���������#������ ��� ��������������������� ����!������5�������"������0�8� ".������� ���!���9�������������""�"���������� "�"�.����"0���'���!���� ���"��� ���� ��� �:���� �������!��������� "��� ����� ������.��.������ ���!� "��� ���.��.�����!��������������$����% �����#��������������!�'.��.��������������.���� �����G�����G��������� 9'�������"������������"� ��������������� �������(��������.��.�����!��������#��� 5�������"��G"��� ��9����'� �������������� �"����'��������'������"""�!!��� ��0�.9�7.�� "���!.����������������������������� ����G�������C�G.��.������.��� �����C��!��������� ����������"�!����� ?9 ?� ��5�������"��'�� �� ������ �������� �!�228?22�

-���� ���-��������� ����� ..�������������

% ����������������"��"�!!��� ��� ����9������������'9������'������0�������������'� "����0���� ���������.��.���������"�����#��'�������"��������� ������������������� "���"������������������"��� "���"��� �"���0���� '�� �""� ������&���8��.��9���%""*+�����"�!���$�"����������

Page 214: 3Programmer's Heaven C# School Book

��������������� ���������

112

�!����������'�� ������� �������"�"����� '��������.������8�!����� �"�� "���� "�8�!��������� ����������"���������

� ���@ �6�������

���#��5��'�� ������������.���� �� "� ������ ��� ����������"�����"�����!����� "�� "�.�������� !���"�!!��� ����'P#������� 9�������� 9#��� "�������������!����G�� "�'68.�����G�����������'�� ��������'"�!!��� ���� �%������'���.��� ��"� �$��0������������ "�'�&����#��5��'�������!����'� ����� ���"��� �����!���"�!!��� ����'�!���������'�� ��������G5��'G.��.�����!���������'���"����� ��������'�!������'�� �����

86�������� ���� ��� ��� ���@ �6�������

�'����������� 0��""�"���������'�� �����A �������"���#���������#��� "������������"!�������� � �������� ���'�����������������������"����������#��� "������������"!�������� � ������� 9���� ""��������'�<���������'������������������'������"�!!��� ��� ��� ������������C�.��.�����&���8��.��� ����0������� ���9����C��!������ �����������������;19;1'��������C��!������ ����������������12912� "'�������"�����������!������'���������P� !���'�7����.�E.���� ���������� "��� ��"����C��!���

- ��� �������� ��� ���3 �6��������� ����� �����

5������"��.����"�� ���'�����""���������������'�� ��������""���������������'�� ����9��.������(���G����G.��.�����!���������'� ���.��.�����'� "�'���'����.� ���#��5��'��������� 6"�����

Page 215: 3Programmer's Heaven C# School Book

��������������� ���������

11

��������� �����%""0���� ���""�������������6������������8�.��.����'������.��� ������8�"�.����"'������������6��������������������� "�8.��.����'������.��� �����������0��������"'�������������������������"�"!�������������"��������*% ���������� 0��������"�� �#���������#��������������#��.��.�����!���������'�� �����"����0�"�������+��!�����E����� ������'���"*'����'�'���"����0������� ������ +9�����0����.��.������ 0���"���""����0�����!� ��������������9������ 0�������"�� ����)�����@���� �

- ���������������� ����� ������

���#��5��'�� �������.��.���������"����'�������������������� �!#��5��'������0�����"� ���������'�� ����������� 0��""�"��������"�� ���%""*+� ")�����*+�����"����%""*+�����"��������������"�"����� �A ���(�� ������ �'�������"����.��� ������8��!�������������� "� ���(����� �� "� ��������� "�8������� ����"��!�������8���0��� "��������� "�8���"�����������.��������������!�������������"�������������������������"� ���(����#��5��'�����07�������!����'� ���"��""������������������' listView1.Items.Add("Disk"); // text label is passed

listView1.Items.Add("Disk", 0); // text label and image index is passed

ListViewItem item = new ListViewItem("Disk", 2); // a new ListViewItem object is created

listView1.Items.Add(item); // and added to the list view control

Page 216: 3Programmer's Heaven C# School Book

��������������� ���������

111

(3�����.��� ���@ �6�������

������!��J�� �����"��� �!�����������'�� ������������"� "�8��� ��"��� �'�������������"'�� ���������� � ���������'���� ��"����!����'� ���� ��� "���.�� ����������"����G��8�� ��������0�8'�� �������������� � ���������'��� ��� private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)

{

foreach(ListViewItem item in listView1.SelectedItems)

{

MessageBox.Show(item.Text);

}

}

/� ��/����

,�� �� ������""�"�����!�������������,�� �� ����.��� ��"� �$��0������������ "�'�&����,�� ,� �����:���� 7��.��(� ".�������,�� ,� ��� ����!�������������"������0�8� �����!����%������5������"��'�����'� ��.����� �� ��������.�!���!������� ����� ����!������ �� �� "�""�.��� � ����� �0�����(� ����

������.���� �.�� ��0������ �� �����

�� :���� ��� ����� ���� "��8��!������ �� �����!������.��.�����'� "�''���������� ���� ���"��� ���

�� :���� ����..���������(��*��(�����4���%��4&=+!������� ������� ����.��.�����'� "�'� �� :���� ��(��"���"�� �*7����(����� �0��'�� ����� "68���.��� � ����0������� ���+�

����� �0�'���� �� ��"��E� ����� �������8�� �� :���� �""�����(0�80�!������ ������� ���)�"������(.��.����� �� :���� �""� ��� ��� "���!������� �����7��0�"��0������(� ���� ���"��� ��

Page 217: 3Programmer's Heaven C# School Book

��������������� ���������

11;

8��7���

����0�������""�"��(�������������*�� �9��������9���+������.��� ��"� �$��0������������ "�'�&��������@�������������@������� ��� ���������� �����"@���� '������ 0���"���""I����������!����������0���% ���������� ���0��������"'����������0������ �������(���@���� .��.����� ���.��.�����'� "�'95�������"��.��� �������!�������� ���!������""� "������0���� �6�� ��� "����� 0��""�"!���������0��7��0�"��0������(� ����0���� � ���"��� ���

2����8 ���" �:���

�����������(���� ���������� ����"� ���'� "�'� ���� �� �������'������������.���������"����� �$�������.��� ��"0������������ "�'�&���������������(���������� ��.��0�������"!����������0�8� ".����"� ���!���� ���"��� ���<�����'�� ����� ����$���.��.��������!����'� ����� �����'�!����� ��� � �"�������.��(���� �����

������!��J�� �����"��� �!��"�������.��(���5������� ��"������������"'�� ������������ �'"���!�������� ��������!����'� ���� ��� "����������� ���.�� ����������!������""���� ��������0�8 private void dtpSelectDate_ValueChanged(object sender, System.EventArgs e)

{

string msg = dtpSelectDate.Text;

msg += "\r\n Day: " + dtpSelectDate.Value.Day.ToString();;

msg += "\r\n Month: " + dtpSelectDate.Value.Month.ToString();;

msg += "\r\n Year: " + dtpSelectDate.Value.Year.ToString();;

msg += "\r\n Day of Week: " + dtpSelectDate.Value.DayOfWeek.ToString();;

MessageBox.Show(msg);

Page 218: 3Programmer's Heaven C# School Book

��������������� ���������

11B

}

� �����"��0���9'������.���������������"� ".�� ��"���"��9�� ��9����� ""���!���'��(� ��������0�8���� ���.��������8�����"!����'� ����.���������"�

5 �6��&������2 ����70���

�� "�'����'"�����.����.����"��������� ����""�����0�8�� ������..������� ������ ���"����A.� &���9����&���9&� ������ �9������������� � "��� �"�����0�8����'���"������!���!����!����"�������������"�����0�8��������""�"�����!������������'������� ����"� G������ �.���� � ��������.��� ����� � ".�.�.� ��'�� ��"�"0����"�����.��� "����..������� �

�$���� ���2 ����70�

������������ "�����0�8.��� ��"� ��� "�'� ���� �� �!���.� � �!�������� � �� ���!�������� "�'�&����A.� &���������@�8����!����'� ����� �����'�������� �.� !���"������

Page 219: 3Programmer's Heaven C# School Book

��������������� ���������

11=

�����.���� �.��.������ ���"�� ���.���� �����.��� ��!����68� ���"�!�����8�� ��� !���.� � �!��������'��"���"���� �J��!��!������ �!��� �����!

���������!��!����68��W�"��� ����� !���'����8�� ��� G"��G'���0����0��� ����.� !���"������

&���$��� ���!���.���� "!��� ����!���������"!����� �������������� ���� �����"��������*!��"��+��0��.� �"� ���"������,���������� @����� .��.�����)�.��� �'�����������.��!���������� �����'�"�� ���������)���� ������)����� �������� ������''�����������������"���A>������� ���0���� ��

�������"�����0�8�

B� ���� ���$���� ���2 ����70�

<���������.� !���"�����0�8�.��� ��"� ������� '�� �0���� �.���"����� ������������"����!����'� �0���� ��� ��� "���.��� �����.� !���"�����0�8� ".�� ���� ����!!���������"� ������0�8� private void btnOpenFile_Click(object sender, System.EventArgs e)

{

DialogResult res = openFileDialog.ShowDialog();

if(res == DialogResult.OK)

{

MessageBox.Show(openFileDialog.FileName, "File selected");

}

Page 220: 3Programmer's Heaven C# School Book

��������������� ���������

11?

}

����'�!���.��� ��"���"�����0�8� ������� �� ������'������*+�����"���������".��� ����"�����0�8� ���� � "����� ���������)����� �������� '������.��� ���'�������������"�����0�8�� ��� �8��� �'�����('������������.���"A>0���� ���������"�����0�8��!����� '�.�� ��"��� ����!������"!���� ��������0�8���� ����0�����"���8�����"9����'���!����'� �������

��������!�������0��������0�8�������&���$���.��.��������� ������.����.����!���������"!����

&�3��� ���2 ����70�

������!���"�����0�8���"������'����������������"��� ���� � " ����!���!�����0����"����� � �� ���!�������� "�'�&��������&�����������������������������.� !���"�����0�8����!����'� ���"�'�����'������!���"�����0�8� ������� � ".�� ���� ����!���!�����0����"� ��������0�8 private void btnSaveFile_Click(object sender, System.EventArgs e)

{

DialogResult res = saveFileDialog.ShowDialog();

if(res == DialogResult.OK)

{

MessageBox.Show(saveFileDialog.FileName, "File saved");

}

}

Page 221: 3Programmer's Heaven C# School Book

��������������� ���������

11�

�����"��0���'�����'���!����'� ������0�8�

�����������2 ����70���

���!� �"�����0�8���"������'�������������!� ����� ��:�������� ���"�����0�8� ,�����!����"��"9$���.�"� ",�$,�� �������� � �� ���!�������� "�'�&����&� ����������������"�����0�8���"������'��������������������:������������� ���"�����0�8� ,�����!���� ������ � �� ���!�������� "�'�&�������������������!� �"�����0�8���(��(�����

Page 222: 3Programmer's Heaven C# School Book

��������������� ���������

11�

% "��������"�����0�8���(��(�����

#����(�� �..������� '����.��� ��!���'������8�0�8��'��������� ����'�������'����������� �����!� �� "������!�����8�� �����8�0�8����!������(��(�����

���!�������� ��� �'�"�����0�8��A ��!� ������������ �����!� �� "������������������������� ����������������� ��� "���!���'�0���� ����

Page 223: 3Programmer's Heaven C# School Book

��������������� ���������

11/

private void btnChangeFont_Click(object sender, System.EventArgs e)

{

DialogResult res = fontDialog.ShowDialog();

if(res == DialogResult.OK)

{

txtComment.Font = fontDialog.Font;

txtComment.ForeColor = fontDialog.Color;

}

}

private void btnChangeColor_Click(object sender, System.EventArgs e)

{

DialogResult res = colorDialog.ShowDialog();

if(res == DialogResult.OK)

{

txtComment.ForeColor = colorDialog.Color;

}

}

������ ��!� �0���� ��� ��� "���.��� ����!� �"������ "�����!� �� "������!�����8��!�����8�0�8�����������"!� �� "������$�������'�������������'�����.��.�����!���!� �"�����0�8������9'�����!���0�"�!������ ������ �������0���� G��� ��� "���9���.������.��� ���������"�����0�8� "�����������!�����8�� �����8�0�8�����������"� ��

Page 224: 3Programmer's Heaven C# School Book

��������������� ���������

1;2

����2����-�������� ���-2��*���

������"����

��"��'�'������� ��'������..������� �� � ������'���"���0���������'����������0����(� �������������������!%�A�$��� "��"�!!��� ����.� � ��#����'�'���"��� �����"�������� �$���������� �..������� �&� ����'�'������� �0�������".����"���� "�8.�����������3��"�� ����'���������� ����"!�����'� �"����

������ ���-2��*���

,���!��"��G�..������� ��"��� ������'���"���0��������.����9�"�������'"����� �$��9"����������������.����"�"�������%�A�$�����.� � ��%�A�$���� �07������� ��"!����'��(��������'������ ������'���"���0������� ��������� ������'���"���0��������������F#J�����������".����"�������0����� ��0���%�A�$�����������8�������!��8�0��� "�!!���� ��%�A�$������ ���"�������� ��.��!�"��� ����""����������������� ���"���� ��"����������.� � �9�����"���� ����� �����"���0������� "��� � �������"'�������������F#J������� ������ ����� �����..������� ����� ����"������@������� '�� ��� ���� ��@��������������� ��'��������0��� "�8.� ���"���0���������9�����!��������..������� � ��J����� "���'���.����� �"����%�A�$���������.��0���0��� ��� �������0�!!���!.����� �"��������"�"������:����..������� ��������������� ��������"���0�������'�� �� ��"���� �J����� "��� "��� �������"�������!�������� ���������0��(� "���� ���� ���"���������"��� �!%�A�$��������"�"��� ����""���������������� "���������������������� ����� ���������!����� ���� ��� ���������"0� ���"����%�A�$�����.����"��� ����� ���� ��"���"���� ��"��������������

���"���� ������%���%�����������

Page 225: 3Programmer's Heaven C# School Book

��������������� ���������

1;

%�A�$������ ����"����%���%�����������% �������.���� ��.����!"��� ����"���������������������� ��� ��������.�������!"���� ���"������07�������"������07������������0��9������������ ��.� "�����"�!!��� ��� ���� ���������� .��!����.������ ��(��."���9� ���� ""������ ���"������������9� "������ ����"������"���������..���"�����������"���0����0����'�� ��"�"�������������"��� ��'��(���!!��� "������ 0�����.��!���� ���

2 ..��������$������.�-2��*���

@�!������ �� �����"������!��.���� �� �"��������..������� �� �%�A�$��9�����.���� ���� "���� "��"�!!��� ��..���� ����.� � ��������%���� ��������!��"������������ ��� �"� ������������� ���.���� ��� �����.��� ������� �����������������0�!!���!��0�������������� �!"��� ����"�����"���������0�� %������0�����"���� ��� "���� ��0����!����� ���'� "����� ������)�' )�.��� ��� ��������"����'� �������0������������ )�.��� ������� ��!���"�!�������0�������)������ )�.��� ����������� ��.0��'�� "�!!��� ���0��� �"��������� ���� � )�.��� ������ ���� ������������ �����..�����.���������!���"������� �%�A�$������� ��� ���"���0��.���!������������� ����"�!!��� �"���0������.����"�����.����"�����*��"�����+�.����C�"!�������.���������"���0�������,�����!�����!��.����"�"���.������C�"� "�.����C�"����!��������F#�����"���0���������� ����!������������'���G�J�G� "����� ��� �"� ���������������J����� � ���.�������������9A����������.����"�������*"�����+�.����C�"!�����A������@������,�����!������.����"�"����� ��������'������ �� ��������..������� ��� �A#6�..����""���0����������� ����!������������'���GA���0G� "��������� ��� �"� ��������������A���0 ���.����� !���9����� ��A���0�������� ������F#�������A�����"���0��P�� ����"���0��.���!�������� ������.����"��.����C�".��!���� ��9��'����� ��� �����.���

Page 226: 3Programmer's Heaven C# School Book

��������������� ���������

1;1

�J��� ����� 9A���0�� ����� )�.��� ���� ����� �����"���0��������J������ "9A���0����� " )�.��� ��F#J������J�����%"�.���9A���0����%"�.��� %��������� ��������"���0������9!�������������"� "!���

���"�������J�����)��"��9A���0����)��"�� %������������""���!������"���0��� ��� ����""��� ��J����������9A���0��������� )�.��� ��.���������������".����"����

-���3 �6�.���� ��&G��K��� ���

����'�.��� ��0���!�����'�!!���0����F#J������

&G��&(�(�8�&���������

���J�������"������������� ����� �!������ �����"!���� �������"���0����0��� SELECT * from emp

�����������!���"�!�����������"!��������0�� ���"G��.G SELECT empno, ename from emp

��������!���"��. �� "� ����!�����������"!��������0�� ���"G��.G SELECT * from emp where empno < 100

�����������������"!��������0�� ���"G��.G'�������������!���!���"��. ������� 22 SELECT * from article, author where article.authorId = author.authorId

�����������������"!��������0�� ���"G�������G� "G������G��������������������!���!���"�������"

&G���*&(98�&���������

���J�������"��� ���������"� ���"���0����0��� INSERT INTO emp(empno, ename) values(101, 'John Guttag')

� ���������"� �������.��0��� "������. �!���"�� 2 � "��� ���!���"��G-�� 3�����G

&G��B"2-8(�&���������

���J�������"����"�!��8��� ������"� �"���0����0���

Page 227: 3Programmer's Heaven C# School Book

��������������� ���������

1;;

UPDATE emp SET ename = 'Eric Gamma' WHERE empno = 101

�."�����������"'�����. �!���"� 2 0����� ���� ���!���"��G6���3����G

&G��2(�(8(�&���������

���J�������"��"������8��� ������"*+!����"���0����0��� DELETE FROM emp WHERE empno = 101

"�������������"'�����. �!���"� 2 !��������.��0��

"��.�� ����������������������:��6 � �-2��*���

6 ���������'� "� ���"����� O#��G���������� �.���������$�''�'���0���"� �..������� ��"��� �������'����� "���������(���.��!����"�� �%�A�$��� ��'��������,��F#������ ",�%���"���0��������.��!������"���������(��F#���������"0�����.��0�0������!����������'���0��� �,��F#�����'�� "�����.� ��$���..������� �&���F#�����9'�'���0��� �����!������������������J����� � ���.����%������"��"��� ��������A���0"���0���&��%���'�'���0��� �����!�����������������A���0 ���.���� � !���9������ ���� �"�!!��� �� �����'��..������!��"�����.��� "� ���'�������������� �'���0�"�!!��� �� 0���������'���������������.���!�������� �!�������'��� ������ ���(�� // For SQL server

SqlDataAdapter dataAdapter = new

SqlDataAdapter(commandString, conn);

// For Access

OleDbDataAdapter dataAdapter = new

OleDbDataAdapter(commandString, conn);

&������8��.����"�9'�'���0��� ��"���0�� ���"G��������������� G����"���0��'����������0�� ���"G%������G����!���"�!�����0��G%������G����&���"$��� ��.� �����.��� ����" *�������>��+� ����� ���� �J���"� ��!���!�������������� ���� � ���������!������������.�� ���� � ��.��������� ����!������������(�G,���������"� ��

-���G��G��������G�

Page 228: 3Programmer's Heaven C# School Book

��������������� ���������

1;B

�������" *&����� >��+� ����� < �J���"� �����!�������� � � ����� $���!�� �� ����������"���A!��0���� � ���� ���"�������������'�.�0����" ���G��������������� G"���0������� ��� ���0�� ���"G%�����G'������!����'� �!���"� &���"$��� ��.� �����.��� �������" *�������>��+� ����� ���� �J���"� �����!��������� ��� ���� � $����!���������

-����� ���2������ ���-2��*���

���������� �%�A�$��� ��������!����'� ���.�

�� ��!� � ������ ����� ��� �!�����"���0������� �� ��!� � ������ ����� *�J��� ����� ��A���0�� ����� +�����"���0���� ������ ����� ��� � �� ��!� � ��������� "*�J������ "��A���0����� "+������� "��� ������� ��� ���J���� �� ��!� � ����"����"�.���*�J�����%"�.�����A���0����%"�.���+�� ��������� "��� �� "���

�� ����� �07��� �� ������ �� �'��������07��� �� �!�������� "��6#6��9!���� ����"������07���'������������!���J��������������"����"�.��� �� )��"� ���������"!������������0��� ���"������� ��������)�'� "��������� �07��� �� �!�������� "�<��%�69�$�6)����6#6�69��� �."��� ����"���������������"����"�.��� �� %���.�� ������������ ��� ���"����������"���0��

�� ��'����"��� ����� �� �..������� ������0����F#������� "%���"���0��'� ��"��� ���"����!����'� � ���.���� ����..������� � using System.Data;

using System.Data.OleDb; // for Access database

using System.Data.SqlClient; // for SQL Server

#��G �'"��������!����0�����.� "���"�����

2�. � ���� �������� ����� ���

����� ����� ��� �"�!� �'����"���0��������������� �9'���������"�9������� ���� ".�'��"� "�.��� �������"���0�� ����

Page 229: 3Programmer's Heaven C# School Book

��������������� ���������

1;=

&���F#������9'�����'����� ���!����'� ��� ����� ��� �� // for Sql Server

string connectionString = "server=P-III; database=programmersheaven;uid=sa; pwd=;";

&����!���'�����"�!� �"���� �� �� ����!��������9'������E���� �������$�8�'�"�!� �"��� ����!���"���0��9����"*��"+� ".�'��"*.'"+��� �����F#�����"�� G������.�'��"!����������%"�� �������*�+���9�������!���0�� (� ����� ����� ��� ��*:��( �'��������"� ������ "��������0�".�������E ����9�������0�� (.�'��"� ���������������0������� ��'��(+ &��%���9'�����'����� ���!����'� ��� ����� ��� �� // for MS Access

string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source = c:\\programmersheaven.mdb";

&���'�����"�!� �"���.����"���!�������"���0������ '�����"�!� �"���"��������'���������""���!���������"���0��� ����������� �"��������������� �������������� ��������������� ����������������������������� ��������� �����������

������������������������������ �

2�. � ����������� ��

%�� ����� �"�!� �"�� ������ ����� ��� ������07������"0����"����"�.������� ������ ""��� ���!������"���0��&���F#����������� ����� �������"��(����� // for Sql Server

SqlConnection conn = new SqlConnection(connectionString);

% "!��%�������� ����� �������"��(����� // for MS Access

OleDbConnection conn = new OleDbConnection(connectionString);

����'�����.��"����� ����� ��� �������� ��������!����� ����� �07����

2�. � ���� ������������������� ���

�������� "�� ��� ���J������0�.��"�����"���0�������'������ ������� "��� ����'������������� "�07���*�J������ "��A���0����� "+������ ������ ��������� "��� �'�������"� ����..������� �� string commandString = "SELECT " +

Page 230: 3Programmer's Heaven C# School Book

��������������� ���������

1;?

"artId, title, topic, " +

"article.authorId as authorId, " +

"name, lines, dateOfPublishing " +

"FROM " +

"article, author " +

"WHERE " +

"author.authorId = article.authorId";

����'�����.��"�J��������������������������� �'������������G ����A!����������������.���J����9����� string commandString = "SELECT * from article";

2�. � ���� ��2����-�$����

$�8�'� ��"��"�!� ����"����"�.���*�J�����%"�.�����A���0����%"�.���+����"����"�.���������������� "*J����+� "�� ����� 9� "�� ������� ��������"���0��'�� �(�"9!��������������!���J����� "������� ��������"������ &���F#������9���"����"�.����������"��(����� // for Sql Server

SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, conn);

% "!��%���9���"����"�.����������"��(����� // for MS Access

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, conn);

����'�����������"� �'� �� ���!���"����"�.���� "�..���"���������� "��� �� "�� ����� �07���� ����� ������������

����� ������. �� ���� ��2���&���

&� ����'� ��"��������� � �� ���!�����������%'��� ��� �"�������9���������������D�!!�� ��� ��� ���!"���������������07����������"��.��� DataSet ds = new DataSet();

$�''� ��"��!�������������'������������!���J�������'��������"���%"�.����07���!�����.��.��� "������&���*+�����"����������.'�������"����"�.����� ��������.������"���0��� "!��������������!���J�����

Page 231: 3Programmer's Heaven C# School Book

��������������� ���������

1;�

dataAdapter.Fill(ds, "prog");

����'����������"���&���*+�����"�!"���%"�.����07�����������..���"�����"�������!���� "��� ����!�����0��*������0��+� '�������������!J�����!����"� �������'� ��"���� ���� "!����"���!������"���0���$�'���������!���J���������"� ���"������07���� ���.�����0��9'������ � �� ���!���������0������� ������!��� ���������0��0��� ����� "�8��.��.�����!���"������07���G��0����������� � DataTable dataTable = ds.Tables["prog"];

���� "�8��'�������"��(���� ����!�����0��� ���"������ "����� ��������.� "� �������0���07����$�''��� �������0��)�'� "����� ��������� ���������"���� �����0���

-�2�������� ��-$$� ��� ��

#��G �'�������"��� ������ �..������� !������� �"����&����������'� "�'!����..������� � "��(������������' ���!����'� � �.����

�����������$���.��.�����!�����8�0�8�*!�����.��0�����+��8�%��������9�8�%�����������9�8�%��������.��9�8�%������"9�8�%�����$���9�8�$��A!#� �� "�8�����A!��0���� ��%��'����������)��"A ��.��.�����!��������8�0�8��������"� G�'� ������������ �������8����� ����!���0����

Page 232: 3Programmer's Heaven C# School Book

��������������� ���������

1;�

*!�����.��0�����+���0� #��"��0��90� $�8�� "0� ��������� �������'�����"��0��"���$�8�� "�������0���� *0����� ������6 �0��".��.������!���+� ���������"�!� �"����������0��� ���&������� public class ADOForm : System.Windows.Forms.Form

{

DataTable dataTable;

int currRec=0;

int totalRec=0;

���"�����0���07���'���0���"����!��� �������0������� �"��������!���J������������)��� "�����)��� ����������0�������"��(��.����(�!�������� ������"� "����� ��0���!�����"� �����0���

�� ����������

&�����#��"��0��0���� 9'�����'����� ���!����'� ���� ��� "��� private void btnLoadTable_Click(object sender, System.EventArgs e)

{

// for Sql Server

string connectionString = "server=P-III; database=programmersheaven;uid=sa; pwd=;";

// for MS Access

/*string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" +

"data source = c:\\programmersheaven.mdb";*/

// for Sql Server

SqlConnection conn = new SqlConnection(connectionString);

// for MS Access

//OleDbConnection conn = new OleDbConnection(connectionString);

string commandString = "SELECT " +

"artId, title, topic, " +

"article.authorId as authorId, " +

"name, lines, dateOfPublishing " +

"FROM " +

"article, author " +

"WHERE " +

"author.authorId = article.authorId";

Page 233: 3Programmer's Heaven C# School Book

��������������� ���������

1;/

// for Sql Server

SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, conn);

// for MS Access

//OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, conn);

DataSet ds = new DataSet();

dataAdapter.Fill(ds, "prog");

dataTable = ds.Tables["prog"];

currRec = 0;

totalRec = dataTable.Rows.Count;

FillControls();

btnNext.Enabled = true;

btnPrevious.Enabled = true;

}

&���'�������"����� ����� 9"����"�.���� "!����"���"������07���9����!'����'�����"����"��������������"0� ���"����'���������� ��"��������"�!�����A���0.����"��*,�E%���+� "����� ��F#������.���!����"���!���'���"��(������ %���"���0��9����� ��.������� �����F#�������"����� ""�E����� ����%�����"�� $�8�9'�������� �"���"�����0�������� �!������J���������"�����0���07���'����'�"������"�������������9��� �"C���������)�������0��� "��� �"��� ��0���!��'� ���"�����0���� the totalRec variable:

dataTable = ds.Tables["prog"];

currRec = 0;

totalRec = dataTable.Rows.Count;

��� '������"���&����� ����*+�����"9'����!�������� ����*��8�0�8�+� ���!���'����������� ������"�!�����0��H.���H�&� ����'�� �0��"���$�8�� "�������@���� �

� �� ���� ������������ ������

���&����� ����*+�����"� ���.������!�������� ����� ���!���'����������� ������"�!���"�����0�����������"�"�!� �"�!����'� private void FillControls()

{

Page 234: 3Programmer's Heaven C# School Book

��������������� ���������

1B2

txtArticleId.Text = dataTable.Rows[currRec]["artId"].ToString();

txtArticleTitle.Text = dataTable.Rows[currRec]["title"].ToString();

txtArticleTopic.Text = dataTable.Rows[currRec]["topic"].ToString();

txtAuthorId.Text = dataTable.Rows[currRec]["authorId"].ToString();

txtAuthorName.Text = dataTable.Rows[currRec]["name"].ToString();

txtNumOfLines.Text = dataTable.Rows[currRec]["lines"].ToString();

txtDateOfPublishing.Text = dataTable.Rows[currRec]["dateOfPublishing"].ToString();

}

����'������������8�.��.�����!�����8�0�8��������� �������!��������.� "� �!���"�!�������� ������"���������"���)�'��������� �!���"�����0��� "�� ���� "�8��'���������������)�'��.��� �� ��������� ������"���������� �����"���� "�8��.��.�����!�������)�'�� ��������� �����������"���� �����.������!���"��!����8.�� ���� ���('���"�����9����� ��.��!�����0��������� ����E DataRow row = dataTable.Rows[currRec]; // getting current row

object data = row["artId"]; // getting data in the artId field

string strData = data.ToString(); // converting to string

txtArticleId.Text = strData; // display in the text box

'������J������ ��� txtArticleId.Text = dataTable.Rows[currRec]["artId"].ToString();

�� ��'�� �����������..������� � ".�����#��"��0��0���� 9���'��������!����'� ����.���

Page 235: 3Programmer's Heaven C# School Book

��������������� ���������

1B

*�3 ��� ���� ��� �� ��������

$������� ����������������"����� ��������&�����$�8�0���� 9'�����'����� ���!����'� ���.����� ��� "��� private void btnNext_Click(object sender, System.EventArgs e)

{

currRec++;

if(currRec>=totalRec)

currRec=0;

FillControls();

}

����'�!���� ����� ����� ����������0������)��� "����(�!��������"�����������"*�� ���������)�������0��+� �����0����!����9��� '������������� ������"�����!��������"������ �������&����� ����*+�����"��"�.����������� ������"� ���!���� ��������������� ��� "���!������������0���� ���(��(����� private void btnPrevious_Click(object sender, System.EventArgs e)

{

currRec--;

if(currRec<0)

currRec=totalRec-1;

FillControls();

}

����'�"������ ��������)�������0��� "����(�!������"���!��������"� "�!������� '��������������������"�A ������ 9'��������&����� ����*+�����"��"�.����������� ������"� $�'����� ����������������������"�� ����$�8�� "�������0���� �

Page 236: 3Programmer's Heaven C# School Book

��������������� ���������

1B1

B$�� ���� ��������

��!��'������ ��������"���"���!������"���0��� "���� G���� ��"� ���'9� ����" �'��'��"�����"�8��� ���'�#��G���� ��'��.��!���������(� �0�� �� ����� �������!���������� 9'�'���� �������H%������H��0��� "'��� ��0��� ����H%�����H��0��9!������(��!��.������� <."��� ����0��� %�A�$�������� ������ �� "����:�� ��"��!����'������.���."���9� ���� ""����������"� �������%"�.������*�J�����%"�.���+��.��.�����!�������!���� ���9�."���� ""���������� "�&����!���'� ��"��.��.����������� "*�J������ "+� "�""�������"����"�.����07������������ "�����.���F#����� "'���.�������������.���� ������.9'�'���� ���"���������"� ������C������ "*+� ���!����'� ��8��.��� ���� "��'� ��"���"".������������������� "����.�������������.����� ����!���"�����0��!���"� �����"� ���.�������������� "�����.���� ������.9'�'���� ���"���������" ���"%""�����*+� ���!����'� ��8��.��� ����'���."����0�"�0������"� �� ��� ��� ����..������� �&������� ���9�."���� ""�����P'�� ���9�."���� ""�������������.� "� �"�����'*����)�'+�!���"�����0��*������0��+�07����

Page 237: 3Programmer's Heaven C# School Book

��������������� ���������

1B;

%!���� ��."���'��������<."���*+�����"�!���"����"�.������0��..��� �����9���"������ "��0�� ����.�������������."�����������"������ &� ����'��������%���.���� ��*+�����"�!���"������07��������������� ��� ���"����������.������"���0��� %��� !����.������'����� G�� ���"�"�����.!��"�������"���� 9'������(��.����!�"����."���� ������..������� �

7� � ���� ��-$$� ��� ��

����..������� '���!� �������(��(�����

� ����..������� 9'�����"�!� �"������"��������07���*��(��J�����%"�.���9�������+�.������������0�������'��� ��������� "�!!��� ������"� public class ADOForm : System.Windows.Forms.Form

{

// Private global members to be used in various methods

private SqlConnection conn;

private SqlDataAdapter dataAdapter;

Page 238: 3Programmer's Heaven C# School Book

��������������� ���������

1BB

private DataTable dataTable;

private DataSet ds;

private int currRec=0;

private int totalRec=0;

private bool insertSelected;

...

�� ���� ����������� �$��# ������� ��� ��.��H���������

������ ��� "���!�����G#��"��0��G0���� ����� ��"�0��� " �'���(��(����� private void btnLoadTable_Click(object sender, System.EventArgs e)

{

this.Cursor = Cursors.WaitCursor;

string connectionString ="server=P-III; database=programmersheaven;uid=sa; pwd=;";

conn = new SqlConnection(connectionString);

string commandString = "SELECT * from article";

dataAdapter = new SqlDataAdapter(commandString, conn);

ds = new DataSet();

dataAdapter.Fill(ds, "article");

dataTable = ds.Tables["article"];

currRec = 0;

totalRec = dataTable.Rows.Count;

FillControls(); // show current record on the form

InitializeCommands(); // prepare commands

ToggleControls(true); // enable corresponding controls

this.Cursor = Cursors.Default;

}

��������� ��"�����������������������������!��������"9� "��� ��"������!���������� "�!��������"�#����'����������"���� ������C������ "*+�����"�!���!���� ������ ����'������!��������"�

�� � �� ����������

���� ������C������ "*+����(�������"��� "���� "� ����..������� ����"�!� �"� ���.�������� private void InitializeCommands()

{

Page 239: 3Programmer's Heaven C# School Book

��������������� ���������

1B=

// Preparing Insert SQL Command

dataAdapter.InsertCommand = conn.CreateCommand();

dataAdapter.InsertCommand.CommandText =

"INSERT INTO article " +

"(artId, title, topic, authorId, lines, dateOfPublishing) " +

"VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)";

AddParams(dataAdapter.InsertCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

// Preparing Update SQL Command

dataAdapter.UpdateCommand = conn.CreateCommand();

dataAdapter.UpdateCommand.CommandText =

"UPDATE article SET " +

"title = @title, topic = @topic, authorId = @authorId, " +

"lines = @lines, dateOfPublishing = @dateOfPublishing " +

"WHERE artId = @artId";

AddParams(dataAdapter.UpdateCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

// Preparing Delete SQL Command

dataAdapter.DeleteCommand = conn.CreateCommand();

dataAdapter.DeleteCommand.CommandText = "DELETE FROM article WHERE artId = @artId";

AddParams(dataAdapter.DeleteCommand, "artId");

}

����J�����%"�.���*� "A���0����%"�.���+�����.��.�����!�������!���� ���9<."���� "����������� "������.��!����.��.�������J������ "*� "A���0����� "��.��������+�������������"�������� "�� ������ ����� *�J��� ����� +�07���G����������� "*+�����"� ����� ���������� "��8�.��.�����!��������� "�������.�������F#J������ ��� �!������������ ��� ����������������0�������� "��������� ����� "'�������"��� ����!���!���"'���� GfG�� '����������.���!��!���"��������J����"�&���8��.��9'�����'����� �������������� "G����� "��8��� "DELETE FROM article WHERE artId = @artId";

����'�������"f����"� ���"�!� �.������������� !���9��������'���"0���.����"0����.���!�������'�� '�"������.��������������"�

Page 240: 3Programmer's Heaven C# School Book

��������������� ���������

1B?

- ���"������������� ���������

%!���"�!� � ��������� "��8�!����������� "9'��������%""�����*+�����"0�.�� ������������� "����!� "��� ����!� �!���"��"� ��������.� "� �J��������%""�����*+�����"�������.��� "�""���!���"'���� GfG��0������������������������� �!�������� "����%""�����*+�����"�"�!� �"� private void AddParams(SqlCommand cmd, params string[] cols)

{

// Adding Hectice parameters in SQL Commands

foreach(string col in cols)

{

cmd.Parameters.Add("@" + col, SqlDbType.Char, 0, col);

}

}

�������!������ ��� ���� ���%""����������"����������.��!������ ".���������G.������� �MNG private void AddParams(SqlCommand cmd, params string[] cols)

���.����(��'��"���"������������.����������������"'���"��(�������0�� ��0���!��� ������ ���0�����"� ������ ������ ���"G���G��!��������"������"��(����� AddParams(someCommand, "one");

����C��!�����������'���"0�� �9� "�!��������"������"��(����� AddParams(someCommand, "one", "two", "numbers");

����C��!�����������'���"0�������� G�����!��� "�8���������.��������������Q������O �����G���0��(����������"�������"�����.���""����..���"����� ���.��!�8�"'���GfG� ��������������������� �!����J������ "������������.���������!���%""*+�����"��������.��!���.��������9�C��!���.��������� "��������.� "� ������ ���� %!���!����'� �����0�����.9'�����"�!� �""�!!��� ������ "!����������"�."����$�''��� �."��������"�!�����0���

8 ��8����������;<���� �.�����$$� ��� ��

� ���#��"��0��0���� ��� ��� "���9'������"����������� ����*+�����"�!���� ������C� ��������� " private void btnLoadTable_Click(object sender, System.EventArgs e)

Page 241: 3Programmer's Heaven C# School Book

��������������� ���������

1B�

{

this.Cursor = Cursors.WaitCursor;

...

FillControls(); // show current record on the form

InitializeCommands(); // prepare commands

ToggleControls(true); // enable corresponding controls

this.Cursor = Cursors.Default;

}

������"�!� �"����������� ����*+�����"� ����..������� ����� �����6 �0��"� ")��"A ��.��.������!0���� � "��8�0�8�� ���!����������.������������!����������� ����*+�����"������"'����!���0����� �������'�����(����!���������"����"�� "�!�����"'���������������'�����(����!���0��(�� �������"����������"�"�!� �"�� private void ToggleControls(bool val)

{

txtArticleTitle.ReadOnly = val;

txtArticleTopic.ReadOnly = val;

txtAuthorId.ReadOnly = val;

txtNumOfLines.ReadOnly = val;

txtDateOfPublishing.ReadOnly = val;

btnLoadTable.Enabled = val;

btnNext.Enabled = val;

btnPrevious.Enabled = val;

btnEditRecord.Enabled = val;

btnInsertRecord.Enabled = val;

btnDeleteRecord.Enabled = val;

btnSave.Enabled = !val;

btnCancel.Enabled = !val;

}

( � ���;��B$�� ��<�9�����

&���"��� ��������� ������"9'�����.����"�"� 6"��)����"0���� � ���!���������� ��� "���!�����0���� ���.��� ���������.��� "�� private void btnEditRecord_Click(object sender, System.EventArgs e)

Page 242: 3Programmer's Heaven C# School Book

��������������� ���������

1B�

{

ToggleControls(false);

}

������ ��� "�����.����(����!���� "����� �������"����"�0�.�� ��!��������������������� ����*+�����".��� ��"�0������� ����.������6"��)����"0���� 9���!������� ��"9������(��(��

%����� �� �'9�����8�0�8�����"���0��� "�������� "�� ���0���� ���� �0��"��!������'��� ������������� ��9������ ���������� ���0���� 9� "�!����'�������������� ��9������ ������������0���� �!�����(� ������� ���

(3����1������.��� ��&�3��7�����

������ ��� "���!���������0���� ���"��������!��������8�0�8�� "��������� �������� ������"� ���"�����0��������� ��� "���!���������@���� �� private void btnSave_Click(object sender, System.EventArgs e)

{

lblLabel.Text = "Saving Changes...";

this.Cursor = Cursors.WaitCursor;

DataRow row = dataTable.Rows[currRec];

row.BeginEdit();

Page 243: 3Programmer's Heaven C# School Book

��������������� ���������

1B/

row["title"] = txtArticleTitle.Text;

row["topic"] = txtArticleTopic.Text;

row["authorId"] = txtAuthorId.Text;

row["lines"] = txtNumOfLines.Text;

row["dateOfPublishing"] = txtDateOfPublishing.Text;

row.EndEdit();

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

ToggleControls(true);

insertSelected = false;

this.Cursor = Cursors.Default;

lblLabel.Text = "Changes Saved";

}

����'�!������ �����.�������0��*�0�#�0��+��8�����'�������� �.������ "��� ��� ������������'��������� lblLabel.Text = "Saving Changes...";

this.Cursor = Cursors.WaitCursor;

��� '���(����!��� ������������ ������"G��'� "�����"��@��� 6"��*+�����"�����."���� ���'�������0�� "0������)�'G@��� 6"��*+� "6 "6"��*+�����"����@��� 6"��*+�����"���.��������.� "������ �!���������"���� �!��'G"��������� ���@��� 6"��*+� "6 "6"��*+0�� "���9'�����"������ ��"������ �����8�0�8��������'� DataRow row = dataTable.Rows[currRec];

row.BeginEdit();

row["title"] = txtArticleTitle.Text;

row["topic"] = txtArticleTopic.Text;

row["authorId"] = txtAuthorId.Text;

row["lines"] = txtNumOfLines.Text;

row["dateOfPublishing"] = txtDateOfPublishing.Text;

row.EndEdit();

%!������ ������� ��� �����'9'��."������"������ "��0��0������ ����<."��������"�!"����"�.���������������� ��� ����������.�������!"����"����������������� ��"��'� "��0�������.������"���0��9'������"���%���.���� ��*+�����"�!��������������

Page 244: 3Programmer's Heaven C# School Book

��������������� ���������

1=2

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

&� ����'�0����������� ������ �������"�0������ ������������ ����*+�����"� ".�� ������������������������ ����������"�����0����!���9��� ��"��������0��(�� ������ "�."���"���.�������0���*������!���� ����������"�����0���"����"������ ����� ���0���� G��� ��� "���+ ToggleControls(true);

insertSelected = false;

this.Cursor = Cursors.Default;

lblLabel.Text = "Changes Saved";

�����.���� ��� ������������������0���� ���"����������� ��� �������� ������"������0�������"�!������������6"��)����"��� ���)����"0���� �� ���6"��)����"���9���.�� ���������"�� ����� ������"9'����� �������!���� ���)����"0���� 9�'�'������������ ���� ���� �)����"����� 9���.������� ���� �'��.����'9������������ ������".�� �������� ".��� �������������� �������������� ��9���7�0�!�������0���� � 0������������������ ��� �������� ������"!��������8�0�8������"�����0��9�."��� ����"������ "!� �����."��� ����"���0���

(3����1������.��� ���������7�����

����� ���0���� G��� ��� "����� private void btnCancel_Click(object sender, System.EventArgs e)

{

if(insertSelected)

{

btnDeleteRecord_Click(null, null);

insertSelected = false;

}

FillControls();

ToggleControls(true);

}

���!���� "�� ������ 0�0������� ���"����"�0�.��� ����������6"��)����"��� ���)����"0���� ���� ���� ���)����"0���� �������"9���.������� ���� ��.�������"*��'+�������0��*���"������!'����'�'�����������+� "0�� ����!���� "�� �������"����"�0��� ������������ ����*!���+������ ������� ���0���� ���"���� ���0����"��� ��!�������� ������"� "��� �'��� ����"�����"���� �� ���� ����� ����� �!� �'�����"9���.������ ��"��"������������� �* �'��� ����"+��'!��������0�����������"9� ����..������� 9�@����� �����0��G� ����������"G'�������������'�� ������

Page 245: 3Programmer's Heaven C# School Book

��������������� ���������

1=

��������� ���)����"0���� ����@����� ������ !�������� ���0���� '����������"����"�'���0����6"��)����"0���� ��0����� ���)����"0���� ��� ������� ���0���� ��� ��� "���!�������('������� ����������"������ "�!���9���������������0���� G��� ��� "�����"������������� ������"� "��� ����������"0��(��!���� if(insertSelected)

{

btnDeleteRecord_Click(null, null);

insertSelected = false;

}

��� '�!�������� ����*��8�0�8�+!������"���� �������� ������"� "0�� ������ ����0��(����� �������"�� FillControls();

ToggleControls(true);

������ ���9�����

��� ���������"� �������0��9�������� ��������� ���)����"0���� �%�����"�� ����"� �������0��0��""� �� �'��'�����������0��G)�'��������� ������������ ��� "���!������ ���)����"0���� � private void btnInsertRecord_Click(object sender, System.EventArgs e)

{

insertSelected = true;

DataRow row = dataTable.NewRow();

dataTable.Rows.Add(row);

totalRec = dataTable.Rows.Count;

currRec = totalRec-1;

row["artId"] = totalRec;

txtArticleId.Text = totalRec.ToString();

txtArticleTitle.Text = "";

txtArticleTopic.Text = "";

txtAuthorId.Text = "";

txtNumOfLines.Text = "";

txtDateOfPublishing.Text = DateTime.Now.Date.ToString();

ToggleControls(false);

}

Page 246: 3Programmer's Heaven C# School Book

��������������� ���������

1=1

&����!���'������� ����������"�����0��������9��������������� ���0���� ������� !����"��������"����"�'���0����� ���)����"0���� ������ ������"� �'����)�'�� ����������0��G$�')�'*+�����"9�""�"�������)�'��������� �!���"�����0��� "�."���"�������)��� "�����)�������0��� DataRow row = dataTable.NewRow();

dataTable.Rows.Add(row);

totalRec = dataTable.Rows.Count;

currRec = totalRec-1;

$�'��� �'��'����"���������� �'������ ����"� ���������'��������������"!���"���������� ��0���!�����"�'�"� G�'� �������'�������������.������(��!���"�!�����0���A!���������7���"��� ����:�����'� �������'���������� ������.������(��!���"��������� row["artId"] = totalRec;

txtArticleId.Text = totalRec.ToString();

����� ������"��������8�0�8�90��!����"��������!��0���� ���8�0�8'����������� �"���� ��"�������.������9� "!� ����������"����"�0������ ������������ ����*+�����"� txtArticleTitle.Text = "";

txtArticleTopic.Text = "";

txtAuthorId.Text = "";

txtNumOfLines.Text = "";

txtDateOfPublishing.Text = DateTime.Now.Date.ToString();

ToggleControls(false);

2���� �����9����

������ �������"����� ������.���%����� ��"��"��������!��� ���������������'� "������������*+�����"���� ��� ��"���������<."���*+�����"�!���"����"�.���� "���%���.���� ��*+�����"�!������������.���� � ������������� ��� ���"�����0�������.������"���0��������� ��� "���!�����������)����"0���� �� private void btnDeleteRecord_Click(object sender, System.EventArgs e)

{

DialogResult res = MessageBox.Show(

"Are you sure you want to delete the current record?",

"Confirm Record Deletion", MessageBoxButtons.YesNo);

if(res == DialogResult.Yes)

{

Page 247: 3Programmer's Heaven C# School Book

��������������� ���������

1=;

DataRow row = dataTable.Rows[currRec];

row.Delete();

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

lblLabel.Text = "Record Deleted";

totalRec--;

currRec = totalRec-1;

FillControls();

}

}

�� �������� ����"����������"0���� '���.���� � ���"������������� ������"9'� ��"����(�� !������� !���������������(��������������'����������"'������"������ �&�����.��.��'�.��� �������'���������0�8'���:�� "$�0���� � DialogResult res = MessageBox.Show(

"Are you sure you want to delete the current record?",

"Confirm Record Deletion", MessageBoxButtons.YesNo

);

�!��������������:�0���� � ��������0�89�����"���"������������� ������"��8�����"� if(res == DialogResult.Yes)

{

DataRow row = dataTable.Rows[currRec];

row.Delete();

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

lblLabel.Text = "Record Deleted";

totalRec--;

currRec = totalRec-1;

FillControls();

}

&���'�������!��� ���������'��.��� �� ��������� ������"� ���"�����0��� "�����"��������*+�����"������ ���"������ �������"���0��� "�."���"��������)��� "����)�������0���&� ����9'�!����"����� ����*��8�0�8�+'��������������"� �����0��� ����� ���"����"��� ������ �..������� ��.��!�������� "���������(�� �%�A�$���

Page 248: 3Programmer's Heaven C# School Book

��������������� ���������

1=B

B� ���&����"��������

��!��'�����'����� ����F#������ �� �"������"�9'����������� ������"�"���� ���'���������8.�� �����"���0�������*"��� +� �����"�'�������0���� ��"��� ������!�������'�������".����"���� ���"�!.��� �F#������ ��%����".����"�����.�����.���"�8�����0���07��������� ��� � ��������F#������ ���� ������� ��.�����������.��8�F#������ �'����� �������".����"����%����".����"������0�'����� ������.�� .��� "����� ���.���

&��$���&����"��������

�� �'.��� ������.������".����"���'�������"����.��� ������8���������������!�������'�������".����"���� "�����0���"���0���� ��.�������"��"���0����������� �� ��� ����� ����"���"���0��0��������!"���0��� ���%�A�$��� ���� �� ��@������'�.��� �����������'�!!���0�����.��!����".����"���!���6#6��9�$�6)�9<��%�6� "�6#6�6�.������ �� �F#������9����� ������� "�""����".����"���������"���0���� �����F#������6 ���.���,� �����

B"2-8(�&����"�������

%��.������".����"������."���������"� CREATE PROCEDURE UpdateProc (

@artId as int,

@title as varchar(100),

@topic as varchar(100),

@authorId as int,

@lines as int,

@dateOfPublishing as datetime)

AS

UPDATE Article SET

title = @title, topic = @topic, authorId = @authorId,

lines = @lines, dateOfPublishing = @dateOfPublishing

WHERE artId = @artId

GO

��� ����!�������".����"����<."�������� "����� .��.��������!�������!���!���"�!���%��������0������J������0��8�����"'�� �������".����"������ �."�����������"'�������..���".������(��*f����"+�� �����..���".�����������������������������"�'�����'����� ��� ������C������ "� ���.�������8��.��� "'��..�����"� G������ �.��0���� � "���� "� ������� �!������ ��!�������'�������".����"����

Page 249: 3Programmer's Heaven C# School Book

��������������� ���������

1==

�*&(98�&����"�������

%��.������".����"������ ���������"� CREATE PROCEDURE InsertProc (

@artId as int,

@title as varchar(100),

@topic as varchar(100),

@authorId as int,

@lines as int,

@dateOfPublishing as datetime)

AS

INSERT INTO article (artId, title, topic, authorId, lines, dateOfPublishing)

VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)

GO

�������".����"����0���� ���"� �������� "����������������<."��������8��.���������'������ �����$�6)��F#������ �� ���"�!���<��%�6����� "�

2(�(8(�&����"�������

%��.������".����"�����"�����������"� CREATE PROCEDURE DeleteProc (@artId as int)

AS

DELETE FROM article WHERE artId = @artId

GO

����'�������"� ��� �.��������9���"�����������"���� �� ��"��.������(��������

&(�(�8�&����"�������

%��.������".����"���������������"!������0��� CREATE PROCEDURE SelectProc

AS

SELECT * FROM Article

GO

���.��0�0��������.����!������"�� ����(�� �.��������� "� ������������������"!������%��������0���

Page 250: 3Programmer's Heaven C# School Book

��������������� ���������

1=?

%�����!�������".����"���.��� ��"�0������(�.��8���������.�������������"��"�� ��!� "� �"�!!������� � "���� "� �������!����".����"���� ������"��)���'���"����".����"�����������������.��8� "9�!�����9������!����� ����O

B� ���&����"��������6 � �-2��*��� �����

<� �����".����"���'���%�A�$��� ����8���������.��9�.�������'�� '�����"�����.�"����..������� '����F#����� "�%��'� ��" �'�����.��������F#����� "� ���.������'�������������".����"����&���8��.��� ���� �����C������ "*+�����"�!���.�������8��.��'�������"���� �������� "� private void InitializeCommands()

{

// Preparing Insert SQL Command

dataAdapter.InsertCommand = conn.CreateCommand();

dataAdapter.InsertCommand.CommandText =

"INSERT INTO article " +

"(artId, title, topic, authorId, lines, dateOfPublishing) " +

"VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)";

AddParams(dataAdapter.InsertCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

������ �� ��������".����"����������� "'���0�.��.���"� private void InitializeCommands()

{

// Preparing Insert SQL Command

SqlCommand insertCommand = new SqlCommand("InsertProc", conn);

insertCommand.CommandType = CommandType.StoredProcedure;

dataAdapter.InsertCommand = insertCommand;

AddParams(dataAdapter.InsertCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

insertCommand.UpdatedRowSource = UpdateRowSource.None;

...

����'�!���������"� �J������ "�07��� ���"� �������� "�..��� �� ����� �������������� ����!�������������".����"���*� �������� ������+� "����� ����� ��0���"� SqlCommand insertCommand = new SqlCommand("InsertProc", conn);

Page 251: 3Programmer's Heaven C# School Book

��������������� ���������

1=�

����� ���������� "��.�.��.�����!���� �������� "�07�������������"�����"���� �������� ������������������ ���������������� "��"����������".����"���� insertCommand.CommandType = CommandType.StoredProcedure;

&� ����'��""�"�������� "�����"����"�.����07����� ���� �������� ".��.�����-����(�� ���.�������8��.��'���������""�".������������������ "� dataAdapter.InsertCommand = insertCommand;

AddParams(dataAdapter.InsertCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

8 ��� . ���� � �� E�������;<���� �

�� ��'�� �� �����".����"���9�����"�!��"� ������C������ "*+�����"�� private void InitializeCommands()

{

// Preparing Select Command

SqlCommand selectCommand = new SqlCommand("SelectProc", conn);

selectCommand.CommandType = CommandType.StoredProcedure;

dataAdapter.SelectCommand = selectCommand;

// Preparing Insert SQL Command

SqlCommand insertCommand = new SqlCommand("InsertProc", conn);

insertCommand.CommandType = CommandType.StoredProcedure;

dataAdapter.InsertCommand = insertCommand;

AddParams(dataAdapter.InsertCommand, "artId", "title", "topic",

"authorId", "lines", "dateOfPublishing");

// Preparing Update SQL Command

SqlCommand updateCommand = new SqlCommand("UpdateProc", conn);

updateCommand.CommandType = CommandType.StoredProcedure;

dataAdapter.UpdateCommand = updateCommand;

AddParams(dataAdapter.UpdateCommand, "artId", "title",

"topic", "authorId", "lines", "dateOfPublishing");

// Preparing Delete SQL Command

SqlCommand deleteCommand = new SqlCommand("DeleteProc", conn);

deleteCommand.CommandType = CommandType.StoredProcedure;

dataAdapter.DeleteCommand= deleteCommand;

AddParams(dataAdapter.DeleteCommand, "artId");

Page 252: 3Programmer's Heaven C# School Book

��������������� ���������

1=�

}

$�������'�������"��� ����!�������".����"���� ����� �����������������J������ "G�07����%�� ������� �''��������� ���"�"������������� "� ���"����"�.�������� ���������� ����� ��"��"� �'�� ���#��"��0��0���� G��� ��� "������ ��'���� �'�� �����".����"������������������"!��������0��9'� ��"����� �����#��"��0��0���� ��� ��� "���������"�!��"����� �!������ ��� "����� private void btnLoadTable_Click(object sender, System.EventArgs e)

{

this.Cursor = Cursors.WaitCursor;

string connectionString = "server=P-III; database=programmersheaven;uid=sa; pwd=;";

conn = new SqlConnection(connectionString);

dataAdapter = new SqlDataAdapter();

InitializeCommands();

ds = new DataSet();

dataAdapter.Fill(ds, "article");

dataTable = ds.Tables["article"];

currRec = 0;

totalRec = dataTable.Rows.Count;

FillControls();

ToggleControls(true);

this.Cursor = Cursors.Default;

}

����'���� ����"��������������"'���0��"!������� �� ����0�����"��$�''���������� �����J�����%"�.����07����� ����"�!���� �E������ ��� ���������������'���� ��.�� ���������������� "�'������ ��������".����"���!����� "'���� ��.�� �������� ����� �07���� �'��������� "����"� ������ ����� ��!��� ��� "����������� "����!� ����0�� �""�"�����"����"�.���������� "��� ����������"��� ��!���� ������C������ "*+�����"�������� �'�����"7��0�!���!���� ����"������ �'������������� "����0�� �.��.���"� �"����� ������C������ "*+�����"� ���������� ��"����� ��� ���.�������8��.���..������� ����.��������F#����� "'�������".����"����� G�����.��O������.�����������"��!����..������� �� 0�"�' ���"�"0�����(� ������ ����������� ����������� ��������� � ��������� ������������������ ������ �������� ����������+����������8���� ��������

-� ���+,����� ��� �8���6�����������"������+,����� ,����������� ����������� ������%��������������������� ��� ��������

Page 253: 3Programmer's Heaven C# School Book

��������������� ���������

1=/

�������������������������������'�������� ��������?� ������������%��� �� ����(-'���������������������������������

�������������������������� ��������������� �������������������������������� ������������ ���� ������������� ����

���������������������������������������������� ������������������ ������� �������������������������������+-78�'2��

��62='��� �82:2'2,���������������2$�������@����+,����� ������������ �+65�"���� ���0��8"���� ,�

.������� ������������������������������� ����������������������� ���������������������������������������� �

B� ���2����)� ���������@ �6��*�������

����3��"������ "��"�� ����!�����'� �"���� ����$��� ���� �� ��%"������"�� �������.��� ��"� �$��0������������ "�'�&��������3��"�������"������"�� ������ "�.���"���!���� ��0���!"��������9�����"�����0��9"�����9"������'��� ������%"������"�� �������(��(�����

-�2�������� ��-$$� ��� ��.��2����)� �������

#��G���������.���..������� !����������"��0��"���!����"���0������������"������"�� �����&����!����""�"������"�� ����� "�0���� ������!����� ����5�������"������0�8������������$���.��.�����!���"������"��G"�������G� "����.��� ��8�.��.������G��������������� ����0��G���� ����!���0���� �G0� #��"����G������� ��� "���!�����0���� �� private void btnLoadData_Click(object sender, System.EventArgs e)

{

string connectionString = "server=P-III; database=programmersheaven;uid=sa; pwd=;";

SqlConnection conn = new SqlConnection(connectionString);

string cmdString = "SELECT * FROM article";

SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);

DataSet ds = new DataSet();

dataAdapter.Fill(ds, "article");

dgDetails.SetDataBinding(ds, "article");

}

Page 254: 3Programmer's Heaven C# School Book

��������������� ���������

1?2

����'�!���������"�"����"�.���� "!����"���"������� ���9�'�"�"� ���.�������..������� ����� �� �'��� �����0� "� ��!���H�������H��0�������"������"�� ����9'�����"� �0������ �����������@� "� �*+�����"�!�������3��"�������!���.���������!��������"����"�����9'���������� ".������������ ����!��0��� ���"������ dgDetails.SetDataBinding(ds, "article");

��� ����8��������.������� "��������#��"0���� ���'�����������.��.��� ��"� ���.������!������

&����2�������� ��F�B� ������� $�����������������

$�''�'�������''��� ���������3��"�� ��������'�����.��������"��0������ �'���0�����������"9� �������"���.��� ���0��� "��������������"�������"��0����������"��0���� ��� ���.������(���!���.��� ���0����!����� (���&���8��.��9� ������������������ "���0��9��0��%���������.��� ���0���!���%��������0������%��������0���� ��� G%������"G��!����� (��9'��������.������(��� ���%�������0��� � ����8��.��9'�'��������"������"����'���������"�����"!�������������� "��������0���� ��"����.���!����������� ��.0��'�� ����'���0��'� ��"�����������)������ ���� DataRelation relation = new DataRelation("ArtAuth",

ds.Tables["author"].Columns["authorId"],

ds.Tables["article"].Columns["authorId"]);

�������!��������� ��!�������)������ �� ����������� ���!����� �'������� 9'���������� "� "����"������ ������������ �!�����0��'����'���0���"������������'���0���%!��������� ����������� ��.'� ��"���""�������)������ ��������� �!���"������ ds.Relations.Add(relation);

�� �������"�!��"��"�!�����#��"����0���� �� private void btnLoadData_Click(object sender, System.EventArgs e)

{

string connectionString = "server=P-III; database=programmersheaven;uid=sa; pwd=;";

SqlConnection conn = new SqlConnection(connectionString);

string cmdString = "SELECT * FROM article";

SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);

DataSet ds = new DataSet();

dataAdapter.Fill(ds, "article");

Page 255: 3Programmer's Heaven C# School Book

��������������� ���������

1?

cmdString = "SELECT * FROM author";

dataAdapter = new SqlDataAdapter(cmdString, conn);

dataAdapter.Fill(ds, "author");

DataRelation relation = new DataRelation("ArtAuth",

ds.Tables["author"].Columns["authorId"],

ds.Tables["article"].Columns["authorId"]);

ds.Relations.Add(relation);

DataView dv = new DataView(ds.Tables["author"]);

dgDetails.DataSource = dv;

}

� ����0�����"�'�!���!����"���"�����'�������'���0��9"�!� �"���������� ��.0��'�� ����� "��� �""�"�������"������� �������'��� �9'�������"� � �� ���!�������5��'���0��..��� ����.��� ���0��� ���� ������������ "��� ���������������.��.�����!���"������"�����"������'� ��� '����.���� "�8���������..������� 9���"������"'�����'��������"�!.��� ���0��'���G4G0���� � �����!��!���������"�

��� ���.�����G4G0���� � �����!��!��������"9��'����8.� "����'��� ����!���������� ��.���� (�

Page 256: 3Programmer's Heaven C# School Book

��������������� ���������

1?1

$�''�� �������(���������� ���9���"������"'�����'������������"�����"� �������"��0���

���������� �����.��� ������"�������.�!��������'�!�������"��0���:���� ��0��(�����.��� ���0���� ����0��(����'0���� *^\\+����������0���!���"������"�

9��� �3 ��������� ���� ��&(�(�8�������

&���������� �"����� �����6#6������� "9'��� '�������!����'� ���"�� private void btnLoadData_Click(object sender, System.EventArgs e)

{

string connString = "server=siraj; database=programmersheaven; uid=sa; pwd=";

SqlConnection conn = new SqlConnection(connString);

string cmdString = "select * from author";

SqlCommand cmd = new SqlCommand(cmdString, conn);

conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

Page 257: 3Programmer's Heaven C# School Book

��������������� ���������

1?;

while(reader.Read())

{

txtData.Text += reader["authorId"].ToString();

txtData.Text += ", ";

txtData.Text += reader["name"].ToString();

txtData.Text += "\r\n";

}

conn.Close();

}

����'�����������"� �J��� ����� � "� �J������ "�07���0��� �����6#6�������� ������� �.� �"����� ����� �� ����A.� *+�����"�!����J��� ����� ���� conn.Open();

%!����.� � ������ ����� '������8�����"�������� "�� ����68�����)��"��*+�����"�!����J������ "������������"����� � �07����!��.�����)��"��'������ 0���"�����"���"�������� �"��������!��������J����� SqlDataReader reader = cmd.ExecuteReader();

��� '����"�����������"� �����0���� �������"���07�������)��"*+�����"�!�������)��"������"�� ���������� ������".�� �������� �8������"� "����� �����������!�������!��9�'����������"��������"� ���'���������� ���������)��"��������������"�"� � "�8��.��.����� ���07���'������(���� ���*�� ��0��+�!�������� � "����� ��������� ���.���!��"����� �!�������� ������"� while(reader.Read())

{

txtData.Text += reader["authorId"].ToString();

txtData.Text += "\t";

txtData.Text += reader["name"].ToString();

txtData.Text += "\r\n";

}

&� ����'���������"���"���0���� ����� 0������ ��������*+�����"�!����J��� ����� ���� conn.Close();

������'����� ����0�����"�� ������ ��� "����!���#��"@���� �!���!���� "������.���"�.����"� ���8�0�8 ���"G�8�����G

Page 258: 3Programmer's Heaven C# School Book

��������������� ���������

1?B

B$�� ���9������� ����*&(98'�B"2-8(����2(�(8(��������

���.����"���!���."��� ������"�� ��$�6)�9<��%�6� "�6#6�6����� "����������������� �'�.��� ��"� ���.�������8��.���8��.����������������� ""�� ������� � ���� �� "�����������"������� ����J������ "�07���������"68�����$� F����*+����"��� ������ ��"�!������� ������ private void btnExecuteNonQuery_Click(object sender, System.EventArgs e)

{

string connString = "server=siraj; database=programmersheaven; uid=sa; pwd=";

SqlConnection conn = new SqlConnection(connString);

// Un-comment any one of the following three Queries

// INSERT Query

string cmdString ="INSERT INTO Author " +

"(authorId, name) " +

"VALUES(3, 'Anders Hejlsberg')";

// UPDATE Query

/*string cmdString = "UPDATE Author " +

"SET name = 'Grady Booch' " +

"WHERE authorId = 3";*/

// DELETE Query

/*string cmdString = "DELETE FROM Author " +

"WHERE authorId = 3";*/

SqlCommand cmd = new SqlCommand(cmdString, conn);

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

}

$�������� �����"��0���'�������"����$�6)�9<��%�6� "�6#6�6����� "������� "��� �� "'����������"���68�����$� F����*+�����"�!����J������ "�07����

Page 259: 3Programmer's Heaven C# School Book

��������������� ���������

1?=

����/��� � ��� ���

������"����

��"��'�'������� ��'�������������������"� �� ��� "�$�����'����������0����(� ���'�������������"� ��� "'��'� ��"���#����'�'���"��� �������''��� ��.���� �����������"� �� ������..������� �&� ����9'�'������� �0������"�!!��� ���������"� ������"� ���� �C���� � "��'���� "�������

5 ��� ��/��� � ��� ���

,���������"� ���!������.����"�"0�����.����� ���������� �0�������..������� ������������� � ��8������ .��������������������������"���� "�'G�������(� ��0������9'��������'����8������������ � ��..������� �������������-������� �'���'���� ���� B����� �!������������������ G��������� ,�����!����"9���� � �����!�������� �� �� %�.� ""�' ���"� �� �'� ��� �� ��� ����' ���",� ������ ��������� ��9'����������������"� ����� "�!!��� ������"�!���.�������������������,���������"� ����������� ����� ��!��"��G.�������� ��������"�!!�������!� "'� "�'�..������� ����"� G������&���8��.��9,�����!����"��(����� .��� ""�.������ ������� � � ������"'�������� �� ��������(.���� �� "���������������(�� ������ "�����"9� "�������������������"�����"������"����� ������������������������ �������� ��������� ��9�� %�..�������� � ������"9"�.���������C���� � ������ "� "��(����� .��� �������"�����J����"�!!��� �!����������(� �������� ����..������� �"�� ������.����(������������9'����� �������(� �"�!!��� ��..������� �8������������������ ����������� ���������������������������������������� �����������������������������������������������������������

�$�������������������������������������������������������������������������������������������'�����������������

�����������������$���������������������������������������������������������+������������������,��������������������

������� ������ ��� ������������������$����������������������� ���������������������+��������%���,��� �����������

���������������������� �������������� ��+���������� ���,�� �

Page 260: 3Programmer's Heaven C# School Book

��������������� ���������

1??

�� �"�����!����'� �����.������� namespace CSharpSchool

{

class Test

{

static void Main()

{

Fun1();

Fun2();

Console.WriteLine("End of Main()");

}

public static void Fun1()

{

for(int i=1; i<=5; i++)

{

Console.WriteLine("Fun1() writes: {0}", i);

}

}

public static void Fun2()

{

for(int i=10; i>=6; i--)

{

Console.WriteLine("Fun2() writes: {0}", i);

}

}

}

}

������.���!���.�������� Fun1() writes: 1

Fun1() writes: 2

Fun1() writes: 3

Fun1() writes: 4

Fun1() writes: 5

Fun2() writes: 10

Fun2() writes: 9

Fun2() writes: 8

Fun2() writes: 7

Fun2() writes: 6

Page 261: 3Programmer's Heaven C# School Book

��������������� ���������

1?�

End of Main()

Press any key to continue

%'��� ��9��������"&� 1*+� �������"���8������ '�� &� *+��"���.����"���8������ �����0�����'�� ������"��������"9����8������ �� ������� !�������������"9� "'�� ��������"����� ����8������ ����!���������� �8��� ��!�����"����������"��������"9����9���.��������.���������� ��� ��8������ .���� <� �����������"� �9'��� "�!� ������.���� ����� ��8������ .���'���� ���.�����������"�����"�&���8��.��9'��� �������"���������'������"&� *+� "&� 1*+����8�����'������'���� �!������������������ ����

/��� � ��� ��� �����

����$��&����'��(9� "�����9.����"�!����..���!�������.���8������ �����"� �.�������:���� �""�����"� �!� ���� ������������..������� 0��� ���������������"� � ���.����%�����"� �$�����.��� ��"0���������������"� �������"�������� �����������.�������"� ���.������0������� ������.��� �� ��*�07���+�!�������%�����"�������8������ 0������ ����.���!��"�����"� "����� ���'�� ����8������ �!���������"������.����"����� .���!���������" ���������������"'�������'�� ������0�.�� ��"��������!��������"�������.�� ��������"����� �����������"������������������"� �������"����������!��� ��� ������"'�����������"����� ��.�� "'������(� ������� �� public delegate void ThreadStart();

&���8��.��9'��� ��� �����.�������..������� ���� ����'������"� �'�"�!!��� ������"��(����� Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

����'�����������"�'�� �� ���!��������"���� ".��"������"�������.�"�������� ����� �������'������!��� ��������"� ���.������������.���� �������������"��!��� ��"� ��������"����� �������9���������������"�����"�������9�.����������� "��������"����� ��.��%�����""�� ���������8������ '�� ���07����������"�)�����9'���������������8������ �!������"0������ ���������*+�����"�!��������"���� firstThread.Start();

secondThread.Start();

����'����������"��������*+�����"�!!��������"9'����� ��� '����������&� *+�����"� � �'�����"���'���������������8������ '��� ������� ���������.����� �!���&� *+�����"90��'�������"�������� �� ��'������ �8������� �'���������������8������ �!&� 1*+�����"� � �'�����"�%��� 9������

Page 262: 3Programmer's Heaven C# School Book

��������������� ���������

1?�

�����"�!����..������� '��� ��'���!��������.����� �!���&� 1*+�����"� "'����� �� ��'������!����'� ������� ��������.�����������"�!������..������� �� using System;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

Console.WriteLine("End of Main()");

}

public static void Fun1()

{

for(int i=1; i<=5; i++)

{

Console.WriteLine("Fun1() writes: {0}", i);

}

}

public static void Fun2()

{

for(int i=10; i>=6; i--)

{

Console.WriteLine("Fun2() writes: {0}", i);

}

}

}

}

A �.��0�����.���!���.�������� End of Main()

Fun1() writes: 1

Page 263: 3Programmer's Heaven C# School Book

��������������� ���������

1?/

Fun1() writes: 2

Fun1() writes: 3

Fun1() writes: 4

Fun1() writes: 5

Fun2() writes: 10

Fun2() writes: 9

Fun2() writes: 8

Fun2() writes: 7

Fun2() writes: 6

Press any key to continue

���"�"'���G� �.��0�����.��GQ������� �����'��� G�0�����0�������8������ �J�� ���!��������"������"'����� �����.������A.����� ������"�.� "� �� "������ ��������������8��������.�����������'���'� �������������,�� *+�����"� "�"0�!�����������!� ��!���������'������"90���!�����������'�!� ���� �����0������ �� ��J�� �������'�����������8.����"'����.�������� �!����'������"���� �� ���8�"'�����'��"�" G�'�����������.��Q� !���9��������"&� *+� "&� 1*+������������8������ ���������������!� ���"��� 0�!������'����� ��!����'������"!���� ���������!'�� �����������.��� ����!���������"9'���� �������������"� �8������ �

8 ��������� ��� �#�

��������"���.����"�� ��0���!��!�������"� ".��.��������� ����� "�� ��������"�8������ �

&��� ����������.�� ��&#�����8 ��� ���8 ����������

��������.��.��������� ������"�������!��� ������������ ����8����� ������"�% �������.���� ���������0���!��������"�����������.*+�����"��������������� ����8����� ������"��.������.�������!�����.���!��"���� ��!����� ��������"�����.*+�����"��(��� ������ �������� ��!����*� �������� "+!��'����'�'� ���.�����������"�&���8��.��9'��� .����������� ����8����� ������"!�� ��� "0�.�� � 222�� ������ �����������"�����.*+�����"� static void Main()

{

Console.WriteLine("Before Calling the Thread.Sleep() method");

Thread.Sleep(1000); // blocks the currently executing thread (Main thread) for 1 second

Console.WriteLine("After Calling the Thread.Sleep() method");

}

Page 264: 3Programmer's Heaven C# School Book

��������������� ���������

1�2

��� ����8���������0���.������9���'��� ������"�����! ��� "0��'�� ���.�� �� ��!����'��� ��

�����������������.�� ��&#�����8 ��� ���8 ����������

����������� ����"� �� �����0���!��������"������� ,��0�� �����.��� $��� %.��.�����!��� ���.���"�����I�����!��� "�� ����!��������"� �� ����������� %.��.�����!��.������������"� �������"������������.��.������������I����������

� "����� �������"��� �.��������!��������"����.��������� 0�� �� �� ���!��������"��������� �������� '����� ���"�%0���$�����9@���'$�����9$�����9������� "#�'���

�%���� %@����� .��.����� "����� �'��������������"����������0�� ����� ���"������"����� %.��.�����!��.������������"� �������"���������.��.�������"�������������

�� ��� � ���������!��������"�������������� �"0����.��.������ � �� ���!��������"������ �������� '����� ���"�%0����"9%0���)�J����"9��.� "�"9���..�"9< �����"9)� � �9��������.-�� 9����

�����*+ ���������8������ �!��������"�%0���*+ %���'�������� ������"����.����8������ �!������".���� � ������������"����'���

�����"%0���68��.��� � ����8����� ������"���.� "*+ ��������8������ �!������"���.��������)����*+ )��������8������ �!��.� "�"�����"�-�� *+ ,�(��������� ������"'���!��� ����������"��!� ���

8 ����2�������� ��(0��$���F�7�� ���$���� ���

$�''�'���������� "���� "�����.���� ����� �!�����"� ����� �"�����!����'� ����� ���.������� using System;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static Thread mainThread;

static Thread firstThread;

static Thread secondThread;

static void Main()

{

mainThread = Thread.CurrentThread;

firstThread = new Thread(new ThreadStart(Fun1));

Page 265: 3Programmer's Heaven C# School Book

��������������� ���������

1�

secondThread = new Thread(new ThreadStart(Fun2));

mainThread.Name = "Main Thread";

firstThread.Name = "First Thread";

secondThread.Name = "Second Thread";

ThreadsInfo("Main() before starting the threads");

firstThread.Start();

secondThread.Start();

ThreadsInfo("Main() just before exiting the Main()");

}

public static void ThreadsInfo(string location)

{

Console.WriteLine("\r\nIn {0}", location);

Console.WriteLine("Thread Name: {0}, ThreadState: {1}",

mainThread.Name, mainThread.ThreadState);

Console.WriteLine("Thread Name: {0}, ThreadState: {1}",

firstThread.Name, firstThread.ThreadState);

Console.WriteLine("Thread Name: {0}, ThreadState: {1}\r\n",

secondThread.Name, secondThread.ThreadState);

}

public static void Fun1()

{

for(int i=1; i<=5; i++)

{

Console.WriteLine("Fun1() writes: {0}", i);

Thread.Sleep(100);

}

ThreadsInfo("Fun1()");

}

public static void Fun2()

{

for(int i=10; i>=6; i--)

{

Console.WriteLine("Fun2() writes: {0}", i);

Thread.Sleep(125);

}

Page 266: 3Programmer's Heaven C# School Book

��������������� ���������

1�1

ThreadsInfo("Fun2()");

}

}

}

&����!���'�����"�!� �"������������!��� ���!��.������������"� �������"����!��� ���������������"*��� 9!���� "��� "�����"+������ ���,�� *+�����"� static Thread mainThread;

static Thread firstThread;

static Thread secondThread;

������"�!� �"�����������"�����"�����"� !�*+��"�.������� !������� * ���� "����+�!�������������"�����'������"&� *+� "&� 1*+��������������.������.������� "7��.�� �= ��0���� ������.�!���������"'����������"�������.*+�����"'����'�����(���������"�8����� ���������"�.� "!�����.���!��"���� ��!��������������������"�!!��� ������ ������������"G����.*+�����"�%!���������.9'�����.�� ��"���� !������� �0��������������"���� � public static void Fun2()

{

for(int i=10; i>=6; i--)

{

Console.WriteLine("Fun2() writes: {0}", i);

Thread.Sleep(125);

}

ThreadsInfo("Fun2()");

}

� �"����,�� *+�����"'�!���� �� �����"����'������"� �� ��*!��������"� "��� "�����"+0�.�� �������� ������������!��� ���!���&� *+� "&� 1*+�����"��.���������� ���������"�����"���������������"������!��� ����� �����".�� �����������"�8����� ����,�� *+�����"0��� ���������"������ ������".��.����� ���,�� *+�����"� static void Main()

{

mainThread = Thread.CurrentThread;

firstThread = new Thread(new ThreadStart(Fun1));

secondThread = new Thread(new ThreadStart(Fun2));

����� �����$���.��.�����!���������"����������"�����.� "� � ���� mainThread.Name = "Main Thread";

Page 267: 3Programmer's Heaven C# School Book

��������������� ���������

1�;

firstThread.Name = "First Thread";

secondThread.Name = "Second Thread";

%!������� ���� ���9'�.�� ��"�������� ������!�������������"0������ ��������������"� !�*+�����"9�����"����'������"� "!� ���������"��������"� !�*+�����"7��0�!������� "�!���,�� *+�����"� ThreadsInfo("Main() before starting the threads");

firstThread.Start();

secondThread.Start();

ThreadsInfo("Main() just before exiting the Main()");

A �.��0�����.���!���.�������� In Main() before starting the threads

Thread Name: Main Thread, ThreadState: Running

Thread Name: First Thread, ThreadState: Unstarted

Thread Name: Second Thread, ThreadState: Unstarted

In Main() just before exiting the Main()

Thread Name: Main Thread, ThreadState: Running

Thread Name: First Thread, ThreadState: Unstarted

Thread Name: Second Thread, ThreadState: Unstarted

Fun1() writes: 1

Fun2() writes: 10

Fun1() writes: 2

Fun2() writes: 9

Fun1() writes: 3

Fun2() writes: 8

Fun1() writes: 4

Fun2() writes: 7

Fun1() writes: 5

In Fun1()

Thread Name: Main Thread, ThreadState: Background, Stopped, WaitSleepJoin

Thread Name: First Thread, ThreadState: Running

Thread Name: Second Thread, ThreadState: WaitSleepJoin

Fun2() writes: 6

Page 268: 3Programmer's Heaven C# School Book

��������������� ���������

1�B

In Fun2()

Thread Name: Main Thread, ThreadState: Background, Stopped, WaitSleepJoin

Thread Name: First Thread, ThreadState: Stopped

Thread Name: Second Thread, ThreadState: Running

Press any key to continue

�����.���� ���� ��� ������������J�� ���!�8������ � "��������"������"�!!��� �.�� �"��� �����8������ �!���.�����������'������"*!��������"� "��� "�����"+"�" G���������"��� '�� ���,�� *+�����"'��8��� ��%����� "�!!��������"9���,�� *+�����"����..�"'���������� "�����"�� �������.�����

8 ����2�������� ��(0��$���F�8 ����"� � �#�

��� �'������������"����8����� ������� ����������������.������������ ������� "���� 9����.����� �����������"����0������.�����������J�������� ����������"�!�.�������'����9�!'�'����� !��� ����'.�����������"����0���"9'��� ���.���!����.������������!����������"�� �$��'�"������ ���������������"� �������"��������� �������� 9'������ ��� $�����9%0���$�����9@���'$�����9������� "#�'������"�!����.�������������!������"�9�� �� �G��.���9$������%�����"'����������.������������ ��������0�A.����� ��������� ������"'������'��.���������� �"�����.������0���''��� �.����������� �� class Test

{

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Name = "First Thread";

secondThread.Name = "Second Thread";

firstThread.Start();

secondThread.Start();

}

public static void Fun1()

{

for(int i=1; i<=10; i++)

{

int t = new Random().Next(20);

for(int j=0; j<t; j++)

Page 269: 3Programmer's Heaven C# School Book

��������������� ���������

1�=

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

public static void Fun2()

{

for(int i=20; i>=11; i--)

{

int t = new Random().Next(20);

for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + "empty strings");

}

}

}

����'������(�"����� ������������� ����������� ��0���!�07���� ����'������"�����"*&� *+� "&� 1*++�A �.��0�����.���!���.�������� First Thread: created: 18 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

Second Thread: created: 16 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Page 270: 3Programmer's Heaven C# School Book

��������������� ���������

1�?

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Press any key to continue

$�''�'������ �����.��������!��� "�����"��%0���$������ class Test

{

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Name = "First Thread";

secondThread.Name = "Second Thread";

secondThread.Priority = ThreadPriority.AboveNormal;

firstThread.Start();

secondThread.Start();

}

public static void Fun1()

{

for(int i=1; i<=10; i++)

{

int t = new Random().Next(20);

for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

public static void Fun2()

{

for(int i=20; i>=11; i--)

{

Page 271: 3Programmer's Heaven C# School Book

��������������� ���������

1��

int t = new Random().Next(40);

for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

}

����'�������"��'���� ���������� �����"���.��������!������ "�����"��%0���$���������������� �����"����� ��!���� "�� ��0������������� "�����"'���"0���J����"���������������� ��0���!�07����A ���.��� �� "�8����� ����.������9'�������.����(�� Second Thread: created: 14 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

Press any key to continue

�� �"������0������.���%������������� "�����"������� ������07���9������!� ���0�!������!��������"�������� ��.������� �'���.�������������!��� "�����"���������� �����!���!��������"�

Page 272: 3Programmer's Heaven C# School Book

��������������� ���������

1��

8 ����2�������� ��(0��$���F�8 ����(0���� ��������

��������"������.����"������"!���� ������ �����8������ �!"�!!��� ������"�:���� ����9��.*�����"�0���+9�.� "� "������.� "�"�����"7��0������ ��� ��������"� ��������"�07������ �"�����!����'� �"��� ������ �..������� � static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

firstThread.Start();

Console.WriteLine("Thread started");

Thread.Sleep(150);

firstThread.Suspend();

Console.WriteLine("Thread suspended");

Thread.Sleep(150);

firstThread.Resume();

Console.WriteLine("Thread resumed");

Thread.Sleep(150);

firstThread.Abort();

Console.WriteLine("Thread aborted");

}

public static void Fun1()

{

int i=1;

try

{

for(; i<=20; i++)

{

int t = new Random().Next(20, 50);

Console.WriteLine("Thread 1: slept for: " + t.ToString() + " milliseconds");

Thread.Sleep(t);

}

}

catch(ThreadAbortException ex)

{

Console.WriteLine("Thread 1 aborted in iteration number: {0}", i);

}

}

����'����������"9�.� "�"9�����"� "�0����"��������"*!��������"+'������ �� �"�!!��� ���! =2�������� "�)����0������'�� ������"��0����"����� ��������'��������"%0���68��.��� � ��������"

Page 273: 3Programmer's Heaven C# School Book

��������������� ���������

1�/

�����"�����8��.��� ����'��������"��.��!���������� �.'��(0�!�����G����� ���� 9�������� �����.� �""���0��9 ��'��(��!����� ����� ���� '��8���������0���.������9'������!����'� ����.��� Thread started

Thread 1: slept for: 35 milliseconds

Thread 1: slept for: 29 milliseconds

Thread 1: slept for: 49 milliseconds

Thread 1: slept for: 36 milliseconds

Thread 1: slept for: 33 milliseconds

Thread 1: slept for: 30 milliseconds

Thread suspended

Thread resumed

Thread 1: slept for: 44 milliseconds

Thread 1: slept for: 48 milliseconds

Thread aborted

Press any key to continue

$������������� ������"��������0��'�� �����"�.� "� "������%'��"�!������ ��!�����������������.� "*+9)����*+��%0���*+�����"� � � E�� � ������"*�0����"��� E�����"+9���'��������������"�����68��.��� �

B� ���= �;<���6� ��.������ ���� �����

&� ����9����� ��(�������"'���!��������� � ������"�����.����0������ ����-�� *+�����"��� �"�������.����"�� static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

Console.WriteLine("Ending Main()");

}

public static void Fun1()

{

for(int i=1; i<=5; i++)

{

Console.WriteLine("Thread 1 writes: {0}", i);

Page 274: 3Programmer's Heaven C# School Book

��������������� ���������

1�2

}

}

public static void Fun2()

{

for(int i=10; i>=5; i--)

{

Console.WriteLine("Thread 2 writes: {0}", i);

}

}

� ����0�����"�9��������"�!���,�� *+�����"'�������� ���J���(���!�������� �����'������"�������.���!���.������'������(��(����� Thread 1 writes: 1

Thread 2 writes: 10

Ending Main()

Thread 1 writes: 2

Thread 1 writes: 3

Thread 1 writes: 4

Thread 1 writes: 5

Thread 2 writes: 9

Thread 2 writes: 8

Thread 2 writes: 7

Thread 2 writes: 6

Thread 2 writes: 5

Press any key to continue

@���!'���(���(��.���,�� *+�����"������ ������!��������"������9'��� �..��-�� *+�����"����� static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

firstThread.Join();

Console.WriteLine("Ending Main()");

}

Page 275: 3Programmer's Heaven C# School Book

��������������� ���������

1�

public static void Fun1()

{

for(int i=1; i<=5; i++)

{

Console.WriteLine("Thread 1 writes: {0}", i);

}

}

public static void Fun2()

{

for(int i=15; i>=6; i--)

{

Console.WriteLine("Thread 2 writes: {0}", i);

}

}

����'������ ����"������������-�� *+�����"�!!��������"� "� �����"������.��� ���!����� "�����"�$�'���,�� *+�����"�����"'��� ������� ���� ������!��������"�������A �.��0�����.���!���.�������� Thread 1 writes: 1

Thread 1 writes: 2

Thread 1 writes: 3

Thread 1 writes: 4

Thread 1 writes: 5

Ending Main()

Thread 2 writes: 15

Thread 2 writes: 14

Thread 2 writes: 13

Thread 2 writes: 12

Thread 2 writes: 11

Thread 2 writes: 10

Thread 2 writes: 9

Thread 2 writes: 8

Thread 2 writes: 7

Thread 2 writes: 6

Press any key to continue

����������� 6������������� ������ ������������������%�����������������������$�����������>������������������+,�

����� ��$��������������� ��������������� ���'���������������������������� ���������������������������������������

��� �� ����������$��������������������� �

Page 276: 3Programmer's Heaven C# School Book

��������������� ���������

1�1

8 ����&#�� �� E�� ��

��!��'������� ���.�������.����!�� ������.�������"�� ����!���.������.��� ��"�!��9��������"�!�.������'��� ������ �� ������ �������*�07���+���������"'����� �� ���������������0���!����������.� "� ������"�@��'�����..� '�� �����.�������"�������������������"�������Q���.��0������(���� "������������������� �������� ������'� ���'���������.������ ��9�����"�������(�������� "'�����!!����������� '������'� ���'���������������� ��� "�����"����������� �������� 9�'������.������ ��0��9� !���� �����9����������� "������(����������� "'����������������� ��90����� ��������������� "�$�'��� �� ��!������������� � "���������"� �� "��� '�����!!����������� �����'���"0����� "�����Q�'���"����� "�"'���������"� ��!��������� ����� ���"�!�������(������������������ '���"0�'���������"�9��'���"0������.�� ��������"!����'�����"�!!�������� O��������..� '��������.�������"����� ��� �������"�������*�07���+���������!����07������������ ��""��� ��� ���������������'���������( �'��"���!��������"����������" ��������.�� �Q#��G��(���������� �����8��.��P�..�������" ��������)�'!����������0��� "�����."��� �������� ������%�����������9�����"1����� "������������������)�'�07������."���������� ������@�����������"������"�����'0��(�������0��� ".������"���0���@��'����"�����'����� ��0�� ���"�����"���0��Q���� ��."���"0������" ������ ��."���"0������"1Q���� G�.��"�����������������'���� ������� ������� 0����� ��."���0������" ������ ��."���0������"1�������0������8�����!0����!�����."���c���'�����(������������������ Q ��'������������� Q���������.��������� � ����������"�07���'��������.�������"����������� "!� �90������'���������.���������.����������������"�� �����"�07���'��������.�������"�8����� ������� ������@��� ������9���"����0����������"�07���'��������.�������"��$��.����"�����(� ������ ��������"����"� ��������� �������0������.�������"������������"�07����

8 �������: ���/�� �� ���

�� �"�����!����'� ���"�� class Test

{

static StringBuilder text = new StringBuilder();

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

Page 277: 3Programmer's Heaven C# School Book

��������������� ���������

1�;

firstThread.Join();

secondThread.Join();

Console.WriteLine("Text is:\r\n{0}", text.ToString());

}

public static void Fun1()

{

for(int i=1; i<=20; i++)

{

Thread.Sleep(10);

text.Append(i.ToString() + " ");

}

}

public static void Fun2()

{

for(int i=21; i<=40; i++)

{

Thread.Sleep(2);

text.Append(i.ToString() + " ");

}

}

}

@�����������"����..� "� � ��0�����������"��� �0���"��*��������8������ �@���"��+�07����%������� ������.��9���!� ����� �'���"0������� ���(�� Text is:

21 1 22 2 23 3 24 4 25 5 26 6 27 7 28 8 29 9 10 30 31 11

32 12 33 13 34 14 35 15 36 16 37 17 38 18 39 19 40 20

Press any key to continue

���!� ����8��� � ��"���"�J�� ���! ��0���������"�����"� ���!��� �'�����������������9���������"����"���(�������"�07���0�!����������..� "� ���� ��0��� "������������('�� ���"� �����������07�������(�"9 �����������"����"0�����'�"����� ����������!����07���������8�����'�����������((��'��""������� ��� ������0���.��������.����"�������(� �!� ���� ������ class Test

{

Page 278: 3Programmer's Heaven C# School Book

��������������� ���������

1�B

static StringBuilder text = new StringBuilder();

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

firstThread.Join();

secondThread.Join();

Console.WriteLine("Text is:\r\n{0}", text.ToString());

}

public static void Fun1()

{

lock(text)

{

for(int i=1; i<=20; i++)

{

Thread.Sleep(10);

text.Append(i.ToString() + " ");

}

}

}

public static void Fun2()

{

lock(text)

{

for(int i=21; i<=40; i++)

{

Thread.Sleep(2);

text.Append(i.ToString() + " ");

}

}

}

}

$������� �'���������"����(� ��������"�07���G��8�G0�!���'��(� �� ����� ��9������.���!���.������'���0��

Page 279: 3Programmer's Heaven C# School Book

��������������� ���������

1�=

Text is:

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 30 31 32 33 34 35 36 37 38 39 40

Press any key to continue

A�9 Text is:

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Press any key to continue

:��'��������!������.��'�� ���!��������"'��������"�����(���G��8�G�07���!���'���������� "���.����'���������� "�����"�������"�"� ���(� ����G��8�G�07���!����

8 �������#�������2����:�

% �������������"� ������"� ���� �C���� �"��"���(��� �"�����������'�����"�07���%� "@������" !������(%� "��� @ // code for Thread 1

lock(A)

{

lock(B)

{

// do some work

}

}

����������� "�����"���(@� "��� % // code for Thread 2

lock(B)

{

lock(A)

{

// do some work

}

Page 280: 3Programmer's Heaven C# School Book

��������������� ���������

1�?

}

��..������'������"����������������� "!����'� ��8������ �J�� ����� �����"0����A.����� ������ �����" ��(����(� % �����"1��(����(� @ �����" '���!��������(� @���"0������"1��� �����"1'���!��������(� %���"0������" ��� �����" '��� �������������(� %� ����������������(� @� "��!� ���"��'��(�A ���������� "9�����"1'��� �������������(� @� ����������������(� %� "��!� ���"��'��(9'����'��� ������..� O�� �����.������'���0�"��"���(�"������������'������""��"���(���������!�8�� ���������������(� "�!"��"���(��������� "���0�������� "�.����� �������� ��9� ����0���������!��'�� �� ������.�����(����(� "�!0������������������"��"������ "�������������� ��8��.��� ����' � " ���������.����"���� ����P���.������7����.��.� "� �9����� ����� � �0����������� ����8.���� ���"��"���(������!9���.���� "�8��������!����'� �.������� using System;

using System.Threading;

using System.Text;

namespace CSharpSchool

{

class Test

{

static StringBuilder text = new StringBuilder();

static StringBuilder doc = new StringBuilder();

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Start();

secondThread.Start();

}

public static void Fun1()

{

lock(text)

{

Thread.Sleep(10);

Page 281: 3Programmer's Heaven C# School Book

��������������� ���������

1��

for(int i=1; i<=20; i++)

{

text.Append(i.ToString() + " ");

}

lock(doc)

{

doc.Append(text.ToString());

for(int i=1; i<=20; i++)

{

doc.Append(i.ToString() + " ");

}

}

}

}

public static void Fun2()

{

lock(doc)

{

Thread.Sleep(10);

for(int i=21; i<=40; i++)

{

doc.Append(i.ToString() + " ");

}

lock(text)

{

text.Append(doc.ToString());

for(int i=21; i<=40; i++)

{

text.Append(i.ToString() + " ");

}

}

}

}

}

}

����'�������"�'�����"�07���*��8�� ""��+�@�����������"��������.�� ������(����'��07���� ����..������"����!� �.����������� 90�����������"���(�����!����07���������������9�"��"���(�0�� "��������:������� ��!� "� �"��"���(� ���!����� �@��� �8����� ����.���������� � "���� 9���'�������������.���� ��"��"���(�"������� �� ������9���.������'����� �� "��.��.� "� ��

Page 282: 3Programmer's Heaven C# School Book

��������������� ���������

1��

����8 ��� ���&#�����!�&�������

������"����

��"��'�'������� ��''��� �� �.���������� "�'!�������� ""�!!��� ���.��!������� ���� "�$�����'����������0����(� �����''��� �� �.�����.������"����9!��"��� "!���9� ".��!���"�!!��� ��.������ � ������ ������ "���!�!������ 9'�'����8.�������"�!!��� ���.��!�������"� �$��� "����''��� ���""���!���� "'����"�����!����&� ����'�'������� �0�������� ��.��!������C���� �!�07���� "��''��� ��.���� �������C���� � ��������� ����� ��� ��..���� ������.��� %� ���� ���IA�

5�: ���6 � �� ��� ���&#�����

�������� �!������"���"�"� �������.����� ���!���.���9'�'�������''��� �������!�'�������K� ���� �� �� !������� 9�������.���������� "�'�����!��"��D����������&���!��"��9����� ���� ���9�.����� ���������� 9����� ������ ".���9'�'������� ��'��.��!�������� !����.������ ������.�� �9���� �9"����� �9�� ��� �� "������ ������.����!�������� 9'�'����8.����"��������*��&��"��+�� �.������ ���� �J���

���� � ���� ��-$$� ��� �L��(�3 ���������.���� ��M�8 ��&#�����(�3 �������������

���������6 ���� �� ��������0�������"���0��� � !������� �0������� ���� �� ��!����..������� ����"����.��� �!����!��.��.������ "�����"�.��� ��"� ���!����'� ���0��� ,��0�� �����.��� ����� ���������� 3���������"�������� ���!���'�������.����'������"�,���� �$��� 3����� ����!���.����� '�������.���������� ����8����� ��A�5���� )���� �������� �!����� �A.����� �������<��$��� )���� ������ ����!�������8����� ����.�����5���� )���� �������5���� �07�����.��� �� �������.��������� � !������� �!���

����� �� ������� ����*�#)+�68��*+ ����� ����������� �.����9����� � �����8����"������A.����� �������3��&��"������*+ )���� ������.����.����!�������� "��"!��"���!���'� "�'�.����� ������

��(��������!���9,�"����� �9�����,� �� "����3��#�����������*+ )���� � ������!��.���� ��� ��� � ��������!���"����.��� �� �������� ������

2�������� ��-$$� ��� ��M�(�3 ���������.���� ��

#��K��(��"��� ������ �..������� ����������0����� ��� �".��.������ "�����"��"�.���� ���� �� �� !������� �����'� "�'�..������� .��7��������� ��� ����0�8 ���"g�08K'�������"��

Page 283: 3Programmer's Heaven C# School Book

��������������� ���������

1�/

"�.������� !������� 9�0���� ���"g0� 3�K'����'���0���"������!����� ����� !������� � "�0���� ���"g0� 68��K'��������� �������..������� ������� ������.��� �� �"����3�0���� G��� ��� "���� private void btnGo_Click(object sender, System.EventArgs e)

{

OperatingSystem os = Environment.OSVersion;

PlatformID OSid = os.Platform;

string[] drives = Environment.GetLogicalDrives();

string drivesString = "";

foreach(string drive in drives)

{

drivesString += drive + ", ";

}

drivesString = drivesString.TrimEnd(' ', ',');

lbx.Items.Add("Machine Name: \t" + Environment.MachineName);

lbx.Items.Add("Operating System: \t" + Environment.OSVersion);

lbx.Items.Add("Operating System ID:\t" + OSid);

lbx.Items.Add("Current Folder: \t" + Environment.CurrentDirectory);

lbx.Items.Add("CLR Version: \t" + Environment.Version);

lbx.Items.Add("Present Drives: \t" + drivesString);

}

����'�������.����������"���� ���� �� �� !������� !���.�0���.��.������ "�����"� "�""�"������������0�8�%!�'��.���� ���� ��� �����������

�� 6 ���� �� ��3��#�����������*+����� � ������!��� �9'���������� ���.��� �� ����"���� ���� �� 6 ���� �� ��5���� ����� � �07����!��.�������5���� '������ ��� ���"������"� !�������

�0����������� ������ �!�������� �� ������� ����*�#)+� �� ������ ��� "���!���8��0���� ��.���������68��*+�����"�!���6 ���� �� ����������� ������

����� �.����� private void btnExit_Click(object sender, System.EventArgs e)

{

Environment.Exit(0);

}

����'�����.��"C��������68��*+�����"���������'���0������ �"�����A.����� ������� "�� 0���"������('���������.����������� �������!������ ������ ��8�����"���.��������������9�������!����'� �������

Page 284: 3Programmer's Heaven C# School Book

��������������� ���������

1/2

���� � ���� ��$�� ��.�3�� ���5 �6��&������.�����M�(�3 �������)������"�� ;<�

��������"6 ���� �� ��3��&��"������*+�� 0���"�����������.����.����!������'� "�'�� "��"!��"��� �������� ������ ������ �������� �.��"����������"�������!������������6 ���� �� ���.�����&��"��� �������� ������!������������ ���0���!���� �������� ���� ,��0�� �����.��� �������&��� ���.������!���!��"��'����.���������������� �����"������ �������&��� �������� &���!��"���!�������&������(��.��������� ���!��"����.��� �� ����"�(��.�!����&������� ���&�������!��"��������!��������� (������� ���������!��"��������������!�������� �� ���,������� �!��"���������� ���!��"����.��� �� ������������� ��!�����,� ��)��� � ���)��� �!��"����� "�� ����� "��!��"��������,� � ���������� �!��"���������. &��"���!���������.�� �� ��������]]��������� ������� ��������!��"���!�� "�'!��"���%..������� ���� ���%..������� ����!��"�������� %..������� ���� �������� %..������� ����!��"��#����%..������� ���� ���#����%..������� ����!��"�����(�� ���!��"����"���������(������ �#��K������� ���.��.��������� "���� "�����!� ���� ���������� ��"�!����.������.��������� ���"����������0���� �� ����0� 3�Z����(�����"��� private void btnGo_Click(object sender, System.EventArgs e)

{

Page 285: 3Programmer's Heaven C# School Book

��������������� ���������

1/

OperatingSystem os = Environment.OSVersion;

PlatformID OSid = os.Platform;

string[] drives = Environment.GetLogicalDrives();

string drivesString = "";

foreach(string drive in drives)

{

drivesString += drive + ", ";

}

drivesString = drivesString.TrimEnd(' ', ',');

lbx.Items.Add("Machine Name: \t" + Environment.MachineName);

lbx.Items.Add("Operating System: \t" + Environment.OSVersion);

lbx.Items.Add("Operating System ID:\t" + OSid);

lbx.Items.Add("Current Folder: \t" + Environment.CurrentDirectory);

lbx.Items.Add("CLR Version: \t" + Environment.Version);

lbx.Items.Add("Present Drives: \t" + drivesString);

lbx.Items.Add("Program Files: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));

lbx.Items.Add("Common Program Files:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));

lbx.Items.Add("Windows Desktop: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

lbx.Items.Add("Favorites: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

lbx.Items.Add("History: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.History));

lbx.Items.Add("Personal (My Documents:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Personal));

lbx.Items.Add("Start Menu's Program:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.Programs));

lbx.Items.Add("Recent: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.Recent));

lbx.Items.Add("Send To: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.SendTo));

lbx.Items.Add("Start Menu: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));

lbx.Items.Add("Startup: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.Startup));

lbx.Items.Add("Windows System: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.System));

lbx.Items.Add("Application Data: \t" +

Page 286: 3Programmer's Heaven C# School Book

��������������� ���������

1/1

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

lbx.Items.Add("Common Application:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

lbx.Items.Add("Local Application Data:\t" +

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));

lbx.Items.Add("Cookies: \t" +

Environment.GetFolderPath(Environment.SpecialFolder.Cookies));

}

��� ��8�����"���.������'�������0������ ��� ������9�������!����'� �������

/�� $���� ���� ������ ���&#��������� ������&#��������� ����.���������

���� �� �.�����!���� ".��!���"�!!��� ��.������ � ������ �����������A�&���� "�������A�&���� !����������������A�&�������8.������������"��.��!����������.������ � !����A ���������� "9����07����!��.��������A�&���� !������.��� ��� ���!����������'����'��� ���I��"�!!��� �.��.������!�!����#���.������������ �0�� ��

&#��������� ���������

%�����'�!����������"�!���&�������.��� ��"� ���!����'� ���0��� ,��0�� �����.��� ��.�*+ ��.�����.���!��"!��������.���!��"������.����������*+ ���������.���!��"!����������*+ ���������.���!��"!����

Page 287: 3Programmer's Heaven C# School Book

��������������� ���������

1/;

68��*+ )���� @����� ������ "����� �'���������.���!��"!����8���3��%����0���*+ )���� � �07����!��.��������A�&���%����0���'������ ��� "�!!��� �� !�������

�����"� �!�����(�'�����������""� �� ���3��������� ����*+ )���� � �07����!��.���������������.��� ����"��������!���������� �!���

!����3��#��%�������*+ )���� � �07����!��.���������������.��� ����"��������!���������������

!����3��#�����������*+ )���� � �07����!��.���������������.��� ����"��������!������'��������� ��

���!����,���*+ ,������.���!��"!��������.���!��".����A.� *+ A.� ���.���!��"!���� "����� ����������A�&����������07���!�����!����A.� )��"*+ A.� ���.���!��"!���!�����"� �.��.��� "����� ����"� ���������A�&���������

�07���!�����!����A.� �����*+ A.� ���.���!��"!���!�����"� �.��.��� "����� ����"I'����

�������A�&����������07���!�����!�������%����0���*+ %���.�� �07����!��.��������A�&���%����0���'������ ��� "�!!��� �� !�������

�����"� �!���� "�����������0��������!����,���!����0��������"��������������!��'��"� "���"�!!���������'����'��(� �� ���.���..������� � "�����.�����'�'����� �"������!����� "���"�������"��� �������''��� ������� ����..������� �

����� �����. ����� ���������;<���� �

��..��'�'�����������!��� ���"S�E���.��8�T� �������!��"���!�"��������� '�������!����'� ������� ���"����� File.Create("C:\\c-sharp.txt");

����������� �'��������������������������������������� ������������������������������������������ ���� �����

6�������0��������������������������������������������%�� �

using System.IO;

Page 288: 3Programmer's Heaven C# School Book

��������������� ���������

1/B

�$# ������/3 �����. ����� ����$#;<����/3�;<���� ��

$�'�!'�'� �����.����!�������a��.������!��"��9'��� �����!����'� ������� �� File.Copy("C:\\c-sharp.txt", "c:\\my programs\\c-sharp.txt");

�������������� �����,���*+�����"�������!����%������� ������������"�"!����!�����.�*+� "������*+�����"������(��@����� �������� "�����'���������'��������'�������!����!���!���'��������� ����8��� ���������.����6���9 File.Copy("C:\\c-sharp.txt", "c:\\my programs\\c-sharp.txt", true);

� ��: ���� ���0 �������.�� ��. ����� ���(0 ���;<���� �

��������"�� 0���"������(����8��� ���!���!��� if(!File.Exists("C:\\c-sharp.txt"))

{

File.Create("C:\\c-sharp.txt");

}

)��� ���-��� ������.���. ����� ���)��-��� �����;<���� �

���� ����(��������0����!�!����� ����3��%����0���*+�����" FileAttributes attrs = File.GetAttributes("c:\\c-sharp.txt");

lbx.Items.Add("File 'c:\\c-sharp.txt'");

lbx.Items.Add(attrs.ToString());

��� ��8�����"���.������9�������� !������� ��������� �������!������������������ ����������0����!���!���0��� ����&���%����0���� ��������

Page 289: 3Programmer's Heaven C# School Book

��������������� ���������

1/=

&#��������� ����.�������

����������A�&���� !����������"��.��!���"�!!��� ��.������ � !����< ��(����&������9'� ��"��������� �07����!���&���� !�����������������%�����'�!���������.���� ������"� ".��.������!���&���� !�����.��� ��"� ���!����'� ���0��� ,��0�� �����.��� ������� ���� 3��������������!������� �!���!������������� )���� ����������� !��07���������.��� ����.��� �"��������*!��"��+�!���!�������������$��� )���� ��� ����!���.��� �"��������*� ��� �+�!���!����68�� )���� @����� ������ "����� �'���������.���!��"!����8���68�� �� )���� ����8�� �� *��.�+�!���!���*����9��8�9��9��.8+�&���$��� )���� ���!���.���� " ����!���!���*����9��a�E����.��8�+�#��%������� )���� � �07����!��.���������������.��� ����"��������!���������������

!����#����������� )���� � �07����!��.���������������.��� ����"��������!������'��������� ��

���!���#� ��� )���� ����C�* ��0���!0���+� �!����$��� )���� ��� ����!���!���*����9�E����.��8�+���.���*+ ��.�����!��������.���!��"������.����������*+ ���������!����������*+ ���������!����,�����*+ ,������!����A.� *+ A.� ���!���'������������"I'����� "���� �.���������A.� )��"*+ A.� ���!���!�����"� �.��.��� "����� ����"� ���������A�&����������07���

!�����!����A.� �����*+ A.� ���!���!�����"� �.��.��� "����� ����"I'�����������A�&����������07���

!�����!����A.� ��8�*+ A.� ���!���� "����� ��������A�������)��"���07���'���<�&�� ��"� �����

���"!���� �8��� ���8�!����

-�K� �:����� �$����0��$���

%�������������������0���.��.������ "�����"���� "���� "�0��7��0����"� ������ ���P'����� ��"�����������.���8��.����"��� ��������!� ���� ������!���&���� !������ ���!����'� ��8��.��9'�'�����.��.��!���"�!!��� ��.������ � �!���� ""�.������������ ����0�8 ���"g�08K ������������������� �������%���������� �������������������������������������������������������������������� �

����������������������A����������������������� ���� ������<BBC��� ��� �D����� �����������������������������D��

=������������������.�������D�������������"����������:���+,������������������������������������82������������� �

�$�������������������� �

Page 290: 3Programmer's Heaven C# School Book

��������������� ���������

1/?

������'����� ���!����'� ���"�� ���g3�K0���� K��� ��� "���� private void btnGo_Click(object sender, System.EventArgs e)

{

FileInfo file = new FileInfo("c:\\c-sharp.txt");

lbx.Items.Add("File Name: " + file.Name);

lbx.Items.Add("File Extention: " + file.Extension);

lbx.Items.Add("File's Full Name: " + file.FullName);

lbx.Items.Add("Parent Directory: " + file.DirectoryName);

lbx.Items.Add("File Size: " + file.Length.ToString() + " bytes");

lbx.Items.Add("File Attributes: " + file.Attributes.ToString());

}

����'�������.����"���.��.������!���&���� !��������������� ".�� ����� !������� �0����!������� ��8�����"���.������� ������9�������!����'� ����.���

$����@�!����8����� ����.������9���� ��"��������0����!!���g��a�E����.��8�K��)��"� ��9��""� � "%�������� ��� "�'68.������

/�� $���� ���2 ����� ���;.����<��� ���&#��������2 �����#����&#��������2 �����#��.���������

������������&���� "&���� !�����'��� .��!����������.������ � "����������� �������������� "���������� !������%��� ���'����E ��� ���������������A��������������� ��� ����������"'��������������A����������� !������ ��� � �� �����0����.��!�����������(� "����������

Page 291: 3Programmer's Heaven C# School Book

��������������� ���������

1/�

&#��������2 �����#�������

%�����'�!����������"�!����������������.��� ��"� ���!����'� ���0��� ,��0�� �����.��� ���������������*+ ���������.���!��""���������������*+ ���������.���!��""���������68��*+ )���� @����� ������ "����� �'���������.���!��""���������8���3��������� ����*+ )���� � �07����!��.���������������.��� ����"��������!���������� �!���

.���!��""���������3������������*+ )���� � ������!��� ��� ��� � ���� ����!�������0E"����������!���

.���!��""���������3��&���*+ )���� � ������!��� ��� ��� � ���� ����!������!����� ��� �"� ���.���!��"

"���������3��&��������6 ����*+ )���� � ������!��� ��� ��� � ���� ����!������!���� ""����������� ��� �"

� ���.���!��""���������3������ �*+ )���� � �07����!��.����������� !���.��� �� ����.��� �"���������!���.���!��"

"���������,���*+ ,������.���!��""��������� "������� �� �*!���� ""���������+�����.���!��"

.����

����� ��'����� ������� ��: ���.��� ���0 �������.� ����� ���

������"�����"��� �������'��.��!�������0����.������ ���' 0���'� private void btnGo_Click(object sender, System.EventArgs e)

{

lbx.Items.Add("Directory 'C:\\Faraz' exists: " + Directory.Exists("C:\\Faraz"));

lbx.Items.Add("Creating Directory 'C:\\Faraz': " + Directory.CreateDirectory("C:\\Faraz"));

lbx.Items.Add("Directory 'C:\\Faraz' exists: " + Directory.Exists("C:\\Faraz"));

lbx.Items.Add("Parent Directory of 'Faraz' is: " + Directory.GetParent("C:\\Faraz"));

lbx.Items.Add("Deleting Directory 'C:\\Faraz'... ");

Directory.Delete("C:\\Faraz", true);

lbx.Items.Add("Directory 'C:\\Faraz' exists: " + Directory.Exists("C:\\Faraz"));

}

%��� 9�����"���.�������������������������"�!�����������������.��!��������.������ �A ���� ��� ������������'�����.��"���.���E��� �� "�������������������������������*+�����"���� �������������������� ".����������������� ����� ���� �� �*�#)+�����������"���������������������� ��� ��"��������!���� ���"��������0�����"��������!���� ""����������� ��� �"� ���0E"���������� "�� �

Page 292: 3Programmer's Heaven C# School Book

��������������� ���������

1/�

)��� ���� ����������;. ����������F ����� ��<�.��� �����#�

����������������8.������������"��������������� �� ��!�"�������������������3������������*+����� �����!�������0E"����������!���.���!��""��������9����������3��&���*+����� �����!������!���� ���.���!��""��������� "����������3��&��������6 ����*+����� �����!������!���� "�0E"����������� ��� �"� ���.���!��""���������#��K��������!����� �� ��!����� "�'!��"���!��������� private void btnGo_Click(object sender, System.EventArgs e)

{

// get the path of Windows Folder's System Folder

string winFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);

// Separate the Windows Folder

winFolder = winFolder.Substring(0, winFolder.LastIndexOf('\\'));

string[] fileSystemEntries = Directory.GetFileSystemEntries(winFolder);

string[] files = Directory.GetFiles(winFolder);

string[] directories = Directory.GetDirectories(winFolder);

// show windows folder path

lbx.Items.Add("Address of Windows Folder: " + winFolder);

// show files/folder in windows folder

lbx.Items.Add("");

lbx.Items.Add("File System Entries (files/folder) in the Windows Folder: ");

foreach(string fileSystemEntry in fileSystemEntries)

{

lbx.Items.Add("\t" + fileSystemEntry);

}

// show files in windows folder

lbx.Items.Add("");

lbx.Items.Add("Files in the Windows Folder: ");

foreach(string file in files)

{

lbx.Items.Add("\t" + file);

}

// show folder in windows folder

lbx.Items.Add("");

lbx.Items.Add("Directories in the Windows Folder: ");

foreach(string directory in directories)

Page 293: 3Programmer's Heaven C# School Book

��������������� ���������

1//

{

lbx.Items.Add("\t" + directory);

}

}

% "'�� ��8�����"����0���.������� ������9�������!����'� �������

&#��������2 �����#��.�������

����������A����������� !����������"��.��!���"�!!��� ��.������ � "����������< ��(����������������9'� ��"��������� �07����!������������� !�����������������%�����'�!����!�����.���� ������"� ".��.������!������������� !�����.��� ��"� ���!����'� ���0��� ,��0�� �����.��� 68�� )���� �@����� ������ "����� �'���������.���!��""���������8���68�� ��� )���� ����8�� ��� *��.�+�!���"���������&���$��� )���� ���!���.���� " ����!���"��������*����9��a&���C+�$��� )���� ��� ����!���"��������*����9&���C+����� � )���� ���!���.���� " ����!���.��� �"���������!���"���������������*+ �������"��������'������.���!��" ����

Page 294: 3Programmer's Heaven C# School Book

��������������� ���������

;22

������*+ ���������"��������'������.���!��" ����3������������*+ )���� � ������!��.����������� !�������.��� ��������0E"����������!���

"���������3��&���*+ )���� � ������!��.�&���� !�������.��� �������!����� ��� �"� ���"���������3��&��������� !�*+ )���� � ������!��.�&��������� !�������.��� �������!���� "!��"��

�� ��� �"� ���"���������,�����*+ ,������"��������� "������� �� �*!���� ""���������+�����.���!��".����)�!���*+ )�!�������� �� ���!���������� !��

2�������� ���$$� ��� ��.��� ��2 �����#��.�������

����'�������� ��"���.�������8��.��������� �'��������������� !����� ���"�!����������������������.���!���.������'�������� ������������!���.������� �������"�!��"��"��� private void btnGo_Click(object sender, System.EventArgs e)

{

// get the path of Windows Folder's System Folder

string winFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);

// Separate the Windows Folder

winFolder = winFolder.Substring(0, winFolder.LastIndexOf('\\'));

DirectoryInfo winFolderObj = new DirectoryInfo(winFolder);

FileSystemInfo[] fileSystemInfos = winFolderObj.GetFileSystemInfos();

FileInfo[] fileInfos = winFolderObj.GetFiles();

DirectoryInfo[] directoryInfos = winFolderObj.GetDirectories();

// show windows folder path

lbx.Items.Add("Address of Windows Folder: " + winFolderObj.FullName);

// show files/folder in windows folder

lbx.Items.Add("");

lbx.Items.Add("File System Entries (files/folder) in the Windows Folder: ");

foreach(FileSystemInfo fileSystemInfo in fileSystemInfos)

{

lbx.Items.Add("\t" + fileSystemInfo.FullName);

}

// show files in windows folder

lbx.Items.Add("");

lbx.Items.Add("Files in the Windows Folder: ");

foreach(FileInfo fileInfo in fileInfos)

{

Page 295: 3Programmer's Heaven C# School Book

��������������� ���������

;2

lbx.Items.Add("\t" + fileInfo.FullName);

}

// show folder in windows folder

lbx.Items.Add("");

lbx.Items.Add("Directories in the Windows Folder: ");

foreach(DirectoryInfo directoryInfo in directoryInfos)

{

lbx.Items.Add("\t" + directoryInfo.FullName);

}

}

%������������ �����"9���� ��"�!!��� ��0��'�� ���� "���.��������"����������'������ �������������� !��07���K�����"� ".��.������ ���"�!���������������G����������"�%��� ���������!���.������'���0����������!���.������� �9.�� �� ���� ����!!���I!��"��� ����� "�'!��"��� �� ����������� ��'���� "�������!��'��"�����.��!���"�!!��� ��.������ � !���� "!��"��� "������ !������� �0���!�������� "����..������� K� ���� �� �� �$���A ����'��"�!������ �����������0���������!��'�� �� �.����� �!���� ""���������� �����..������� �� ����8��.��� ������ 9'����� K������.��"�������� ��8��.��� ���������0�����' 0������!����0� ���!.���!��"!���� "���.���������!"�!!��� �!���������'��0�����.���������'�������!���������"�� �����������0���(�

&�������

� �.�������� ��� ��8�9������"�!� ���� �!������8���������g%������� �07�����"�����"� "'����"������ "!������"��������*����9!���������9 ��'��(9"�����9���+K�������g%������� �0������� �!��J�� ���!0���9��(��!���K��.���������������"������ ������ ��'�� "P� ���������"�����"��������'��������������������"��������"����'������!���"�������"���������� 0��!���9"���� ��������"���� � �������� ����&���������9,������������ "$��'��(�����������"I'����"������ "!�������"����������� ��9���0���!� ���� ������!� �������������'"�����0����"!���� "'����� �����"���������

� ������/5� �8�9��+�:8�-���.��8��;4�

6�����

4.�7��) '���)�/<� �;9��+�:�;6������'���)2

�����������

�����������

Page 296: 3Programmer's Heaven C# School Book

��������������� ���������

;21

-��3��3 �6�.�� �� ..�������#$���.���������

���������� �"�!!��� �"��������������� 0����"!���!�������"� �"�(9� ���������9� ��������!������������IA"������%"�����.��'� ��"���.������ � ���!���������"���!��������� �"���������� ���.�������"��(�)��"*+� "�����*+� ��"� K�'� ������ �������'�������"�����9������'�"��������������"� "��'"������������"� "���"�����"��������9� "� '����!������������.����"��8���������!����������$��*�$��+.����"�"�!!��� ������������"�!!��� ���.��!���������0������!������������ ����$��!����'��(��������A���������!���'� �������"���� �!������������������A�&���������9�!���'� �������"���� ������9���������������A�,�����������9� "�!���'� ����� ��������������9��������������$������(���$��'��(������� ���0����� ��0������������������������� �$����������"� K� ��"��'�����0�����'"���������������"!���������!�������9 ��'��(��(����������P������ ��"��"����� �� ��������������07���0�"�!� � ����"������������ ������ "��� ���������.��)��"*+� "�����*+�����"� ������0�� �� ��� ���������#� �*+� "�� ����)��"#� �*+�����"�����!������������ !���9����������� ��������.��� ����� .��9���.��� "����������������� ���'� "�'�@������ ���������*+�����"� ����� ��������9'��� � "���"���*0���+������� ���'� "�'�

8 ��&#�����&������������M�� �������.������������� ��� ���*���.����6�:�

����������������� �0��������'������������������ ����$��!����'��(� ���������8.�����.��.������ "�����"������""� 0������ ��������������*��(�&���������9$��'��(������9���+� %�����'�!����!������� ������ �.��.������ "�����"�.����"�"� ���!����'� ���0��� ,��0�� �����.��� �� )��" )���� �@����� ������ "����� �'���������������� ���"!������"����������� ����� )���� �@����� ������ "����� �'���������������� '���������"���������#� ��� )���� ����� ����� ��0���!0���� �������� ������������� 3��I����������� �.����� �!���������% ����"I'�����.������ � ���������

������"������������� �.����� �����*+ �������������&���*+ �����������"�������"� ��������0�!!�������"������������(*+ ����������� �.����� �!���������)��"*0���MN0�!!��9� ��!!��9� ���� �+*)���� ��.��� �+

)��"���.���!��" ��0���!0���!����������� �.����� �!��������� ������..���"������!0���� "����� ��� ��0���!0��������������"!������������

)��"@���*+ )��"�� ���0���!����������� �.����� �!��������� "����� ���0�������"� ��� � �����gE K����� ������ "��������� "�!���������!"����

Page 297: 3Programmer's Heaven C# School Book

��������������� ���������

;2;

�����*0���MN0�!!��9� ��!!��9� ���� �+*)���� ��.�����"+

��������.���!��" ��0���!0������������� �.����� �!��������!�������..���"������!0����

�����@���*+ ������� ���0������������� �.����� �!���������

2 ..�������#$���.�. �����������M�9�� ������5� � �����. ����

�����7����.���!�������� ��0���!��������9'���������"�����"!���� "'������!����������������������� ����$��!����'��(������ 0���"�����"� "'����!����:���� ��.�����������A�&��������������"I'����0��������!����%���� �������������������������A�@� ���)��"���� ��������A�@� ������������������"0� ���"����.��������"�����.��% "����� ���������������A�������)��"��� "�������A����������������������"I'������8�!������'���"��� ����������!����� �0�� ��

B� ���&#��������� ��&���������������6� ���������. ����

����������A�&������������� ���������������A������������.����"����������!� ���� ����������"� "'����"�����!��������.���� �����!����0��������0���!��������������'��('���!����@�!����� �� ��!���������.������ 9'� ��"��������������A ���.���� ���.��7��� using System;

using System.IO;

���� � �� ��������&������������� �� �"�!!��� �'����������� ��!���&����A.� *+9&����A.� )��"*+� "&����A.� �����*+�����"�!����������A�&������� FileStream fs = File.Open("C:\\C-sharp.txt", FileMode.Open);

:�������������&���� !����KA.� *+9A.� )��"*+� "A.� �����*+�����"� FileInfo objFileInfo = new FileInfo("C:\\C-sharp.txt");

FileStream fs = objFileInfo.Open(FileMode.Open);

&� ������������� ��!��� ��0���!�������"�"�� ��������!���&���������������� ������ �� �!��������9'����"�!� �!���.���������

-���� �����$������ ���� ��$�� ���������.�� ��. ��� �

%&���,�"�� �������� � �� ������"�!� ���'����.����� ���������"�.� ���!�������.��0�������� ���"�A.� 9A.� A�������9%..� "9������� "�������!&���,�"��A.� 9��'��������.����.� ����8��� �!�����!&���,�"��A.� ��������'��������.����.� ����8��� �!���P�! �!����8��9��'������������ �'� ��� %..� "!�����"�9��� �'"���'���0�'����� ������ "�!����8��� �!����

Page 298: 3Programmer's Heaven C# School Book

��������������� ���������

;2B

%&���%���� �������� � �� ��"�!� �'�����.������ �������'�""��� ����!�����������.��0�������� ���"�)��"9������ ")��"�������!&���%����)��"9����� � �����"!������!��������� %&��������� �������� � �� ������"�!� �������� ��.��� !�����!�������.��0�������� ���"�$� �9)��"9)��"�����9�������!&���������$� �9 ������������� �.� ���!���� ����������������������.� ��!&���������)��"9������������ �.� � "� �����"!������!����

�$�� ��������� ���.�����. ���

$�''�G���������"���.�!����������!!��������9���G"������� �.���������� .�������9�� "�� ��!��������������� ���� ����� ���#��K�������'� "�'�..������� �������"�!���� �����8�0�89����'����������� �������8�� "�����0��(�����!��������..������� '���!� �������(��(�� �

�����"�0��� "���������.��������� ��� "���!�����)��"&���0���� *0� 3�+�� private void btnGo_Click(object sender, System.EventArgs e)

{

string fileName = "C:\\C-sharp.txt";

lblFileName.Text = "File Name: " + fileName;

Page 299: 3Programmer's Heaven C# School Book

��������������� ���������

;2=

// Open existing file in Read only mode without allowing any sharing

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);

// create an array of bytes of the size of file\

// and read file contents to it.

byte[] byteText = new byte[fs.Length];

fs.Read(byteText, 0, byteText.Length);

// convert bytes array to string and display in the text box

txtFileText.Text = System.Text.Encoding.ASCII.GetString(byteText);

// close the file stream so that other streams may use it

fs.Close();

}

����9'�������"���!�����������.� ����8��� �!���*&���,�"��A.� +� ���"� ����"�*&���%����)��"+� "'����������'� �� �����������������'��������..������� ��� ����!���*&���������$� �+������ ������"� ������!0���'����� ����J���������� ����!���!���� "���"���!���� ����� fs.Read(byteText, 0, byteText.Length);

����'�����.���!��"����'�'� ������"���!����� �� �� �����0�����8������9�������!������������"����!���� ���������!������g2K� "�8� "����������"���"0�����8��#� ���*�C��!!���+0���!������!����%!������"� ������ �� �9'� ��"���� �������0�������������� ���������� 0�"�.����"� �����8�0�8�&� ����9'���������"��������*� "������!���+������������������������� ���������9������ ��� "���!������&���0���� *0� ����&���+�� private void btnSaveFile_Click(object sender, System.EventArgs e)

{

string fileName = "C:\\C-sharp.txt";

// Open existing file in Read only mode without allowing any sharing

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None);

// covert the text (string) in the text box to bytes array

// and make the byteText reference to point to that byte array

byte[] byteText = System.Text.Encoding.ASCII.GetBytes(txtFileText.Text);

// write the byte array to file from the start to end

fs.Write(byteText, 0, byteText.Length);

Page 300: 3Programmer's Heaven C# School Book

��������������� ���������

;2?

// close the file stream so that other streams may use it

fs.Close();

}

����'�����!����� �����"�����8�� �����8�0�8���0��������� "��� '����� �������!����� �&������������K�����*+�����"� fs.Write(byteText, 0, byteText.Length);

� ����0����� �'��������"���&��������������'�������0���� ���0�����8������������������"!�����������(�"��������'���� �!������!���0����!��������� "'����������.��������������!����

B� ���7 ���#9��������7 ���#5� �������������6� ���$� � � 3�����. ����

���.��0���'����� ����&�����������������'��� � �����"� "'����0��������!�������������8.��������� �����������.��!"���*� �9"��0��90���9��� �+��0���0�!���'���� ������!���*� "��������!�����"� �+�������$��*�$��+!����'��(�����0����.����"��'�������������'������"� "'����.��������"�����.����!�������� ���������A�@� ���)��"�������".��������"�����.�!����!���� "�������A�@� �����������'����.�������������!����% ��.���� �.�� ��0������@� ���)��"��� "@� ���������������������� ��"��������0�.��"� ������� �������������������������������"� "'����.��������� #��K������� �..������� ����'����"�!!��� �.�����������!���� "��� ���"����0��(� ����..������� '���!� �������(��(�����

Page 301: 3Programmer's Heaven C# School Book

��������������� ���������

;2�

�����8�0�8� ����..������� ����"� ���%�!���9���)��"&���0���� ����"��0��"� "������ ��"��������������&���0���� ��������!���!���9� "��� ���"���!���0��(�������8�0�8������ �� �'����� �����!���������"��"�"� �������&���0���� K����(��� ��� "���'������ private void btnSaveFile_Click(object sender, System.EventArgs e)

{

string fileName = "C:\\C-sharp.txt";

// Open existing file in Read only mode without allowing any sharing

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None);

// Open the Writer over this file stream

BinaryWriter writer = new BinaryWriter(fs);

// write different types of primitives to the file

writer.Write("I am Fraz\r\n");

writer.Write("Age: ");

writer.Write(23);

writer.Write("\r\nWeight: ");

writer.Write(45.5);

// close the file stream so that other streams may use it

writer.Close();

fs.Close();

btnLoadFile.Enabled = true;

}

����'�����!���������"���!��������� "��"����� �� ��������@� ���������������������� '����� "�!!��� �.�������������������� �@� ���������K�����*+�����"9'�������� ��������"�"����� ��'����"�!!��� ���.��!.���������&� ����'���������"����'������� "� �0��"���#��"&���0���� � ������ ��� "���!�����#��"&���0���� �� private void btnGo_Click(object sender, System.EventArgs e)

{

string fileName = "C:\\C-sharp.txt";

lblFileName.Text = "File Name: " + fileName;

// Open existing file in Read only mode without allowing any sharing

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);

Page 302: 3Programmer's Heaven C# School Book

��������������� ���������

;2�

// Open the Reader over this file stream

BinaryReader reader = new BinaryReader(fs);

// read different types of primitives to the file

string name = reader.ReadString();

string ageString = reader.ReadString();

int age = reader.ReadInt32();

string wtString = reader.ReadString();

double weight = reader.ReadDouble();

// concatenate primitives into single string and display in the text box

txtFileText.Text = name + ageString + age.ToString() + wtString + weight.ToString();

// close the file stream so that other streams may use it

reader.Close();

fs.Close();

}

����'����������@� ���)��"�����K�07����� ����&����������07��������� ���"���.��������.��������'����� �����!����%!������"� �������"���9'��� ���� ������.������������ ������ �� ""�.������ �����8�0�8�&� ����'���������'������������.���� �.�� ��� ���������������.��������������"� ��������"������'���'����� �

B� ���&�����9��������&�����5� �������������6� �����0��. ����

�������������)��"��� "�����������������"�����"� "'������8�!�����������������!�������"��(�)��"#� �*+� ")��"��6 "*+��!���������������"� �� "'���� ��!��8�!����,������ ����9����������� ��"�!!��� ���8�� ��"� �*��(�%����� "< ���"�+!�����"� �� "'���� ����!���� ����������� �>������������������ ��������������D�������������� ������������������������������������������'������������

��������������������������������������������������������� �������������������������� �������������� E������ �������E��������� �

'�������������������������� ������������� ���� ������������ �������������������������������������� ���

������������������������� ������ �������������������>��������������������� �������������� ��� �������� ����������

������������68���

A ������ 9�'�������� ��������������.��'������.�������7��0����������(��.��� "����:������".� "�������.���� �'��������������!��0������ "���� "� ��

Page 303: 3Programmer's Heaven C# School Book

��������������� ���������

;2/

&�� �� E�� �����2�F��� �� E�� ��

�������C���� ����.�����!'���� ��07�������������'������E������C���� ����.�����!���"� ��07���!������������<.� ��� �'9'������� ��'�����"I'����.����������.�����������0��'����� K����"I'����� � ��8.�����*���"�!� �"+��.�������������������������� .�� ��������0������0�!�������������.���� �� ����������C���� � ���.��.���!������C� ���'���� �� �07��������������������������������!� �07����"������ �"0���� �� �������0����� ��������C� �� �07������ '���� �����!�����0��*��� �� ��+�����0��*��������"� �07������.�+�����������,����"����������0����� ��������C�"��'����� ����������� :���� ������C�� �07���������!0���.��'���� �����!�����0�������0���������������� "�E������C� �9���'���"���������"���������0�������0��� �������J�� ��� '��������'���'����� ���'�������.�����!������C� �� ""�E������C� ����'���7��"��"�� �����

�� ������"���7�0��'�������������0�������0��������!� "�������0������������!��������� ��� �����!�����0��� "�!��������� ��� ��������"�!� �"�07����

�� ��� ������� "��".����"�������.��� '���'���� ���"�E������C�����07������������C�".��������9'���"������0��'����!����J�� ��� '�������'����������0�������0��� "���!����'�����J�� ���

������*�$��+!����'��(��(������!�������� ".����"�0� ���� "�A%�!���������� �'��������� ������C������07���7��0������ �������������C�*+� "��������C�*+�����"� ������������������������ ����"'���������C���� �:������� ��'� ������� ������0�������C�"�������������������� ��'� ���������C�����!�������0�������0�������������&���8��.��9�'�0E�..������� ��� ������'���<��� !��07�����0�������C�"������'��"!���"��0�������C�"� %���������� �$������ E������C�0��0�"�!����E����������� ��0�'����� ���������:���������8.����������(���������0�������C�0���� ����M�������C�0��N�����0���� :���� �.��� �������(�.������������0�������0��*��!���"+�$� E�������C�"0��� ����M$� �������C�"N�����0�����.���� �����#)!���'���� �����!���"'�� ������C� �����07���� ����������� ��������D������� �����������������"#�6������������������������������������������������������������"#������

�������������� ���$�����������������������������������+��%�������������������������� ��������������� ����� �,��'���

��������������������������������������� ��5������������������������������������������� ��������������������������������

��� ��68���������������� �

��$������ ���&�� �� E�� �����2���� �� E�� ��M�-�� �$����0��$���

#��K��������� ����..������� ������ ��� �������C�0����������..������� '���������C����07������!���� "��� "�������C������� !������!������ ������07������'��������.������������������������!�'�� ����������0���������.�����������"��!���.��������

Page 304: 3Programmer's Heaven C# School Book

��������������� ���������

; 2

using System;

using System.IO; // for FileStream

using System.Runtime.Serialization.Formatters.Binary; // for BinaryFormatter

namespace Compiler

{

class Test

{

static void Main()

{

Addition addition = new Addition(3, 4);

FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);

BinaryFormatter binForm = new BinaryFormatter();

Console.WriteLine("Serializing the object....");

binForm.Serialize(fs, addition);

fs.Position = 0; // move to the start of file

Console.WriteLine("DeSerializing the object....");

Addition sum = (Addition) binForm.Deserialize(fs);

int res = sum.Add();

Console.WriteLine("The sum of 3 and 4 is: {0}", res);

}

}

[Serializable]

class Addition

{

private int num1;

private int num2;

private int result;

public Addition()

{

}

public Addition(int num1, int num2)

{

Page 305: 3Programmer's Heaven C# School Book

��������������� ���������

;

this.num1 = num1;

this.num2 = num2;

}

public int Add()

{

result = num1 + num2;

return result;

}

public int Result

{

get { return result; }

}

}

}

���%""���� ����������.��� "�������.���������0���$�������'��������(�"������'������M�������C�0��N�����0����%�� �������'������ ���"�"�..��.����� ���.���� using System;

Uusing System.IO; // for FileStream

using System.Runtime.Serialization.Formatters.Binary; // for BinaryFormatter

� ���,�� *+�����"'�����������"� � �� ���!���%""���� ������������� ������"�!��������� "������C�"����07��������!����� ����@� ���&�����������*��'�����������6@� ���&������������� ������ +� FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);

BinaryFormatter binForm = new BinaryFormatter();

Console.WriteLine("Serializing the object....");

binForm.Serialize(fs, addition);

�������'� ��"��"�� ���.���'�� ������C� �� �07������������C� ������ ������0��0�!���"������C� �'� ��"�������!���.�� ���.����� ����������!���!��� fs.Position = 0; // move to the start of file

$�''��� "�������C�����07���!������������� �������@� ���&��������� �� ���

Page 306: 3Programmer's Heaven C# School Book

��������������� ���������

; 1

Console.WriteLine("DeSerializing the object....");

Addition sum = (Addition) binForm.Deserialize(fs);

���@� ���&�����������K��������C�*+�����"��(����!�����������.��������9���"����07������.�!������ "����� � �07����!��.�������A07�������������8.�����������������"����"����A ��'������������07���9'��������%""*+�����"�!������'���������.���������0���!�����������.��������� ".�� ���������� ����� ��� int res = sum.Add();

Console.WriteLine("The sum of 3 and 4 is: {0}", res);

��� ������.���� "�8��������.������9���'��������!����'� ����.��� Serializing the object....

DeSerializing the object....

The sum of 3 and 4 is: 7

Press any key to continue

�!�������� �7�����M�������C�0��N�����0����������%""���� ���"�!� ���� 9���'������� �8��.��� '�� �������C�*+�����"�!���@� ���&��������������"������O // [Serializable]

class Addition

{

...

���������� ��&�� �� E�� ��

%!��������"����0����!������ '����� �07����������C�"����!������������"0��� "��"� "0������������C� �� ""�������C� �.����������������������.���0��!���������� �$��9�!�������� ��"����.���� ����������)� ������������C���� ��&��������� ���!��������'������ !��������������@� ���&��������*������)� ������������C���� �&���������@� ����@� ���&��������+� "����A%�&��������*������)� ������������C���� �&������������.����.&��������+�����A%�*���.��A07���%�����������+���� "��".��������������� ��� ��� "���������..����!,�����!�9�@,� "������ "������� �����@� ���&��������������.����C�"!�������������

"��3��� �������� �����������.���&�� �� E ���M�8 ��N*�&�� �� E�O����� �����

:���� .���� �����#)!���������C� ������� !���"'�� ������C� �� �07������������0�"�!!��� ����� !�������:�������"���"�� ��!���������.��.�����!���'� ������"�(.���0� ��'���� ������ �

Page 307: 3Programmer's Heaven C# School Book

��������������� ���������

; ;

�������� �!���"�&����������.�� ��"�����(���!���"'������M$� �������C�"N�����0����&���8��.��9������� ����������!���"��M$� �������C�"N�������.�����������"��!�����"�!��".�������� using System;

using System.IO; // for FileStream

using System.Runtime.Serialization.Formatters.Binary; // for BinaryFormatter

namespace Compiler

{

class Test

{

static void Main()

{

Addition addition = new Addition(3, 4);

addition.Add();

Console.WriteLine("The value of result is: {0}", addition.Result);

FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);

BinaryFormatter binForm = new BinaryFormatter();

Console.WriteLine("Serializing the object....");

binForm.Serialize(fs, addition);

fs.Position = 0; // move to the start of file

Console.WriteLine("DeSerializing the object....");

Addition sum = (Addition) binForm.Deserialize(fs);

Console.WriteLine("The value of result is: {0}", sum.Result);

Console.WriteLine("The sum of addition is: {0}", sum.Add());

}

}

[Serializable]

class Addition

{

private int num1;

private int num2;

[NonSerialized]

private int result;

Page 308: 3Programmer's Heaven C# School Book

��������������� ���������

; B

public Addition()

{

}

public Addition(int num1, int num2)

{

this.num1 = num1;

this.num2 = num2;

}

public int Add()

{

result = num1 + num2;

return result;

}

public int Result

{

get { return result; }

}

}

}

$������M$� �������C�"N�����0���������������!���"�$�'�� �"�����,�� *+�����"�!���.���������!���������� � �� ���!���%""���� ���'����'� ��0��9������%""*+�����"�����.������������ ".�� ���� Addition addition = new Addition(3, 4);

addition.Add();

Console.WriteLine("The value of result is: {0}", addition.Result);

����� ������C�����07���� ""�������C���0��(��� ������07���97����(�� ���.�������8��.��� FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);

BinaryFormatter binForm = new BinaryFormatter();

Console.WriteLine("Serializing the object....");

binForm.Serialize(fs, addition);

fs.Position = 0; // move to the start of file

Console.WriteLine("DeSerializing the object....");

Page 309: 3Programmer's Heaven C# School Book

��������������� ���������

; =

Addition sum = (Addition) binForm.Deserialize(fs);

$�'�� �"����� �8���.�&���'�.�� ����������!�������������0���� ����)����.��.����� Console.WriteLine("The value of result is: {0}", sum.Result);

�!���������!)����� ��������C�"9����0����� �����".�� �C����&� ����'�.�� ����������!����""���� Console.WriteLine("The sum of addition is: {0}", sum.Add());

����0����� �����".�� �������!;� "B�!��������0�� �� � " ��1'���������C�"���� '����.���� "�8��������.������9'�������!����'� ������� The value of result is: 7

Serializing the object....

DeSerializing the object....

The value of result is: 0

The sum of addition is: 7

Press any key to continue.

� ������.��9'��� ���������������!��������!���"�!���"�������C���� �C���9������ ��������!���"������ ��������C�"'�������07�������������!�����*;4B\�+�!�������� ����%""*+�����"������������!���" �� � " ��1"�"���������C�"'�������07������ ��9'��� .���� �����!���!���"!���������C� �'�������07�����

)��� ����� . ��6 ���2���� �� E ���F�� ���2���� �� E�� �����7��:� ����.����

��� '�"� K�'� �����!���!���"��������C�'�������07���9'������(���.��!������'��(� ����07���'�� "�������C� ������'����.��.��� � E������C�"!���"�&���8��.��9'������(������.����������������0���!���%""���� ���'�� "�������C� �����07����&�����9'� ��"����.���� �������������C���� ����@��(� ���!�������� ���!���� ���� ��� � ������" void OnDeserialization(object sender);

��������"���'�������"� �����.���� �� ����'�� ����07����"�������C�"�#��G��� �����.�������..������� ���������������0������� ���������� �!��� ��������C�"������"�!��"�������"��� using System;

using System.IO; // for FileStream

using System.Runtime.Serialization; // for IDeserializationCallBack

using System.Runtime.Serialization.Formatters.Binary; // for BinaryFormatter

namespace Compiler

Page 310: 3Programmer's Heaven C# School Book

��������������� ���������

; ?

{

class Test

{

static void Main()

{

Addition addition = new Addition(3, 4);

addition.Add();

Console.WriteLine("The value of result is: {0}", addition.Result);

FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Create);

BinaryFormatter binForm = new BinaryFormatter();

Console.WriteLine("Serializing the object....");

binForm.Serialize(fs, addition);

fs.Position = 0; // move to the start of file

Console.WriteLine("DeSerializing the object....");

Addition sum = (Addition) binForm.Deserialize(fs);

Console.WriteLine("The value of result is: {0}", sum.Result);

}

}

[Serializable]

class Addition : IDeserializationCallback

{

private int num1;

private int num2;

[NonSerialized]

private int result;

public Addition()

{

}

public Addition(int num1, int num2)

{

this.num1 = num1;

this.num2 = num2;

}

Page 311: 3Programmer's Heaven C# School Book

��������������� ���������

; �

public int Add()

{

result = num1 + num2;

return result;

}

public int Result

{

get { return result; }

}

public void OnDeserialization(object sender)

{

result = num1 + num2;

}

}

}

$��������������������%""���� � �����������������C���� ����0��(� ���!���� ".����"����"�!� ���� �!���A ��������C���� �����"� '���������.���������! �� � " ��1� "������� ��������!���"� public void OnDeserialization(object sender)

{

result = num1 + num2;

}

���,�� *+�����"����������������� �� ���.������.������0���������'�����" ������C��������� ��������!���"�!���"�������C���� �!���A ��������C���� �����"������"���� '����.���� "�8���������0���.������9'������!����'� ������� The value of result is: 7

Serializing the object....

DeSerializing the object....

The value of result is: 7

Press any key to continue

����������'�������A ��������C���� *+�����"��������������"'�� "�������C���� �.��!����"�'�"�" ��������C���������!��������!���"�!���"�������C���� �

Page 312: 3Programmer's Heaven C# School Book

��������������� ���������

; �

-�#�� �����9�� ������5� � ���6 � �&�������

<.� ��� �''������ ����"� ���� �����"� �� "'���� ���������$�''�'������� ���� �����"� �� "'���� ������������!����0����J����� �'����� ���� ��� "� ���� �����"I'������� Q-���� �"�����.������.����"����!���"� �� "'���� ����������������"���������)��"*+� "�����*+�����"�&���8��.��9'��������)��"*+�����"0�.���!�� �������� ��!"�����0����"������..���"������byte[] byteText = new byte[fs.Length];

fs.Read(byteText, 0, byteText.Length);

SomeOtherMethod();

��� '��������)��"*+�����"9���.������*���������� ������"+�0���(�"� ������"�����0�� ���"������..���"������ "����A����,����"*+�� �������"'�� ������.����"�����0�� ���"� ��������������������"�� ���� �����"9����'������������'���� ��������"�������"���������� ���..� '��������*+9� "���������"�� ���� ��'������ � �� ���� �����"� "'����'�7������������� "�����"��'���������������������A����������G@��� )��"*+� "@��� �����*+�����"�A ��'�����@��� )��"*+��@��� �����*+9�'���� ����������� ������

�� �������� ������"�����8����� ���������� �!����'� ����@��� )��"*+��@��� �����*+'������'���� �!��������"��'������0����.����"�

�� �������� #� �����)� ����*�#)+�������"� ���'���� ����"���� "� !������.������'�� ������.�����

�������( ���O@����'"������#)� !������.����������������"��'������0�� ���.����"Q������ ���� ���.������ �����'����.���� ��"� ���� �"�������90���6�� �9�����"��%� ���� ���IA������@��� )��"*+� "@��� �����*+�����"��(��"��������!��.�������%� �����0��(����"�������%� �����0��(�"�!� �"� �������� ���.����� public delegate void AsyncCallback(IAsyncResult ar)

������ �������"�������%� �����0��(�� ��!��� ��� ������"����������"����� ��.�� "��(��.���������!��.��������%� �)����������.��%� �)������ 0���"���..��� !������� �0�������� ���� ���.������ �,���!�������9'�"� K�0������0�������07����%��.�������"����� %� �����0��("��������� ��!��� ���� public void OnWriteCompletion(IAsyncResult ar)

{

Console.WriteLine("Write Operation Completed");

}

Page 313: 3Programmer's Heaven C# School Book

��������������� ���������

; /

-��������� ���$$� ��� ��

#��K �'���������.���� ����..������� ����"��� �����������!%� ���� �����"I'������������������"I'�����.������ � ����..������� '���0��� ���� �������������"��!���.�������� using System;

using System.IO;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static void Main()

{

FileStream fs = new FileStream(@"C:\C-Sharp.txt", FileMode.Open);

byte[] fileData = new byte[fs.Length];

Console.WriteLine("Reading file...");

fs.Read(fileData, 0, fileData.Length);

fs.Position = 0;

AsyncCallback callbackMethod = new AsyncCallback(OnWriteCompletion);

fs.BeginWrite(fileData, 0, fileData.Length, callbackMethod, null);

Console.WriteLine("Write command issued");

for(int i=0; i<10; i++)

{

Console.WriteLine("Count Reaches: {0}", i);

Thread.Sleep(10);

}

fs.Close();

}

static void OnWriteCompletion(IAsyncResult ar)

{

Console.WriteLine("Write Operation Completed...");

}

}

}

� ����0�����"�0���(9'�����"�!� �"�"�������� �� ��g����0��(,����"K�!��.�%� �����0��(� ���,�� *+�����"� "��"�����!��� �����A ��������.����� *+�����"�������������"�!��������9���""�������������g!�������K� "����"���!���.�� ���.����� ����������!���!����

Page 314: 3Programmer's Heaven C# School Book

��������������� ���������

;12

��������� �����"'���� ������!����� ����@��� �����*+�����"9.�� ���������0��������g!�������K� "���"����������������0��(�����"gA ��������.����� *+K� AsyncCallback callbackMethod = new AsyncCallback(OnWriteCompletion);

fs.BeginWrite(fileData, 0, fileData.Length, callbackMethod, null);

��������� .�� ��" ��0��� ����.� ���"��� ��"�����! 2�������� "� ������������ �&� ����'���������"����������� ��'����������"���@��� �����*+�����"��'��������� �� ��!���!���9������ �����"�����"����" ��0���(� "'����������'���� �����.����9������"�� �� ��� ".�� ���� ��0��� ������.���� '���� ������!�������.����9���A ��������.����� *+�����"����"��������"9.�� �� ���������� ����� ����&�����.��.��9���.��"��������8�� ������ �����g�E����.��8�K!���� "�8�����"���.��������������!����'� ������� Reading file...

Write command issued

Count Reaches: 0

Count Reaches: 1

Count Reaches: 2

Write Operation Completed...

Count Reaches: 3

Count Reaches: 4

Count Reaches: 5

Count Reaches: 6

Count Reaches: 7

Count Reaches: 8

Count Reaches: 9

Press any key to continue

��������� ��� ������.�������!������ ����'��������� "9������ �����"�����""�" �����0���(�"90���� �� ��"��.�� ���� ��0���A �����'�����.������ '����.����"9��.�� ��"��������'����������.�� �� ��"����������

�������9���� ���-�#�� �����9��J5� ���

��� �� ��� ���� �����"I'����9������ �������"0��� �"���"�

�� %� ���� �����"I'�����"��� �"!���IA��(������(�������������� ������������.��������� ����(��������� ���� ���.������ !�����"� �� "'���� �����!��������������� '�������"�����������������!���� ����0���"��� ������ �

�� � ���0��(���� "9@��� )��"*+� "@��� �����*+��������.����������"� ""�������������"� �I'���� ���(����� �'�����"��� ������� �����.���� ������' @��� )��"*+� "

Page 315: 3Programmer's Heaven C# School Book

��������������� ���������

;1

@��� �����*+�����"9!���8��.��9!�����������)��"��� "��������������������"� ���� ��� ���������"�

��$������$ ��������� ���� ������.�&�������

��������.���� ���� �'�������0�(�.�� �� "'�� ���"� ���������������8���������.���� �������������������.������������"��!���"� K�������������9���!����.� �"0�����.������'��� ��0������0����� ������.����� ��������..������� �����"�������.��!0��������������"��"�0��� �����'������".����������.� �!����������� ������������0���(����!���.���!��"� �����"���� ��0�������0��"��� �����8������ �!�����"�� �����'������"����(��������������� ���!� ����0���(������'������� ���"���������"�� !� ����0���('���0��8�����"�����"���!���.��� ���!�8��.��� ����'�����'���"0�"��"�����������.� �"�����'���"0�����"�

Page 316: 3Programmer's Heaven C# School Book

��������������� ���������

;11

����*�6������������������4�

����3�3���

6������� �.��� ��"�!��� ���0��(����"'��('���0������!���� "��� "����� �!������� ������ "���� "����� ��$��!����'��(�������.������(�������7���""���� ����'�����"��������� "����� �!��9( �' ���1�2�,�������� ��"�"'�� �""� � �'!����������� �����9.�����������'��������"��'����!!�������'�������� �8��� �.�������<��������"����0������� ��� 0��('��"���.���0�����'���.������'����� � .����������� �!����� ��������1�2���.�����!�� �29��� � �����.������'����� � �� �2����"�������.���� "�� ��8.����"'�����1�2� "����� 1�2�!����$���� ���������� ����9��'����9� ����������������1�2.�������� ��0��8.����"�����.���'������ �2���.����90�����������8.�������������.���"�$��0�����"�������� � ���!�������� �!����#)����� ��������L�""���� �������0�� ��"�������� ���������9!�����"���� ����'���0��8.����"������������ �����!���'����9���.���� ""����0�����1�2.������9�������'��� ��"������������ "����� �!����$���� �������� �����

8 ������.������� ���

����""���� �!�� ������������� �!��� ���� ��� ����� 1�2�!������� ����������� ��.�0��� "�� �����( �' �.���������.������.���9'����.����"��'��������!��������'����� ������0���������������"��� .������.���� ��9'��0��(� ���.���B��������.�����0�����.�9� "���!����!.������.���'������� �!��� ����0�� �0��.� �9�������"�������� ������ �����������.������C�"������ 0���"� �'�����������.��� ��������� �����!����� 9� "������.����( �'���������!�� ���������"��������������0�������� "��.��� ����� ����.��� ����������������.������.����7��� �����'���!� ���"��� ��0������� � ������.��������������"���'����"�!!��� �.��0������0��.� �P����'����.���� ������������������� ���.������ �"�����!����'� � �..���!����"��// Create a list that we'll put values of factorial in.

ArrayList factorial = new ArrayList();

// Compute first 10 factorials.

int fact = 1;

for (int i = 1; i <= 10; i++)

{

fact *= i;

factorial.Add(fact);

}

Page 317: 3Programmer's Heaven C# School Book

��������������� ���������

;1;

// Print them.

for (int i = 0; i < 10; i++)

{

int value = (int) factorial[i];

Console.WriteLine(value);

}

����'������ �� %����#����������� �����"�����!� �����L���!��� 2������!���!��������!� ���� � ������������"�'��(��8.����"9�'���K���.��0���Q���!�����������'�������������*����.�� �+'�� �������� ������!��������������� �%�����.������ ��������(L������ �������������'���������������'�����������(���������� � ������%�"�!�������� �������9'��� ��!������(� ������.������������������'�����'��0�� � ������ "��'���"0�.��!���0�� ����������'����������������� "��������9� ������������ ��"��0��0�������"�07����!� ���.�9������ �'�����.����C�������'����� � �����*��� ������ � E��!��� ����.�+�0�� �����"������� ����0�8� �� "� 0�8� ����0�.��!����"'�� �""� �������� "!����� �����!��������������� �%����"� "������ ������� ��"��������8��0���"� ���.��������������.��0����� ���� �07�������� K�� � ������ �������������� ����'��������� ���.������!���� ����� �����%���!����.��0�������� ������ ����L������ �'������������������������ '���� �����"� �������!�����'��'�����������9��� ��"!��������'���""��..���9��'���"0�����.��0��������"0�8� �� "� 0�8� �� "������.������ ����(����� ��� ���������""�"�������������� � "!��������.��������!� �������07�����""�"�6 ����� �����

)���� �������� ���

��� �..���!��"�0���'����E'�����!���.������� �90����������� ��������0�� ��"�// Create a list that we'll put values of factorial in.

List<int> factorial = new List<int>();

// Compute first 10 factorials.

int fact = 1;

for (int i = 1; i <= 10; i++)

{

fact *= i;

factorial.Add(fact);

}

// Print them.

for (int i = 0; i < 10; i++)

{

Page 318: 3Programmer's Heaven C# School Book

��������������� ���������

;1B

int value = factorial[i];

Console.WriteLine(value);

}

A ���������� ������0�� ��"������.��������'��!�������� ������ "�� ��List<int> factorial = new List<int>();

���!������ ��� �������������.�#����������� #��%�����0�� ���"�#���� �'��.�� ��1�2� "�������"� ��� �'��������������� �3� ���� ���.�������0�����"�!!��� ���������� �'�� ����� ��8���"�����%!��������.�#��9�����.�� ������0�� �� ��� �"9.����"� � ���0���(������������"���.�.��������*������ ���.���������.������.���+9� "��.���!�������.��!���������'���0�����"� �"������������� ��������"��� �������.������������������� �'"��..����"�������.���� �'( �'������������ � �����"� �����9� "�����!��������� � ��"��"������(���� �����&�����������9��� �'.��0��!������� �������.����C������������� 9������ ���� ��"!��0�8� �� "� 0�8� �9��������.��!���� ����� ��0���"��������&� ����9�� �"��'�����..� �!����!����'� ��� ���""�"�!������!������.�factorial[2] = "Monkey!";

������������ ��.������9���'���"���.������'����9���.������'���"�������� �����1

2

System.InvalidCastException was unhandled

at CSharp2Examples.Program.Main(String[] args)

<� ������ ����#����.���� ������.���� �'��� ����� !������� ��( �'��������� ��� ����"L� ��� ���������"0��""�"�������������� ����� ������.������.������ �'������ � �����������Cannot implicitly convert type 'string' to 'int'

% ������8��.���!��� ������������� ���� �'������ �����.������� ��������'� ��"�����������! ����!.��.��*�!��.���� �+'���������������"���*�!��.�� �+�%����� ������ '���"0�����������0����������� 9'��� �������(��� "�������������HashTable ages = new HashTable();

Page 319: 3Programmer's Heaven C# School Book

��������������� ���������

;1=

��������� ��������"��.E� ��.������ �!����������0��90��'��������.��!���(��� "�����.���������C�"����� �� �����"�!����'�Dictionary<string, int> ages = new Dictionary<string, int>();

����� ������.���(��'���.�.��������L� �!�����(��� "��������!�������������� ������� � ���.�.���������.����"�"9��������.�����"0������97���.��������0�� �.��"��������"'���"0��� !���9�'�K����� ����9���� ������������".���������� "��.��'�� �����.���� ��� ������.��!�����' �&� ����9��K'������(� �������������.��0�������������!����!� ������������������'����� ����������8.����List<List<int>> nested = new List<List<int>>();

����������� �D�������������$��������������������� ��������������������������������������� �������������������

�����������������>�D�������������������������������������������������������������� ��>�D�����%������� �������������

"#�������������%���������������7����������������������������������� ��������������������%����������������������������

�����������������������D������ ����������

����� �������� ���#$���

���.��0���������������' �� ������.�� ���'������ �����..���"� ��������!��'�� ������������ "����.����"�������!� ���� �����!��������� � ���.�������.�!��.��&���8��.��9����� ��'� �����'���� �.��.����������(����"�!������������07���� ����� ���������!� ������������0�� ����"�������.��.������'������ �� "����0��(��! ��9��'����� "0��(���"�!�����������'� ��������!��� �����9!����� "����07�����.��A ��.��� ����������.���� ����!����'9�� �����07�����.������� ���� ��� 0�����"� �������class DefaultProxy

{

private object defaultObject;

private object stored;

// Set the default object we'll return if nothing has been

// set.

public DefaultProxy(object o)

{

defaultObject = o;

}

Page 320: 3Programmer's Heaven C# School Book

��������������� ���������

;1?

// Property for object o.

public object target

{

get

{

return stored == null ? defaultObject : stored;

}

set

{

stored = value;

}

}

}

���'���'��(90����������������� � E.���������C�"��������� '��'��������% ������.��� '���"0���'�����.������C�"��.��!������!��������.�9!���8��.��� �������!�������8�9&������!�������8�������������0�"�"��9� ���!�0���!�� "��� �������� ��"��0��."���"�3� ����.����"���������� ���� � ��������0���� ����� ������.�� ������'���.����!������.���������C������.�L�����9����������������(����.�.�������������"� ��� ����� ���0���(��� ��89� "�� ��������'���� ����.�����������'�� "�!� � �������"�class DefaultProxy<T>

������S�T���.������0���-����(�'��� ��������0��� �'��������'�'���"��������9'��� �����.������0��� �'��������'�'���"�����.�������!���9'��� ��.����������!���S�07���T��.�� ����0�����"�'��������.������0��9���' 0���'�class DefaultProxy<T>

{

private T defaultObject;

private T stored;

// Set the default object we'll return if nothing has been

// set.

public DefaultProxy(T o)

{

defaultObject = o;

}

// Property for object o.

public T target

Page 321: 3Programmer's Heaven C# School Book

��������������� ���������

;1�

{

get

{

return stored == null ? defaultObject : stored;

}

set

{

stored = value;

}

}

}

% "����K��L'�K��7��������"��� ������.������ �� �����"0��..��� ����.�����!����.������0��S�TP� ����8��.��0���''��������'�� �� ���!����� ������.�9� �.���������C�"'���� �� "��������'������ ��DefaultProxy<int> proxy1 = new DefaultProxy<int>();

DefaultProxy<string> proxy2 = new DefaultProxy<string>();

��������.�������� (�!���'��"S� �T0�� ���8�������0������"'��������S�T�!�� "� �����!�������8����9������0��'�������� "��������"���� ��'�����������..� �

������ � ����#$��$����������

����� ������.�����'�K���� �!������'����������0�.���������C�"'���� ���.����'����9����� ���� ���.��!���� ��.������ ����� ���..���������.���� �"�����!����'� �.�������class MyGenericType<T>

{

private T item;

public void PerformSomeOperation()

{

foreach(object o in item)

{

// do something

}

}

}

�� �� ���������.��� �����0��*!���8��.��9� � �� ��+9�����.�� ������.���������'��������� � ������

Page 322: 3Programmer's Heaven C# School Book

��������������� ���������

;1�

foreach statement cannot operate on variables of type 'T' because 'T' does not

contain a public definition for 'GetEnumerator'

��������� "���9'��� ���� �����.������0��S�T��������������'����.���� �����6 �����0��� ���!��������"� ��� ���� �'S'����T(��'��"��� �'������S�T� ��89���"!��.���!�� �'���������� �����!���� "'���� ���!�������.���� ��class MyGenericType<T> where T : IEnumerable

������'��� �'���.���9� ".���������C� ���'���� ���.�������.���� �����6 �����0��� ���!���'��������"�MyGenericType<List<int>> chimp = new MyGenericType<List<int>>();

��'����9�..��� ����.�!�������.�.������������"�� ����.���� ��6 �����0��9����S� �T9'�����������.�������������The type 'int' must be convertible to 'System.Collections.IEnumerable' in order

to use it as parameter 'T' in the generic type or method

'CSharp2Examples.MyGenericType<T>'

$����������.��0������ ���� ���.������0����������������.���� �������� � �� ���!������ �����!���������� .��� ����� "��.���� �� �������� ���!������"����97���.���������0�������class MyGenericType<T> where T : IEnumerable, IComparable

���..���� ���� �!������ "��.������0��9���S'����T(��'��"���0�.����"���� ����������!������ ���� ��#��� ������������������� ����J����"90��.����.��(������ ���� �������������"�class MyGenericType<T, U> where T : IEnumerable, IComparable

where U : ICollection

������.��0������ ���� �.����������� ������.���!��� �����������.��� �������� "�����(��'��"�$������������� ���� �����..���0�!���� �� ���!������ ������ ���� ���� �!������� ��.������0���class MyGenericType<T, U> where T : struct

where U : class

�!'�� ��.���� �� ���� ������.����'����� �� ���������'�����.������ 0����.������0��9��� ������0������� ����������� ����������� �'*+�� ���� ��� 0���"��.���!������ ���.��������

Page 323: 3Programmer's Heaven C# School Book

��������������� ���������

;1/

�� ������.��.���������C�"'������.����"����"�!����.������������ ��������$�������!������� ��.������0������� ���� ���������!���� ������9� "�������� ����"������'������������� ���� ��class MyGenericType<T> where T : new()

� ����� �� ���������� ���

�!���K����44.������������ ���K��.��0�0�����"������������� (� �S�K���� ������0�!���9��K7�����.�������� T��44���.�������'��(��!���������.���������.������.���9��'��������������.���"�'��������9���.����"� ���8������ ����L������.������������.������.��"�������� ��"�"9� �!��������.�������.�����.���������C�"'������K���'�����(�� �8�� �� �!�K�����L�����������8�����0������� �-����������� �"�� ����9'���� ��8���'����������������� � ��90���'����44���.������ ����������.���"�'������'�"� ����������� ����*���-5,+"�" ��������0���"�!��"���..����� �������'����9���� ���"����'�.��0�������!����������!������ *���(� �����.����� ����+"�� ������� �C���.�.��������������� "��������������.��!���� ��0� �!��9� ������� ������.���� ����� � ������.������������7����.�����������.������0��'���A07���� "��� � ���������J����"������!���.���������C� �� �.��������* � E��!��� ��+��.����"��0�8� �� "� 0�8� ��� �$��9����� ������0�� ��"�!��"��0��'����!�� �������������0���(0��('��"���.���0�����'���.����������� �!����� ����9����� ������ �����8��'�� ���"���!������ � "��������.����C���� ���K"��.�!��'��������"������� ��� �0�8� �� "� 0�8� ���������C�"������������ ���������������������������������%����� ����F���� ��+������������=�������,������%� ������������

�����������������������������D������������ ������������������������������ ��������������������������������������� �

��� ����������� ����� E����������>��������� ������������������� EE�������������������EG�%���E��

"��� ����#$���

��� �!�������"�������.��������.�����������'���� ���0������� ��8���� ��%!������9.�����������.����.��������������� ����������������������J����0��� �0����.���� 90����� (!�������������� "���� "�������!���'��"S��.�T�������!���� �����.��������"�!� �������!L�����9����� "�����*0�� ��� �������� +����S.������T0����.����� ��������� �������"�!� �� ��.����!��������������6� ������9���K����� ���������.����S�����.����!���"�!� ���� �!������L����!���!���"� "����!��������"L0����������0������.������'����T�

Page 324: 3Programmer's Heaven C# School Book

��������������� ���������

;;2

���'� !���.��������.����������"�!� ���� �� �'0�.������������.��!��������������� ���!����� 5�������"���������3<�"��� ��9'����.����!������!���'� "�'��� �����"� "��� ��� �"������������������.��������.�������� �� �����"� "����� ��� �����"��"��� ��� �.�����!����% �����������'�����'�.���������'� ���'��(� "�!!��� ������"�!�����������������������!�'��.����!'���������"���� 0����� ��.����"�.���9��� �����.����� 0�.��� ���.��������������.�����!������������0���!��!��'�� ������������� "�������� �0��'����.������.90���������".��0�0��0��(� �'������� �����"�� ���������!���K�������� �������������� ��8���.��L��.���!����������.����!�������0�"�!� �"���'����7���""��� �'S.������T��"�!����&���8��.��9����� �'���"��������"�!� �"� ���� "�����.����������'�����.�����������"�!!��� ���.��!��� ��.���������� "�� ���.�����!����&���8��.��9� ���!���S��� (� ���T'�����������partial class PartyGoer

{

private int drinksHad;

private bool drunk;

public void HaveADrink(string drinkName)

{

// ...

}

}

���������!���S)������ ��.��T�����.����.�� ��� �partial class PartyGoer

{

.���������%U � �8P ���"�*+UcVV

.���������%�@U � �CP � ��*+UcVV

���%�@U � �8P ���"�*+UcV � �CP � ��*+UcVV

���.����

Page 325: 3Programmer's Heaven C# School Book

��������������� ���������

;;

public string[] FindAttractivePeople()

{

// ...

}

public void ChatUp(string name)

{

// ...

}

}

$����������.������!���""�!� �"� � �!����� ��!���.���������������0��� �����.������9� ������������������������������!���� S)������ ��.��T���.��!���������"��'����������"����������.������!���"!������!����� ��!������"�!� �"� S��� (� ���T�public void AskToMarry(string name)

{

if (drunk)

{

// Ask!

}

else

{

throw new ThatIsAReallyStupidIdeaException();

}

}

�����������������.���"�'��L����"� ���8������ �����������.���������������!���!����� ��!��������������� "���.�������!����"���0�� .���!��"���������&��������� 9���!����� ��!�������0�� ���������0���$���������� ����J����"��.���!����� ���!������������.���� ��"0���������������������.����!���"�!� �"L������.����'�����(����� �� �!��������� ��� �"�������.����������������!���9�!��������� �����!���"�!!��� ������ "�!!��� �.����!������9���K���������.�������������%�'�������0���������������7��� ����'�����"�!� ���� .����.�������������������.��� ��������(��.����������� "���� "�0��0������������.����� ��.!� ���� ��������'����9���������(��������'�������������(�"�0���� �� "� "���� "� �.���������"��0���������� ��� �����������'��!��������"�'�������� �����!���� "'���� ���!�������.���� �L���������!� "����!���!����� ��!��������0�����%���!���!���"� "�����"��� ��� � �.����������9'������� ���������� �������������.��!�����K������� �� �������� �����.��������������" ��0���"90���'���"� �����������!������ �"��� �'����������������� �����(���!�����������"��!��� �� �'��������� ��� �����"��!�������

Page 326: 3Programmer's Heaven C# School Book

��������������� ���������

;;1

����������� ����������������� ��������������������������������������������������� ���� �.����������������

������������������� ����������������������!�����0.��������������������������H������������������������������������������

����������!�������������������������������������������������>�����������������������������������%����������������������

��������� ���������������������������������� �������I����� ����J�������������������� � ��'�������D��������� ���H��������

���� �������������������������� ���� ������������������������ ���$������� �D������������������������?�����"#���������

*���������#$���

��� "���� �'�����!��� ����.����.��0�����������!��� ���� ���9� "����� ����������� ��07���0�� ���!��� ��"������ 0���"��� "���������9!���8��.��9 ���!��"���'�������0���������0���:���� ����� ����������� '�� !����� �"���!����"���0��L�!���J����!�����!� "���'9� �����!��� ���� 0������ �"��� "��������P�����'��9���!��� �����07��������.� "� ��������'����'�!�� "�� 0������ �"� $�'����� ��������� '����'�K��"�� ��J����0��7��'� �� ������0��(9� "����"���0��!���������"'���7������� 9!���8��.��9� � ������%.��0�����������9!��'����������.������� � ���������A ������� 9�!���� � ����������0�� �!�����"9�����(��������!C������ S ���� �'�!�� "T����� ����'��.��0��9��'����L�������� �� ����������"� "'� ��"� �����'������ ������������ �������$����0����.��""�����.��0���0�����'� ��������.�����(�� �����������.���!�������������.�����"0� ����0��9��.��.�����J����� ���(�!������ ����!�����.��int? x = null;

double? y = 5.2;

$��������� �'�'����0�� ��0������� ������9'��� ������ ���������'������� ����0�������0����0�� ���� ���������!��� ���9�����������!���J�������� �������'���"'������!��� ���������0����� ��5����.��.����9'����'���0�����!����!��������0��� ����if (x != null) // Alternatively, if (x.HasValue)

{

// It's not null.

}

else

{

// It is null.

}

:���� .��!�������������� "��������.������ � ����0���������.�7������'���"� ����� � E ����0������� �����0������8�����������'�� ��������0����� �� ����int? x = 6;

Page 327: 3Programmer's Heaven C# School Book

��������������� ���������

;;;

int? y = 7;

int? z = x * y; // z is now 42

��'����9�!� ��������0�������������"'������������ ������������������������8.���� 9���������!����'�����8.���� 0����� ����int? x = 6;

int? y = null;

int? z = x * y; // z is now null

&� ����9����������8���� ��8!����� � ��"�!����������!� ����0�������0��� ����������� �' ���������� ��.������9'�����.���SQQT������!��'�� ��� � �� ����0����.���� � E ����0����.��int? x = 5;

int? y = null;

int a = x ?? 0; // a is 5

int b = y ?? 0; // b is 0

����������� � �� ����0�������0����������0���!����J������ � � E ����0����.������.�������������int? x = 5;

int a = x; // Ooh, naughty.

:���� � �������9'������.������.�������.��� � ��int? x = 5;

int a = (int) x; // OK

��'����9�!8'� ����int? x = null;

int a = (int) x;

��� ��'������.���90��� �8��.��� '���0�����' ���� �����System.InvalidOperationException was unhandled

Message="Nullable object must have a value."

6� ����������� ���� ������8��.��� 9'�������0���!��!�������������9��������������0��0��������'��������.!���8��.��� �8.��������

Page 328: 3Programmer's Heaven C# School Book

��������������� ���������

;;B

���J����� ���(� ��8� " ���������� ��.���������������7��� �����������< "��������"������.���"�' ���� ����$����0����.�9'�������� �����������.�� ����� "��"�$�������0����������!���������$���� �����'������.������..���!�� ����0����.��� ����'��('�������������������8��.���!�� ����$����0����.�"�������9'������.�������������� ��"� �����������Nullable<int> x = null; // Same as int? x = null;

int a = x.HasValue ? x.Value : 0; // Same as int a = x ?? 0;

$����0����.����� ������8��.���!��'�� ������ 0�.�������"���

-��#������� �� ���3���� ��� ���

��������� ��� �0��������������"� "�������������9� ���"�!( �'� ���� ����!��������"�����0�� ������"9'��������!��� ������� "��(��������������������!��� �����������!������0���!����$����� �����9� "������ ����.����� ���'������������"���� "��� ��� �cpublic void MyButton_OnClick(object sender, EventArgs e)

{

// Handle the event…

}

c� "��� �""���������������"�������!��������� ��myButton.Click += new EventHandler(MyButton_OnClick);

<�������������"������ "�������� �*,�@���� ZA ����(� ������+�� �����������"'�� ������ ������E�����9���� �����������"�� ����"�������� " ��"��������� ��� ����&����������9�� ���� ��� "�� ������"���� ���!�'�� ��!��"��� ��%��9��'���"0����"��0��0���������'"��'����� "��������"� "���0���.��� �������� ���������������% � ���������"�""������!�����0������� � "����0�� �0�� �����������"���������.�� "�..�������.���� ����� !����������"�������"�������'�����!��� ������"�������!���'��"����� ��8!��� � � ���������"��!����'�myButton.Click += delegate(object sender, EventArgs e)

{

// Handle the event...

};

$������������!����� ����(���'���������������0���.��� �������� ��� "���90��� ���"�!.���!�� �'���������"���0���0�������� "���9� �'"���������.��0�� �������"�� ����(��'��"S"�������T�����"������!����'� ����������� �!���"���������.��������"0�"�9����� ���"0��������� �!���������� ������0���(��9'����������!���������.��������!����������"'���0������0������������ ���.���!��"� ���"���������.�"�!� ���� 9�"��� �����"0���'�

Page 329: 3Programmer's Heaven C# School Book

��������������� ���������

;;=

myButton.Click += delegate(object sender, System.EventArgs e)

{

MessageBox.Show(e.ToString());

};

-3��������6 � ����#������� ��

��!��� � ���������"����..�����0�������������� � �������(����"���������� ��!��"����� ��"��0�'����� !����� ��� "����A ��!������ ���(��������'�������.�'��!����� ���������������������0���!���� ���� ������"������0��� �"�������� �"�����!����'� �.�������// Create a list containing the numbers 1 to 10.

List<int> numbers = new List<int>();

for (int i = 1; i <= 10; i++)

numbers.Add(i);

// Add them all together.

int sum = 0;

numbers.ForEach(delegate(int x) { sum += x; });

Console.WriteLine(sum); // Prints 55

������ "�� �!������0������� ��� ���� ������ ����!� � � ���������"����&��6��������"��(��"���������!��� ��� "!������������ ������������ ��(�����������������"��������$����������"���������!��� �����0��!�"���������.�������(�� �.��������*�!��.�� �� ������+������ � � ���������"�"�!� �"0�!����'� ����"�������"�!� ���� '������� (�!��"�� �����0����������.����.���'�����.��� ������'��� ����������0��S��T� �"����� � ���������"�� ����0����8��.��'�� ����� ���0�������������'�.��"�6� ����������7������.������"'����'�����!���������.90��������� �J���!�������� ����� "�� 0���"� �'�"���������!������� �:������������.�� �0�'� "��� �'�����..� �!�������� �"�������������!��� ��� � � ���������"������� ��!�������������0��� ��� ���� ������"�����������������0��� ��������� ����������"���������� �������8����� �9� "�����������0����!����"��0����� � ���������"'��� ��� ����8��Q� !���9���� ����������� �"��'������!����'� �.������'���"�'�� ����� ���using System;

using System.Collections.Generic;

namespace CSharp2Examples

{

// A new delegate type that takes no parameters and returns

// an integer.

Page 330: 3Programmer's Heaven C# School Book

��������������� ���������

;;?

delegate int Counter();

class Program

{

static Counter GetCounter(int start)

{

// Counter variable outside of the anonymous method.

int count = start;

// Return our counter method.

return delegate()

{

count++;

return count;

};

}

static void Main(string[] args)

{

// Create a counter starting at 15.

Counter c = GetCounter(15);

// Loop 5 times calling the anonymous method.

for (int i = 0; i < 5; i++)

{

int value = c();

Console.WriteLine(value);

}

}

}

}

���.������'����������!����'� ����.���16

17

18

19

20

�������������0��S��� �T������'0�� �(�.����� "�< "��������"������.�������������"�� ��!��������0�����.�����!� ����� "��� !������� 9������ �� � � ���� ���"���� ".���� �� ����������

Page 331: 3Programmer's Heaven C# School Book

��������������� ���������

;;�

����"S���.�T� ������9�'�� ���������� ����������� ������������������0��� ��������� (� ��!���� � ���������"��!��'�����.���� ������������������ ��� ���� ������".��0�0��.����"���������.!��'���!��� (� ��0���'������� �� ����9����������.�����( �' ���(� �������������������� ���������������������� ����� �������������������� �������� �������������������� ����������

��D���������������������������������������� �������� ������������� �������� ��������������������� �����

������������������� ������������������������������%����� ������ ��'����������������������� �������������� ���

�������������� ����������� ����������������������������������� �����"������������������������� ������������

�������������������������������������������������������������������H���D�����������������������������������

���%�������������������������������

� ����� �� ����������4�

������.����� K�������"����� �'!������� ��1�290�������8.����"!����!��������7���""���� ����'���� �0�������"�����.������0��� "�!!���� ������� 9������'������!!�������.�������.����� ��7���8.��� �"���!������90��������� ����!���!��'���������� ���.� "'���(� "�!.��0�����������..����0�����)����0��������� �'������ ���0�8� K���'�����0��� ������.��������.��0���L0��"� K�0��!���"����������� �'���������O