Help - Very novice problem w/ C#

Started by
6 comments, last by SpacedOut 18 years, 7 months ago
I've just decided to learn a little about C#, i've already learned and successfully completed programs in C++ and decided to check out a book called "Thinking in C++ by Bruce Eckel," it came with a CD called "Thinking in C, Foundations for C++ & Java" and the book suggested I learn basic information about C# before reading the book. So what's the problem? Well on the CD they have a few examples i'm supposed to code in myself, but I can't get the code to work. The following is the first very basic code that I must put into the compiler: -------------------------------------------- /* first.c: A First Program */ #include <stdio.h> int main() { puts("** Welcome to Thinking in C **"); puts("(You'll be glad you came!)"); return 0; } -------------------------------------------- The next few examples are arithmetic related programs, which react the same way as this one when I enter them into the compiler. At first I thought I was entering the code in wrong, so I went against my studying will and simply copy and pasted the code from the example of the CD... it also refused to compile. When I say refused to compile, I mean the dos window pops up for half a second then vanishes instantly. Actually, one example does stay up but it vanishes after I enter a value... it is supposed to ask for two values then give a result. I have already done problems exactly like this with C++ code and they have worked 100% fine with no problem at all, so I don't understand why I can't do this with C#. Maybe my compilers aren't adequate? I have DEV-C++ and Miracle C. Miracle is supposed to be C# compatible, I don't know for sure about DEV-C++.
Advertisement
Try this:

using System;namespace Hello{    public class World    {        public static void Main()        {            Console.WriteLine("Hello world!");            Console.ReadLine();        }    }}
Thanks for the reply SnprBoB86,

I want to stick with the code the author of the CD gave, however, because that code is what he uses throughout the CD on all of his examples.

However, I did try the code you gave and received the following errors:

Ln1 expected nested-name-specifier before "System"
Ln1 "System" has not been declared
Ln5 expected unqualified-id before "public"
Ln5 expected ', or ;" before "public"

Ok Blue084, let me explain something what I think is going on. In that book you are writing, they are showing you an example of what unmanaged code looks like, i.e. C code, not C#. The reason why? "first.c" tells all. It is a C program, which it even looks like. If you are in C#, the files are ".cs" by standards.

So if you are wanting to do C/C++, which that code is, you will need to not use a C# compiler, such as Dev-Cpp or Visual C++. For C# code, as SnprBoB86 has showm you will use a C# program. You can use the free Visual Studio Beta 2 compilers from MS if you wanted to as well to help you out.
Quote:Original post by Drew_Benton
Ok Blue084, let me explain something what I think is going on. In that book you are writing, they are showing you an example of what unmanaged code looks like, i.e. C code, not C#. The reason why? "first.c" tells all. It is a C program, which it even looks like. If you are in C#, the files are ".cs" by standards.

So if you are wanting to do C/C++, which that code is, you will need to not use a C# compiler, such as Dev-Cpp or Visual C++. For C# code, as SnprBoB86 has showm you will use a C# program. You can use the free Visual Studio Beta 2 compilers from MS if you wanted to as well to help you out.



Thanks for the reply Drew :-) I appreciate a lot and it has saved me hours of frustration. Imagine how dumb I feel, I didn't even realize C# and C were two completely different programming languages! I admit I didn't think i'd be learning either of the two until just recently, i've always planned on learning C++ or Java before trying any other language until I found out learning C (and maybe also C#?) was a very good foundations as C++ did after all evolve from C.

I'll consider d/ling that program you mentioned and thanks for the recommendation. Thanks again for your help!
whoa there, who said that learning C is a good foundation for learning C++? It is not. Please see this link, written by Bjarne Stroustrup, the creator of C++:

http://www.research.att.com/~bs/bs_faq.html#prerequisite
Quote:Original post by Glak
whoa there, who said that learning C is a good foundation for learning C++? It is not. Please see this link, written by Bjarne Stroustrup, the creator of C++:

http://www.research.att.com/~bs/bs_faq.html#prerequisite


Thank you for the reply Glak,

and thanks for the link, i've added it to favorites. However, they say that you shouldn't learn "all of C" as a prerequisite and i'm not. The book i'm studying from has a CD that has introductory functions and basic lectures about C that the author suggests is a good introduction to programming in general and is beneficial to know prior to reading his book.

Actually, on the link you gave me there is a link under the topic "How do I start learning C++?," that links to "book reviews on the ACCU (The Association of C and C++ Users)," on this site the book I am reading is highly recommended. I'm glad to see i've picked a good book. :-)

Book i'm reading: http://www.accu.org/cgi-bin/accu/rvout.cgi?from=0au_e&file=t002005a

Thanks again for the sites Glak.
here's what you have to realize Blue. Everyone has their own theories about how novices should learn programming. some say start with a simple language like Python, others say jump right into C++.

even with C++, some argue to jump right into C++, others argue to learn C first. i've seen others say, learn Java first. C# though, that's a new one, i've never heard that one before.

the reality is, there's not right or wrong course. just because you see someone with a college degree explain, very logically, why you should do it their way, doesn't mean it's the only way, or the right way for you. however you decide to learn programming, however, the most important thing is to stick with it.

what worked for me, I started learning Java with the intention to learn that language, but switch to C++ early one, and learned that language.

This topic is closed to new replies.

Advertisement