PHP equivalent includes in C #

What is the equivalent PHP include () command in C #?

For example, PHP include is used like this: include ("ex.php");

Can I do the same in C #?

+4
source share
10 answers

If you mean in ASP.Net using C #, you can create a custom control (.ascx) and add it to the .aspx page.
If you are doing MVC, you can create a partial view.

The closest thing I can think of is to create an ascx user control named "MyUserControl"

in your page_load or pre_render:

MyUserControl cont = new MyUserControl(); this.Controls.Add(cont); 
+4
source

In C # there is no such thing. This is not a scripting language, so including a script block does not make sense.

What are you trying to achieve? In C #, there are ways to do things like this.

+3
source

There is no direct equivalent. You use links to reference other CLR assemblies (refer to their type of information) and using to import namespaces.

For example, the FontCollection class is in the System.Drawing.dll assembly and in the System.Drawing.Text namespace. So you would add System.Drawing as a reference and add the line:

 using System.Drawing.Text; 
+3
source

I am not sure if this is what you want to do. But just for the case, maybe you look at:

 <% Response.WriteFile( "YourFile.whatever" ) %> 
+1
source

In addition to the previous answers, which mention the using directive and add assembly references to your project (or on the command line at compilation), there is a way to load other compiled .NET assemblies at run time.

Assembly.Load will load the assembly (a compiled C # .dll file) into memory, allowing you to find and use types inside this assembly. This can be used when building the plugin architecture. You publish an assembly with an interface for the plugin. Plugin manufacturers can reference this assembly and implement your interface. Your application can then load plugin assemblies, check any types that implement your plugin interface, and load and use these types in your application.

+1
source

The only thing comparable in C # is using , which imports the namespaces defined in the assemblies referenced by the project. You cannot β€œinclude” a file in the sense that you are deleting content directly in your code.

For example, if your project references the System.Xml assembly, the following code will allow you to access all the classes in this namespace without fully defining their names:

 using System.Xml; 

This will allow you to use the System.Xml.XmlDocument type, for example, specifying it as an XmlDocument instead of its fully System.Xml.XmlDocument type name System.Xml.XmlDocument .

0
source

In C # there is no such thing.

You need to create an instance of the C # class and use it to call methods / attributes from other "packages" (C # classes).

You can also use the using direction to be able to reference assemblies from other projects.

0
source

Use is vaguely similar. It refers to another class, which can then be used from this file, but it does not include the contents of this file directly in the string.

 using system; 

(right at the beginning of the file)

0
source

Use this for C # @RenderPage ("header.cshtml")

This is taken here: http://www.w3schools.com/aspnet/showfile_c.asp?filename=try_webpages_cs_002

Although I know this post is outdated, people who can link to this post can link to it.

0
source

The using keyword is what you are looking for.

-1
source

All Articles