KCN is ionic in nature and provides cyanide ions in solution. Although both carbon and nitrogen atoms are in a position to donate electron pairs, the attack takes place mainly
through carbon atom and not through nitrogen atom since C−C bond is more stable than C−N bond.
Involved reaction is shown below:
R−X→SubstitutionreactionR−CN+KXAlkylhalideCyanide
Reaction of alkyl halide with AgCN
AgCN is mainly covalent in nature. Here only nitrogen is free to donate electron pairs,hence it forms isocyanide as the main product.
Involved reaction is shown below:
R−X+AgCN→SubstitutionreactionR−NC+AgXAlkylhalideIsocyanide
Hence, here isocyanide is formed as major product.

AgCN is mainly covalent in nature and nitrogen is available for attack, so alkyl isocyanide is formed as main product.
Cyanide ion is an ambidentate ligand (nucleophile or lewis base). It means that donation of a pair of electrons can be through either carbon or nitrogen, so it has two nucleophilic centres. KCN is predominantly ionic and AgCN is predominantly covalent.