mmmm C#

This is a discussion on mmmm C# within the C# Programming forums, part of the General Programming Boards category; I have to admit my first reaction to C# was hostile. Being a card carrying member of the 'Java haters' ...

  1. #1
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796

    mmmm C#

    I have to admit my first reaction to C# was hostile. Being a card carrying member of the 'Java haters' club the syntax was a big turnoff for me at first glance. Now that I've studied a bit of C# I'm warming up to it, and what I first believed to be a wasteful syntax isn't quite as wasteful as I first thought ( Believing C# to be a Java ripoff ).
    Here are two quickie programs that show size differences between C# and my beloved C.
    Code:
    // C# code
    using System;
    
    class Print
    {
      public static void printNumber ( int n )
        {
          while ( n != 0 ) {
            Console.Write ( n % 10 );
            n /= 10;
          }
          Console.WriteLine();
        }
    }
    
    class ProgramDriver
    {
      public static void Main()
        {
          Print.printNumber ( 45678 );
        }
    }
    Code:
    /* C code */
    #include <stdio.h>
    #include <stdlib.h>
    
    void printNumber ( int n )
    {
      while ( n != 0 ) {
        printf ( "%d", n % 10 );
        n /= 10;
      }
      printf ( "\n" );
    }
    
    int main ( void )
    {
      printNumber ( 45678 );
      return EXIT_SUCCESS;
    }
    I still don't like how the functions ( methods ) float in space while C's functions are completely left justified. It forces me to use a GNU bracing style to maintain somewhat decent readability.

    Does anyone have ideas for a C# bracing style that works well? This textbook code just doesn't do it for me ( I hate textbook code )
    Code:
    // Yucky yucky
    class Print
    {
      public static void printNumber ( int n )
      {
        while ( n != 0 ) 
        {
          Console.Write ( n % 10 );
          n /= 10;
        }
        Console.WriteLine();
      }
    }
    At the moment I'm using three bracing styles according to indention level. Class level indention ( fully left justified ) gets an Allman style, method level ( first level in, very floaty ) practically demands GNU to look good, and for the processing level ( if..else's, loops, switches, etc.. ) I maintain my K&R bracing.

    -Prelude
    My best code is written with the delete key.

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    This looks ok, (quasi C++ style)

    Code:
    class Print{
    
         public static void printNumber ( int n ){
             while ( n != 0 ) {
                   Console.Write ( n % 10 );
                   n /= 10;
              }
           Console.WriteLine();
         }
    
    }

  3. #3
    Unregistered
    Guest
    Code:
    class Print
    {
         public static void printNumber(int n)
    {
             while (n != 0) 
              {
                   Console.Write(n % 10);
                   n /= 10;
              }
           Console.WriteLine();
         }
    
    }

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Originally posted by Unregistered
    Code:
    class Print
    {
         public static void printNumber(int n)
    {
             while (n != 0) 
              {
                   Console.Write(n % 10);
                   n /= 10;
              }
           Console.WriteLine();
         }
    
    }
    that's dreadfully ugly. I just code like I usually do in C.

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Prelude, that's about the same code you'd use if doing the same thing in Java. It makes things much easier for a Java guy (like me) when the syntax is so similar.

    I'd structure the code like this...

    Code:
    using System;
    
    class Print {
    	public static void printNumber(int n) {
    		while(n != 0) {
    			Console.Write(n % 10);
    			n /= 10;
    		}	
    		Console.WriteLine();
    	}
    }
    
    class ProgramDriver {
    	public static void Main() {
    		Print.printNumber(45678);
    	}
    }

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Prelude, that's about the same code you'd use if doing the same thing in Java

    hmmm, I didn't realise Java imposed a particular style of code formating.

  7. #7
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Not the format in particular, but the actual code itself. Just a few changes. Example, instead of "Console.Write(n % 10);" it would be "System.out.println(n % 10);". Most of the code from this certain app could be "ported" like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mmmm beer
    By -JM in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-05-2005, 05:22 PM
  2. Mmmm, thats good antifreeze!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-03-2003, 06:32 PM
  3. text rpg adventure
    By BuRtAiNiAn FlY in forum Game Programming
    Replies: 13
    Last Post: 07-20-2002, 02:25 PM
  4. Checking for connected workstations.
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 06-22-2002, 02:07 PM
  5. IDE For C++
    By zzido in forum Windows Programming
    Replies: 5
    Last Post: 05-23-2002, 01:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21