The point of the downvotes I think is that you're asking the wrong questions, which shows a lack of knowledge, which can be perceived as a lack of research.
From your title: "Which code is more CPU/memory efficient?" in itself already is a red flag. Usually when that is the actual question, the question is poor (as in: "is for faster than foreach?") or it comes down to StringBuilder versus string concatenation. Also, you can benchmark it yourself so you don't have to, or the code involves disk or even network I/O and you shouldn't even ask it.
But alright, let's click the title and read the question.
"I would say both codes consume the same amount of memory" - we're talking garbage-collected languages here, where the runtime itself already takes up 10+ MB of RAM to get your application running. Are you really interested in the memory load of one 32-bit integer, and what memory region specifically do you think it will reside in?
"Code 1 is more CPU efficient because it creates and allocates variable a just once". Did you benchmark that? Do you know what compilers do?
"keeping variable a inside the loop makes it belongs to Gen0" - where did you read this?
All in all, the confusion only becomes greater while reading your question, and the proper answer will become longer and longer to write in order to correct all misconceptions.
When learning about a subject, it helps to drill down to the smallest subset of the problem you're trying to solve.
Your first question should be "When do local value types get garbage collected?", which is answered in Do value types get Garbage collected? and How Garbage collector Will behave on value type and Reference type, to which the answer is: that's an implementation detail, but usually when you return from the method or when the runtime can determine that the reference will not be accessed again and it feels like doing a garbage collection run.
Your next question should be "Will an unused local variable be present in my compiled code?", which is answered in Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object? and Watch unused Local Variable is impossible? Why?.
Then finally, you can ask "Is it faster to declare a variable inside a loop?" (which, again, is a poor question in itself, the proper question would be "Does the compiler move a variable declaration out of a loop?") which is answered in Declaring a variable inside or outside an foreach loop: which is faster/better?: it doesn't matter.
Instead of typing all this, most people sigh, shrug their shoulders, down- or closevote and move on.
upvotes
it. Questions involving research on the other hand, mostly, are understood and answered only by a section of the community.