Oxidation state of carbon
Method 1: Assume the compound is ionic

For binary compounds consisting of a metal (but not a transition metal) and a nonmetal, the best method of determining oxidation numbers is to assume that the compound is ionic and determine the charge on the corresponding ions. The oxidation number of a monatomic ion is equal to its charge. This a good assumption because most compounds of this type are ionic, and it will work on many compounds that might be covalent. To make this work, you need to memorize some typical charges for ions:


Alkali metals - Li, Na, K, Rb, Cs - are typically 1+
Halides - F, Cl, Br, I - are typically 1-
Alkaline earth metals - Be, Mg, Ca, Sr, Ba - are typically 2+
Chalcogenides - O, S, Se, Te - are typically 2-


These are typically sufficient to deduce charges for most binary ionic compounds.

Let's do some examples. 


  Definitely ionic: $\ce{Li3P}$


First, we assume that this compound is comprised of lithium ions and phosphide ions. The lithium ion is $\ce{Li^+}$, and there are three of them for a total of $3+$. Thus the charge on phosphorous must be $3-$. The oxidation number of lithium is +1, and the oxidation number of phosphorous is -3. 


  Definitely covalent: $\ce{BeH2}$


Beryllium hydride is covalent, but this approach still works. If we assume this compound is a beryllium cation, $\ce{Be^2+}$, and two hydride anions, we can work it out. The hydride anions must be $\ce{H^-}$ to make the compound neutral. Thus, the oxidation number of beryllium is +2, and the oxidation number of hydrogen is -1.



Method 2: Based on structure

If we know the structure, we can use an alternative method. This method works best for covalent compounds, and does not work well for ionic lattices or network compounds. For each atom, we can assign the oxidation number as follows:


Each bond to a less electronegative atom contributes -1 to the oxidation number
Each bond to a more electronegative atom contributes +1 to the oxidation number
Each bond between two atoms of the same electron contributes +0 to the oxidation number


Let's look at some examples:


  $\ce{H2O2}$


The structure of $\ce{H2O2}$ is $\ce{H-O-O-H}$. Each oxygen atom has one bond to hydrogen (-1) and one bond to oxygen (+0) for an oxidation number of -1. Each hydrogen has one bond to oxygen (+1) for an oxidation number of +1.


  $\ce{HCN}$


The structure of $\ce{HCN}$ is $\ce{H-C#N}$. The hydrogen atom has one bond to carbon (+1), for an oxidation number of +1. The carbon atom has one bond to hydrogen (-1) and three bonds to nitrogen (+3) for an oxidation number of +2. The nitrogen atom has three bonds to carbon (-3), for an oxidation number of -3.


  $\ce{NaC2H}$


This compound is an ionic compound with one of the ions having covalent bonds. The structure is $\ce{Na^+ ^-C#C-H}$. The charge on sodium is 1+, so its oxidation state is +1. The first carbon atom has a formal charge of 1- and three bonds to another carbon atom (+0). The formal charge is added to the contributions from the bonds to get an oxidation state of -1. The other carbon atom has an oxidation state of -1 because of one bond to hydrogen (-1) and three bonds to the first carbon (+0). The oxidation state of the hydrogen atom is +1.



Your compound $\ce{Mg3C2}$

Either approach will work for $\ce{Mg3C2}$. If we treat it as ionic (a valid assumption based on results reported in this paper by Hongzhe Pan, et al.), there are three $\ce{Mg^2+}$ cations in the formula unit, and carbon needs to have a corresponding charge to make the compound neutral.

In the same paper by Pan, et al., the structure of a $\ce{Mg3C2}$ monolayer is described as a honeycomb kagome lattice. Below is a representation of part of this lattice:

.

In this lattice, each magnesium is bonded to two carbon atoms, and carbon is more electronegative than magnesium. Each carbon atom is bonded to three magnesium atoms.

The similar compound $\ce{Mg2C3}$ is a little trickier. If we use the ionic method, we can get an average oxidation state for each carbon atom of $\dfrac{+4}{3} \approx +1.333$. An atom cannot have a fractional oxidation state, so the total +4 must be distributed unequally over the three carbon atoms. If we look at the structure, we can see that one carbon atom (the one in the middle) is different than the other two.

$$\ce{Mg^2+ ^2-C=C=C^2- Mg^2+}$$

Using the formal charges on the carbon atoms, you can determine the oxidation states.