Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can we seperate the functions in different files?

Can we seperate the functions in different files?

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seraph_summer
    wrote on last edited by
    #1

    I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".

    O P A 3 Replies Last reply
    0
    • S Seraph_summer

      I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      Partial Types[^]

      1 Reply Last reply
      0
      • S Seraph_summer

        I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You can have the same class split over multiple files (from .NET 2 on), by using the partial keyword. It doesn't matter what the filenames are (before the .cs that is), because the compiler will combine them together. However, if you have too many functions then this would tend to indicate that there may be a problem with your design. You may be linking too many things together that really should be separated. Anyway here's a quick example:

        FirstFile.cs
        public partial class MyClass
        {
          // Do something.
        }
        SecondFile.cs
        public partial class MyClass
        {
          // Do something else.
        }
        

        Interestingly enough, you could create a member in FirstFile.cs and it would be available in SecondFile.cs. This behaviour is visible in the way that Visual Studio.Net breaks up form files.

        Deja View - the feeling that you've seen this post before.

        1 Reply Last reply
        0
        • S Seraph_summer

          I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".

          A Offline
          A Offline
          Albu Marius
          wrote on last edited by
          #4

          As a best practice it is advised to refractor functions that do similar things in different classes. That way you avoid having problem with too much code in the same .cs file. In my case I choose to have at most 400 lines of code in a class.

          I am fighting against the Universe... Reference-Rick Cook

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups