1

So I'm a Java programmer and I find that the easiest way for me to learn a language is by jumping right in.

I'm creating a class in C#, currently in Notepad++, and I'm wondering how I save it so I can import it in other programs. Do I build it into a .exe or something else? Do I include it with using <classname>?

I can't really find anything with Google that satisfies my need. Any help is appreciated.

I'm not looking at Visual Studio as an answer. I'd really like to know how to do this from a command line.

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • c# is a text file and just need to be save with a cs extension. It is better to do edits in visual studio with the built in schema that will check for typo errors. When learning it is better to only type a few lines at a time so you don't get hundreds of warning messages that will be very hard to eliminate. – jdweng Jul 17 '15 at 06:23
  • See this for compiling from command line: https://msdn.microsoft.com/en-us/library/78f4aasd.aspx You can make it into a class library and link that into another project, or you can just compile it as a source with the other project. But frankly, I don't know what you expect to learn of the language/framework by complicating your life with this. Using Visual Studio would be the best way to start and leave these things to when you actually need them. – Sami Kuhmonen Jul 17 '15 at 06:23
  • welcome to c#, install visual studio and spend 1 day with us. – HungPV Jul 17 '15 at 06:23
  • I'll remove my answer concerning VS in a moment if it is really irrelevant, but out of curiosity first: Is there any special reason why you are so bent on using the command line for this, as opposed to getting a higher level overview of C#/.Net and VS? – Kjartan Jul 17 '15 at 06:41
  • Duplicate provides details on how to use CSC and build EXEs along with all necessary links. You likely looking for building Assembly ("csc /target:library File.cs"). – Alexei Levenkov Jul 17 '15 at 06:49
  • @Kjartan I like knowing how things work. I may not always have access to Visual Studio so knowing how to do it without it is a large priority. – Spedwards Jul 17 '15 at 06:59

3 Answers3

2

you can try CSC, C# compiler or more powerfull MSBuild.

For e.g. if you need to compile Foo.cs producing Foo.dll:

csc /target:library Foo.cs 

Path of CSC c:\Windows\Microsoft.NET\Framework\vX.X.XXX -32 bit version of csc.exe

c:\Windows\Microsoft.NET\Framework64\vX.X.XXX - 64 bit version of csc.exe

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
2

There are two different (main) options that should work depending on whether you are talking about using one single class or a stand alone collection of files (dll).

  1. Just save the file as a .cs file, preferably with the class in a namespace and then you can insert this file into your other projects and reference them as you have said with a using statement.

  2. If its a collection then you can compile it as a dll file, which with my limited knowledge of Java is similar to a JAR file.

For more information on how you can use dll's you can read msdn's How to: Create and Use C# DLLs

Sayse
  • 42,633
  • 14
  • 77
  • 146
0

If you want to build something to share between other programs, you build a "class library". In Windows, that means you will end up with a dll file that you can reference in your other projects.

Visual Studio has good free versions, that will be a lot easier than doing it from the command line.

For starters, you can compile a single file to library using

csc /target:library File.cs

This will result in File.dll.

To see all options of the command line, start here.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I understand I can do it in Visual Studio but I'd really like to learn how to do it from the command line. – Spedwards Jul 17 '15 at 06:21