Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# Tutorial

  1. Nov 1, 2007 #1
    This is for very beginners, trying to start up with C#.

    I'll post some short notes when I'm free. I hope to make this into a useful collection of information on C# programming language. Feel free to ask any questions.

    For now, you will need Visual Studio .NET, there is an express edition here.

    http://msdn2.microsoft.com/en-us/express/aa975050.aspx

    try a simple Hello World for the first program. My method of tutorial will involve programs followed by explanation of what the lines do.


    Start a new console application in VS and type these in:

    Code (Text):

    using System;
     
    public class HelloWorld
    {

         public static void Main( string[] args )
         {
               Console.WriteLine( "Hello World!" );
         }
    }
     
    GO Build>Build solution.

    Then go Debug>Start Without debugging.
     
  2. jcsd
  3. Nov 5, 2007 #2
    Code (Text):

    using System;

      public class Addition
      {
         
         public static void Main( string[] args )
         {
           int number1;  
           int number2;  
           int sum;

           Console.Write( "first integer: " );
           
           number1 = Convert.ToInt32( Console.ReadLine() );

           Console.Write( "second integer: " );
           
           number2 = Convert.ToInt32( Console.ReadLine() );

           sum = number1 + number2;
           Console.WriteLine( "Sum is {0}", sum );
        }
     }
    Think of classes as capsules where things in your program go.

    This is the main method. It is the method which automatically executes when you run the program.

    This declares some variable. They are of type int meaning they are integers, so no decimals.



    Console.Writeline is used to display text on the screen. So it tells user to enter the text

    in the second line number1 variable is assigned to the value on the right.
    text is input by the "Console.ReadLine" and when pressed enter it is converted to an integer value by Convert.ToInt32()

    the next two lines do the same for number2.

    This assigns the addition of two variable to the sum variable.



    Finally the program comes up with the answer. The {0} is tells it to insert the variable in the comma-separated list with that sign, in this case "sum".


    That's it, play around and use 3 variables and maybe -, * / operators.

    Good Luck!

    BTW - is anyone following this?
     
  4. Jun 19, 2008 #3
    how to use multiple operations,

    for example: subtract and addition
    division and multiplication
     
  5. Jun 19, 2008 #4
    how to use multiple operation

    for ex. the sum of the 2 interger(number) will be subtracted by another interger(number)..
     
  6. Jun 19, 2008 #5
    How to use multiple operations

    for example: The sum of two integers will be subtracted by another integer..
     
  7. Jun 19, 2008 #6

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    int n = 5 + 5 - 7; // n will be 3
     
  8. Jun 19, 2008 #7
    Interesting. That language looks much closer to java than C++, and for some reason I thought it was like a simplified C++ for web development.
     
  9. Jun 19, 2008 #8

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    It is more like Java than any other language: C# is Microsoft's attempt to "fix" Java. There are important differences, though. In a nutshell, Java is more portable than C# while C# has more of a focus on performance (thus its 'unsafe' code, i.e. pointers).

    I've never used it for web development, but it can be used for that. (I prefer Apache and related products to ASP.)
     
  10. Jun 19, 2008 #9

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    I really don't like that syntax,
    If Console.ReadLine() returns a string then shouldn't ToInt32 be a member of the stirng class ? so you can write Console.ReadLine().ToInt32()

    Instead Convert is some sort of general utility class, in which case shouldn't ToInt32() be a static member since you are never really creating a Convert object.
    Is this a Java legacy?
     
  11. Jun 19, 2008 #10

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    I would do int.Parse(Console.ReadLine()), personally.

    I'm not sure what your second point is. ToInt32 is a static method, that's why you write Convert.ToInt32(blah) rather than blah.ToInt32().
     
  12. Jun 19, 2008 #11

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Thanks - I don't know c# I was just looking at it from a c++ point of view.
    So built in types are also classes - that makes sense for casts to be members of int rather than standalone functions.

    My 2nd point was that there is nothing syntactic to show that the Convert is static, other than knowing that Convert is a keyword/library call you can't tell it isn't a local object.
     
  13. Jun 19, 2008 #12

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Or more generally, there's no way to tell a static method from a nonstatic method unless you know what is a class vs. what is an object.

    Foo.Bar() could be a static method of the class Foo, or a nonstatic method of the object Foo.

    The convention is to start classes with a capital letter and objects with lowercase, but this isn't a part of the language.
     
  14. Jun 25, 2008 #13
    What is the differences between this two lines?
    Convert.ToInt32( Console.ReadLine() ); and int.Parse(Console.ReadLine())..... ^^
     
  15. Apr 23, 2009 #14
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C# Tutorial
Loading...