Moving up from C and Tcl/Tk to Visual C#

  • C#
  • Thread starter berkeman
  • Start date
  • Tags
    Visual
In summary: C++, you'll be able to pick up C# relatively quickly.In summary, transitioning from non-OOP programming to C# can be a bit confusing, but with a little understanding it's not too bad. Everything is an object, return references need to be explicitly made, and there is a lot of syntactic sugar available. Timers are asynchronous, and you need to be careful when moving from C to C# not to rely on return values in asynchronous handlers.
  • #1
berkeman
Mentor
67,068
19,907
Is there a good way to think about the namespace/object/class/method paradigm when transitionig from non OOP C programming to Visual C# programmig? I've written Tcl/Tk GUI apps for manufacturing test fixtures in the past, but now I need to interface with multiple VISA instruments with National Instruments Measurement Studio and Visual C#.

I'd just like to get a better overall intuition for how C# program structure works, and when I would think about classes, methods and so on.

Thanks for helping out with some intuitive keys...
 
Technology news on Phys.org
  • #2
Namespaces are very important to .NET. Pretty much everything is in its own namespace. This take a bit to get used to when transitioning from C. Once you get used to it it makes finding the thing you want much easier especialy with autocomplete.

Everything is an object. Well except for the integral types but there are objects for those too. You have to be careful when passing integral types where objects are expected. The compiler will transparently box those into objects and eat up your cycles. The everything is an object rule makes shooting yourself in the foot a lot harder.

Return references passed as parameters need to be explicitly made as such even when called (see out parameter modifier).

The good news is you no longer need header files. That cruft is done automatically.

There is enough syntactic sugar available to give an elephant diabetes. See using, yeild, foreach, async, var, properties. And those are only the ones I've stumbled upon in the minimal amount of programming I have done in C#.

One major gotcha I have found is that timers are completely asynchronous. If you have a timer that fires one a second and your handler takes more than a second you will have multiple threads in that timer function. You get called once a second whether you like it or not. Another is that the asynchronous functions such as the Begin... stuff for networking will complete synchronously if the condition is already satisfied on calling. Don't rely on the return value in the asynchronous handler. The documentation sort of alludes to that but there is no warning about it.

Mashing about on bytes is frowned upon in all non byte oriented data. This is more foot protection but can be a pain when moving from C where your access to data is basically god mode.

Basically you need to go full OOP with C#. Lasing off into procedural mode will make your life difficult.

BoB
 
  • Like
Likes QuantumQuest and berkeman
  • #3
Here's some references that may help:

http://www.oracle.com/technetwork/java/codeconventions-135099.html

and

http://stackoverflow.com/questions/...e-for-package-naming-in-java-projects-and-why

At work we do something like this:
Java:
edu/com/org . company_name . your_division . app . client/server . name_of_app . whatever_organization_that_makes_sense_for_your_project
As an example, a computer aided instruction project might be organized as follows:
Java:
edu.txuni.mathed.app.tutor.client.display   // contains all display related classes
edu.txuni.mathed.app.tutor.messages         // contains classes related to messages passed between
edu.txuni.mathed.app.tutor.server.parser    // contains math equation parsing
edu.txuni.mathed.lib.calculus.parser        // contains classes related to parsing calculus expressions
The package naming comes into play during runtime when the JVM / ?VM is looking for a particular class to load so you want the package name to be unique to avoid collisions of simiarly named classes.

An example in Java, is the Logger class Oracle has one and Log4J has one and only the package naming keeps them apart.
 
Last edited by a moderator:
  • Like
Likes QuantumQuest and berkeman
  • #4
With regard to namespaces, C# is very similar to Java. In both languages you have a huge API set, several orders of magnitude larger than the C standard library or even the Standard Template Library (STL) in C++. Without knowing more details about what you need to do, it's difficult to point you in an appropriate direction. One of the features that @rbelli mentioned was asynchronous calls, which I think is tricky for newcomers to understand. Many classes have both synchronous and asynchronous versions of methods. When you use an asynchronous method, you provide a callback that gets fired when the asynchonous task is finished, but until that happens, other operations can be performed. With a synchronous method, your code is stalled until that method returns.
rbelli1 said:
Mashing about on bytes is frowned upon in all non byte oriented data.
So is accessing data using pointers, but you can do it if you mark the section as "unsafe." Just for my own amusement I write some C# code that executes intermediate language (IL) bytecodes (essentially machine code for the virtual machine that is used in all of the .NET languages). One chunk of code, 12 bytes, calculated the larger of two numbers passed to the code. Another chunk, about 30 bytes, converted Fahr. temps to Celsius or vice versa, depending on the value of a boolean parameter.

Anyway, it's a big jump from C to C#. The main source of information about C# is MSDN (Microsoft Developer Network), the company that developed .NET and C#. If you have some specific questions, fire away. I am reasonably familiar with C#, having written code in it for about 10 years while working as MSFT.
 
Last edited:
  • Like
Likes QuantumQuest and berkeman
  • #5
Mark44 said:
mark the section as "unsafe."

There is a clue in the name of the keyword that may indicate why using it is best minimized.

BoB
 
  • #6
Mark44 said:
mark the section as "unsafe."
rbelli1 said:
There is a clue in the name of the keyword that may indicate why using it is best minimized.
As mentioned, the code was for my own amusement.
 

1. What is the difference between C and Tcl/Tk and Visual C#?

C and Tcl/Tk are programming languages used for general purpose programming, while Visual C# is a specific programming language used for developing applications on the .NET Framework. C and Tcl/Tk are considered low-level languages, meaning they require more detailed coding and are generally used for system-level programming. Visual C# is a high-level language, providing more abstraction and built-in libraries for creating user-friendly applications.

2. Do I need to have prior experience with C or Tcl/Tk to learn Visual C#?

No, prior experience with C or Tcl/Tk is not necessary to learn Visual C#. However, having a basic understanding of programming concepts and syntax will make the learning process easier.

3. How can I transfer my skills from C or Tcl/Tk to Visual C#?

Many of the concepts and principles used in C and Tcl/Tk also apply to Visual C#. This includes variables, data types, loops, and conditional statements. However, there are some differences in syntax and structure, so it is important to familiarize yourself with the new language and its specific features.

4. Are there any online resources or tutorials for learning Visual C#?

Yes, there are plenty of online resources and tutorials available for learning Visual C#. Some popular options include Microsoft's official documentation and tutorials, online courses on platforms like Udemy and Coursera, and YouTube tutorials.

5. Can I use Visual C# for cross-platform development?

Yes, Visual C# can be used for cross-platform development with the .NET Core framework. This allows you to develop applications that can run on Windows, Mac, and Linux operating systems.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
13
Views
8K
  • Programming and Computer Science
Replies
2
Views
8K
Back
Top