# What is the coordination number for BeCl2?

I am confused, should it be 2 or 4?

If I consider $\ce{BeCl2}$ as a chain structure the coordination number is 4, and in the gas phase it is 2.

Coordination number is determined by compound. It is normal for an element to have several different CN. Preferable CN can change for an element depending on the atom's neighbors. For example, Fe has preferrable CN=6 for fluorine anions, but CN=4 for chloride anions. When heated or under extreme pressure preferable CN may change as well. For example, $SiO_2$ has high-pressure modification with 6-coordinated silicon and 3-coordinated oxygen, while at normal pressure they are 4- and 2-coordinated respectively.
In case of $Be$ it prefers, just like all elements of the 2nd row, to have CN=4, but in many cases is forced to live with smaller numbers. BTW, in gas phase, $BeCl_2$ exists in equilibrium between monomer with Be's CN=2 and dimer with Be's CN=3.
In your case CN for Be in cristalline $BeCl_2$ is 4, while in gas phase it is 3 and 2 for dimere and monomer respectively. There is no 'one CN for Be atom right for all Be compounds.'
Beryllium is the most electronegative of the Group 1 and Group 2 elements (excluding hydrogen) and so rather than lose 2 electrons (and become $\ce{Be^{+2}}$ - a lot of charge on such a small atom) it can often bond covalently. Just like with carbon, the covalent bonding will involve mixing of beryllium's $\ce{2s}$ and $\ce{2p}$ orbitals. The coordination number for Beryllium can vary depending upon how the orbitals mix and the what hybridization results. In the gas phase the $\ce{BeCl2}$ monomer has a linear structure and the coordination number is 2. This is because the molecule is $\ce{sp}$ hybridized with two empty $\ce{p}$ orbitals. $\ce{sp}$ hybridization with two empty $\ce{p}$ orbitals requires bonding to two groups (coordination number = 2) in a linear arrangement. In the solid phase, where other $\ce{BeCl2}$ molecules are close, the chlorine atom from one $\ce{BeCl2}$ molecule can also form a dative bond to an adjacent beryllium atom. In this case the molecules can rehybridize to $\ce{sp^3}$ and form 4 bonds (coordination number = 4) arranged along the vertices of a tetrahedron. In this case the formerly empty $\ce{p}$ orbitals have become $\ce{sp^3}$ orbitals and share the lone pair of electrons contributed from a chlorine atom on an adjacent $\ce{BeCl2}$ molecule.