c# - Call functions in the Main method -



c# - Call functions in the Main method -

why running c# code shows error on f1(); in main?

namespace project1 { public partial class program1 { private void f1() { console.writeline("f2"); } private void f2() { console.writeline("f1"); } static void main(string[] args) { f1(); } } }

this console application. works if define object of class program1. when seek on windows form application, can set f1(); in button_click event , runs without error without defining object of class form1?

you have not defined methods static. hence need create instance of class first , phone call them using instance.

if want phone call methods directly, create them static. in case since seem displaying static text fine so. however, often, methods need deed on instances , must called such. may want @ this question, discusses when makes sense create methods static.

c# function call

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -