Difference between form1.cs, form1.designer.cs and program.cs in c#

16,554

Solution 1

Yes you are mostly correct, however:

  1. form1.cs is the code-behind file of the windows form. It is the class file of the windows form where the necessary methods, functions also event driven methods and codes are written.

  2. form1.designer.cs is the designer file where form elements are initialized. If any element is dragged and dropped in the form window then that element will be automatically initialized in this class.

  3. program.cs is the main of the application. This will be executed first when the application runs.

Solution 2

Well,

  • form1.cs: It is your code, events and custom code you write are here.

  • form1.designer.cs: The code for the components on the windows forms. You need it and you cannot remove. It is not recommend to change it manually for begginners.

  • program.cs: In C#, to a program start it looks for a static class that contains a static method called main(string[] args) and start executing the program in this scope. Here, in a windows forms application, the code creates an form and open it to the user start using the application.

Everytime you create a form, you will see you have the .cs file and .designer.cs and everytime you drag a control from Toolbox or change some property on the Property Window, the .designer.cs file will be changed.

Solution 3

program.cs - is static class which contain only one static method which need for starting your application. From MSDN:

Every C# application must contain a single Main method specifying where program execution is to begin.

If your project are just a library, then you don't need Main() - method in your code, and program.cs will not be generated

About form1.cs and form1.designer.cs - this is one class form1 which definition are splited in two files of code. From MSDN about partial class:

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

So this two files have a code of same class.
You can write code of control's initialization in your form1.cs. But need to remember that form1.designer.cs file will be generated always when you made a changes through designer of VisualStudio

Share:
16,554

Related videos on Youtube

user3100193
Author by

user3100193

Updated on July 04, 2022

Comments

  • user3100193
    user3100193 almost 2 years

    I'm really unexperienced with c# and I'm sorry if this is to easy question, but it will help me to understand my homework better.

    I have to make some kind of c# application in Visual studio, and my main question is: which part of code is situated in which file: form1.cs, form1.designer.cs or program.cs?

    I think that Visual studio generates code in Form1.designer.cs and that I shouldn't change it unless it is neccessery, in form1.cs are function that are activated by click on some form element, and in program.cs is the main of the application.

    Am I right, and is there anything else that I should know about these files at the beginning?

    Thank you very much for your answers.

    • Dan Bechard
      Dan Bechard over 10 years
      Welcome to C#, a good starting place would be msdn.microsoft.com/en-us/library/ms173077(v=vs.90).aspx The "What's In Your Project?" section answers your question. You may also want to read up on "event handlers", whose definitions generally reside in form1.cs.
  • Brad B.
    Brad B. over 6 years
    I'm working with old code where form1.designer.cs doesn't exist and it puts everything into form1.cs. If I drop a new button on it updates InitializeComponent() in form1.cs. What setting would have it behave this way?
  • Jirka Picek
    Jirka Picek about 3 years
    @BradB. Actually I guess you can create the file form1.designer.cs by yourself if you are using C# 2.0 and above. The *.deigner.cs file contains a partial class with mentioned method InitializeComponent() and with definitions and initializations of visual properties. Theoreticaly when you move these things then it should find it, when it will be in one class with your logic (but splitted to two partial classes). Unfortunately I can't test it because I don't have VS.