Author Topic: C# - Some basic questions  (Read 1152 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
C# - Some basic questions
« on: December 23, 2009 »


Next days - over xmas - i will try out Visual Studio C# and start my first steps into this language! However there are some questions about C#:

All C# programs (created exe/dll) are byte code programs with a Win32 PE header and running 100% JIT (just in time compiler) and dont have 100% x86 x64 asm code inline, right?

As far as i see, there are a lot of functions that C# dont has inbuild and where the coder must use directly the Win32 API inside a C# program, to access this OS functions. So the exe is a mixed exe of byte code and x86/x64 asm for Win32 api?

I thought ever that a C# program is 100% bytecode without using directly the Win32 API. (okay, i know that C# is internally just a layer over the Win32 API). Still wonder that some functions like the Win32 "WNetConnectionDialog()" is not available in C# - just only by using directly the Win32 API ???

Are C# exes have a Win32 PE header and runs itself (C# part) as 32 or 64 bit without having a compiled version for 32/64 bit?

When there is still the need to use directly the Win32 API for some advanced stuff, so where and what are the benefits to use C# instead code directly all in C++ using Win32 api? Sorry but i am cofused a bit atm... (probaly just due fact of to less background knowledge ^^)
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #1 on: December 24, 2009 »
The C# programs all run on the .NET framework runtime installed on your OS.  They normally don't have any x86/x64 code in them.

The .Net library is HUGE and you can do nearly everything with it that you can do with the Windows API.  If it turns out that you really, really need to call a Windows API function, or a function in a non-Windows DLL, then it is very possible to do that.  Interop services marshall the C# call to and from Windows x86/x64 land.  It's actually pretty simple, let me know if you need a sample.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #2 on: December 28, 2009 »
hi jim,
thanks for your answer. i started with my first c# program (winForms) and i managed to show a Win32 dialog, using a Win32 call ;)
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #3 on: December 28, 2009 »
Good stuff!   It really is easier to use MessageBox.Show() or the forms designer though ;) ;D

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #4 on: December 29, 2009 »
Oh yes, the form-designer is really a great and nice tool! The only sideeffect is, when you did something wrong (lost the designer file, dont know why) and so i had completly restart the project / GUI from scratch :P   Using the designer is nice, easy and fast... but seems you cant just copy/paste an existing MenuBar, WinForm to your new project. But its not a prob, now have restarted the work and this time i made a backup of the folder :P

Btw, showing .net components like the messagebox.show is really neat! I managed to show the "network connect" dialog which is not a component inside c#... Atm i am just trying out a lot of things and trying to understand the concept of c# and the project files...
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #5 on: December 29, 2009 »
I will take my lunch break time to ask you, how to create an own include file (class with functions or however its called in OOP) and how to include this file in own C# project, since i cant find any includeFile stuff or so. Thx in advance ;)
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #6 on: December 29, 2009 »
There aren't any include files.  All the code files are .cs files with classes in, you have lots of those.  I like to use one file per class with the file having the same name as the class.
Usually all your classes will be in the same namespace (usually the name of the project) but if not, you can add the 'using' for the namespace at the top of the cs file.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #7 on: December 31, 2009 »
I noticed that my actually project require a lot more of memory in comparsion to non C# program and so i googled.

Maybe an interesting article about C# bloody world of mangaged code:
http://www.codinghorror.com/blog/archives/000271.html

So, is there any way to otpimize C# programs in memory usage without cheating?
« Last Edit: December 31, 2009 by va!n »
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #8 on: December 31, 2009 »
Still having some big beginner problems! :diablo:
Learning process in C# is still going forward in very little steps by trying things...


Problem - Copy full project folder:

I have created from time to tome BackUps of my actually project by just copying the full project folder and rename it like adding _Test2, _Test3 and so on...  The actually project is saved on C:\ in the standard project folder given by VisualStudio!

Why does a full copy of the project folder from the c:\ drive to d:\Projects\ does not works (compile) anymore? Are there relative paths in the projectfiles (even i didnt created myself)! Is there any way, to copy or save the full ProjectAs to another drive/folder without having compiler problems? (i had the same problem even in the past with VisualStudio and until now i dont know how to fix it)


Problem - Loading *.csproj == no files, forms shown due fact of a mistake/error

Its still coding horror and its not the first time, following problem happens to me and loosing the complete project!  :boxer: I phoned with my best girlfriend while i have tried to continue my project and added/removed test code... When i try to open the project by doubleclick on the *.csproj file, the VS #C opens but it does not show any files (designer forms) or so.. Its looks like complete empty... but the form files, designer files are all available in the project folder and the files are correct (not empty).

I had something similar, the project opens in the IDE but showed a error message (i didnt understand and i couldnt fix the problem)... Now it seems once again, the full project is destroyed and i have once again to start from scratch! :( A real horror!

Any ideas to solve the 1st and 2nd problem? Thanks in advance...
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #9 on: January 01, 2010 »
Quote
So, is there any way to optimize C# programs in memory usage without cheating?
Not really.  Basically when you no longer reference an object it gets marked for deletion, and at some point the garbage collector will come along and free it up.  This means memory usage is 'blocky', you will use an awful lot as stale objects hang around, but then GC will come along and clean up a whole slab of them at once.  You can force the GC to run but, seriously, don't do that.
Why do you care how much ram you are using?  It won't try to use more ram than you have got.  It's by design.  If you want more control over how much ram you use then code in C or C++ instead.

Your other two problems...I really don't have those problems myself, I've never seen or heard of anything like it.  It's certainly not normal.  I copy projects around all the time.
Sometimes the designer files take a long time to load or don't appear right unless the project has been recompiled, but that's normally only when you have a massive (100+ projects) solution.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #10 on: January 01, 2010 »
Thanks for your answer Jim and first i wish you a happy new year! I just only care a little bit about the memory usage about .net programs, since i saw the difference to a C/C++ program and at least personally i dont really care so much about it... Its just more a view by the user who want use later my program and is saying, why should i use your .net based version which eats a lot more of memory in comparsion by the original version by xyz coded in C/C++/Deplhi or whatever which takes only half or less memory ;)

Btw, until now i like to learn/code in C# by just trying things^^
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #11 on: February 05, 2010 »
Yesterday i spoke with someone who is coding in C# too and he told me, i should NOT use the name convention i have found here and i am still using: http://www.akadia.com/services/naming_conventions.html He means i should use for GUI elements uiNameOfElement (ui = user interface) and instead using frmName.cs i should use NameForm.cs - So i would like to know whats the best and most common name convention when using / trying to learn C# ?

Another question is, when creating a Form with the Designer, you can write the needed code (methodes) directly into the same Form.cs file! I think it makes more sence to have just only the Form code and using an external file (also another class) where i have all the methodes, right? But when having a Menubar, ToolBar and ListViews on your Form, whats the best way to split up the files? (Its new to me, because i only coded in basic before and now i want like to learn how to code professional - the right way - with C#!

So, does it makes sence to have an own DialogClass, where i still only have a collection for Dialogs (like OpenFile, Win32 API implented Dialogs and so on?) - Its something wired and new for me.

Thanks in advance
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #12 on: February 05, 2010 »
Most people in C# use the conventions shown on that page (upper and lower camel/Pascal case), but for my ui elements, I like to use stuff like
tbTextBox
or
cbComboBox
so I can find them easily.  Your friend is not in the majority.
I don't the like _notation for private members.  I use lower camel case instead.
Hungarian notation sucks ass and should die.

Normally when you need some code to supplement the designer generated code, VC generates 2 files, one which is the automatically generated code and one for you to work in - it uses the 'partial' class keyword to split the code up.

You don't need to use Win32 calls for OpenFile dialog.  There are classes in the .NET framework for doing that.  Check out the System.Windows.Forms.FileDialog hierarchy.  I think I said before, unless you're doing something odd you do not ever need to call the Windows API directly.  You need to stop thinking that way and look at the .NET library instead, use the interop stuff as an utter last resort.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1334
  • Karma: 106
    • View Profile
    • http://www.secretly.de
Re: C# - Some basic questions
« Reply #13 on: February 05, 2010 »
For my GUI elements i am still using nearly the same stuff like you:  lblLabel cmbComboBox cbxCheckBox and so on... (as reference i am using the stuff like on the website i posted before)

Yes, when generating Forms with the designer, there are 3 generated files available:

Form1.cs                  - The one where i can/should write the code in, right?
Form1.Designer.cs      - Generated source defining the GUI / Form
Form1.resx                - Seems to be unimportant for us but not for the compiler/linker

Sorry, if i explained it a bit wrong.... I dont use Win32 API for OpenFileDialog... I know its a build in C#/Net feature... I use Win32 for Dialog like: ShowFileProperties == using ShellExecuteEx, WNetConnectionDialog(), WNetDisconnectionDialog()...

Atm i am writing all needed code of the mainwindow, directly in the mainsource Form1.cs but i think to split the source to have more overview... but first i have try to check and understand, how to add at runtime entries with icons to the menubar ^^ Next i have try to get work how to show all files/dirs with icons in a listview and browsing dirs in the listview... (i started with some tests to show all files/dirs but without its icons and without browsing ^^) i will try step by step...

First i have to restart my full project from scratch, because in the generated Form source there are elements like Buttons, Checkbox i dont use and they are not visible in the Designer... (strange!)   Possible this is from my first dasys when i played with the designer to understand how to work with it... Due fact i dont want any confusing unused "hidden" elements in the WinForms, i will redesign the full project to have hopefully a clean project.

What do you mean with InterOp stuff? (sorry, cant really tranlate / understand it in german)
Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows7 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: C# - Some basic questions
« Reply #14 on: February 05, 2010 »
interop is the name given to the interface between c# managed code and COM and other non-managed resources like the Windows API.  You shouldn't have to use it very often.

btw. you can edit the .designer files by hand provided you have a good idea of what you are doing and you've closed the dialog editor.  That might help you tidy up your Form without starting again.

Jim
Challenge Trophies Won:

Offline Xetick

  • Atari ST
  • ***
  • Posts: 106
  • Karma: 73
    • View Profile
    • Plane9
Re: C# - Some basic questions
« Reply #15 on: February 06, 2010 »
If you have managed to hide elements in the designer just use the outline. Its a great helper for seeing the structure of your winform and for chaning the order and position of some controls.
You can find that window under "View->Other windows->Document outline"

Regarding to naming I personally use "MyPropery" style for public variables and properties. "_privateVariable" for variables that are private to the class and "myLocalVariable" for variables in a function. Reason for it is that using this I can directly see if someone else can access the variable or if it only exists in the current function. Using the same naming in local variables and public properties/variables will give you a headache in the end. But it's of course up to personal choice.
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won: