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.
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. form1. designer.
form1. resx is most commonly used for storing strings that you want to translate into different languages so you don't hardcode them into your app.
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.
Yes you are mostly correct, however:
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.
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.
program.cs is the main of the application. This will be executed first when the application runs.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With