none
How to program in C# using separate code files. RRS feed

  • Question

  • I am new to C#. My background is in FORTRAN. 

    In C# how do you put code in separate code files, i.e. CALL one subroutine/method from the Main program?

    It seems like everything has to be a class, but I am accustomed to CALLing subroutines (methods) and I don't see the point to creating a class to call a subroutine/method.

    I want to initialize my program with a MAIN .cs file and then CALL code that is in other subroutines that are in other separate .cs files.

    My compiler is Visual Studio 2013.  I'm ok with creating a "Hello World" Console program.  That's about it.

    thx...

    Saturday, March 12, 2016 7:16 PM

Answers

  • Yes, every code has to be part of a class. There is no way around that. Even if that class is nothing but a collection of static functions, that rule is absolute.

    However a class can be defined in multiple sourcecode files (each defining only a part), using the partial keyword:

    https://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Visual Studio's Windows Forms designer in particular is using the partial keyword to allow you and the designer to work on different files.

    Saturday, March 12, 2016 7:44 PM
  • Actually, C# is an object-oriented programming. That is why you have to create objects (classes in source code; objects in runtime) which would be used to trigger functions or to hold the values in their fields. Unlike, C++ (because I don't have any experience in FORTRAN; but I know they have similar paradigm), where you can create the code without having to create classes, in C#, you have to define classes and those classes are used in the modules. 

    There is another major feature in C#, partial classes. Which means that you have your source code "for a class" trimmed into different pages, which are merged together later on compilation. But that would still mean that you are going to end up creating multiple classes (with same name, and partially one).

    You can still have static functions, that don't need to have objects created. But that would still require definitions of the classes. So, basically, in C# you cannot have your code running around in the global namespaces without any class parenting it. :-) 


    The sh*t I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem

    Saturday, March 12, 2016 7:29 PM

All replies

  • Actually, C# is an object-oriented programming. That is why you have to create objects (classes in source code; objects in runtime) which would be used to trigger functions or to hold the values in their fields. Unlike, C++ (because I don't have any experience in FORTRAN; but I know they have similar paradigm), where you can create the code without having to create classes, in C#, you have to define classes and those classes are used in the modules. 

    There is another major feature in C#, partial classes. Which means that you have your source code "for a class" trimmed into different pages, which are merged together later on compilation. But that would still mean that you are going to end up creating multiple classes (with same name, and partially one).

    You can still have static functions, that don't need to have objects created. But that would still require definitions of the classes. So, basically, in C# you cannot have your code running around in the global namespaces without any class parenting it. :-) 


    The sh*t I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem

    Saturday, March 12, 2016 7:29 PM
  • Yes, every code has to be part of a class. There is no way around that. Even if that class is nothing but a collection of static functions, that rule is absolute.

    However a class can be defined in multiple sourcecode files (each defining only a part), using the partial keyword:

    https://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Visual Studio's Windows Forms designer in particular is using the partial keyword to allow you and the designer to work on different files.

    Saturday, March 12, 2016 7:44 PM
  • It seems like everything has to be a class, but I am accustomed to CALLing subroutines (methods) and I don't see the point to creating a class to call a subroutine/method.

    Well, in the link it explains the why a class is needed. It's OO 101 and you can't come around it. All OO language platforms like Java and .NET follow the same principles of class vs object vs instance in OO.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Saturday, March 12, 2016 10:30 PM