ASP.net c# publicly accessible functions in namespace - c#

In my namespace I can have public classes which are accessible to all other pages in my namespace. How do I go about having public functions? Do I have to create a class with the functions in it? Or is there an easier way?

In C#, all functions (which are actually called methods in C# terminology) are to be declared in a type (a class or a struct).
However, there is the concept of static classes in C#, which are good for the purpose of replacing “global functions” in older programming languages:
public static class MyUtilityFunctions
{
public static void PrintHello()
{
Console.WriteLine("Hello World!");
}
}
Now you can, anywhere in your program, write:
MyUtilityFunctions.PrintHello();
A static method is a method that doesn’t require an object. A static class is a class that contains only static methods (and static fields, static properties etc.).

Functions (Methods) "live" in types, so you need to put them in classes or structs. By default they would be private so you need to specify the public access modifier for them to be accessible:
namespace myNameSpace
{
public class myClass
{
public void MyMethod()
{
}
}
}
See MSDN - Methods section of the C# programming guide.

When doing C#, functions can only live in types (classes and structures). You can not declare a function on its own.

Related

Visual Studio - Using compiled static classes in client apps [duplicate]

If I define a class in a C#/.NET class library, then by making it COM visible I can instantiate the class and call its methods from VBA using COM.
Is there any way to call the static methods of such a class from VBA?
COM does not support static methods, and instances of COM objects do not invoke static methods. Instead, set ComVisible(false) on your static method, then make an instance method to wrap it:
[ComVisible(true)]
public class Foo
{
[ComVisible(false)]
public static void Bar() {}
public void BarInst()
{
Bar();
}
}
Or just make the method instance instead of static and forget static all together.
You don't have to mark the static method as not visible to COM, however it satisfies some code analysis tools that would warn you about static methods on COM visible types, and makes it clear that the static method is not intended to be visible to COM.
COM does not support static methods.
http://msdn.microsoft.com/en-us/library/ms182198.aspx

Is it possible to do static partial classes?

I want to take a class I have and split it up into several little classes so it becomes easier to maintain and read. But this class that I try to split using partial is a static class.
I saw in an example on Stackoverflow that this was possible to do but when I do it, it keeps telling me that I cannot derive from a static class as static classes must derive from object.
So I have this setup:
public static class Facade
{
// A few general methods that other partial facades will use
}
public static partial class MachineFacade : Facade
{
// Methods that are specifically for Machine Queries in our Database
}
Any pointers? I want the Facade class to be static so that I don't have to initialize it before use.
Keep naming and modifiers consistent across files:
public static partial class Facade
{
// A few general methods that other partial facades will use
}
public static partial class Facade
{
// Methods that are specifically for Machine Queries in our Database
}
The problem is not that the class is a partial class. The problem is that you try to derive a static class from another one. There is no point in deriving a static class because you could not make use Polymorphism and other reasons for inheritance.
If you want to define a partial class, create the class with the same name and access modifier.
you do not need to override anything, just give them the same name:
public static partial class Facade
{
// this is the 1st part/file
}
public static partial class Facade
{
// this is the 2nd part/file
}
You can not inherit a static class.
Static classes are sealed and therefore cannot be inherited. They
cannot inherit from any class except Object.
C# doesn't support inheritance from a static class.
You have to choose between your classes being static:
public static class Facade
{
// A few general methods that other partial facades will use
}
public static partial class MachineFacade
{
// Methods that are specifically for Machine Queries in our Database
}
...or whether you wish MachineFacade to derive from Facade:
public class Facade
{
// A few general methods that other partial facades will use
}
public partial class MachineFacade : Facade
{
// Methods that are specifically for Machine Queries in our Database
}
Although I am too late for the party...
According to your problem description, I think your goals are:
Want a static method group.
Want a sub-group of it to be specialized for certain domain.
In this case, I would suggest using nested classes:
public static class Facade
{
// some methods
public static class Machine
{
// some other methods
}
}
Pros:
Methods are static. You can use them directly without initialize any instances.
The nested class names are used like namespaces. Readers of your code knows clearly that Facade.Machine methods are more specific than Facade ones and their target domain.
You can make methods with the same signature in different class level doing different things to mimic method overriding.
Cons:
There is no way to forbids users calling Facade.Machine methods when they should not.
No inheritance. Facade.Machine does not automatically have methods from Facade.

I don't understand why a class is "public" [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
I'm beginning to learn C# and I come from a C++ background. The example page I was supposed to create by these instructions looks like
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my <b>default</b> action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
}
My main question is why the HelloWorldController class is prefixed by public. I understand that HelloWorldController is derived from Controller, but why does a class need to be public in the first place? My understanding of the words public and private is that they only have meaning if they're functions inside a class, and that public are the ones that can be used by instances of that class. Also, where is my main.cs in this Visual Studio ASP.NET MVC project that I created?
The purpose of public and private on a class differs from that on methods.
Classes (C# Programming Guide)
public class Customer
{
//Fields, properties, methods and events go here...
}
The class keyword is preceded by the access level. Because public is
used in this case, anyone can create objects from this class.
Access Modifiers (C# Programming Guide)
public class Bicycle
{
public void Pedal() { }
}
The type or member can be accessed by any other code in the same
assembly or another assembly that references it.
A private class wouldn't be able to be used by anything, unless it were within another class. C# doesn't allow un-nested classes to be private, as nothing could use it.
However, there is another option: you could mark the class as internal instead. internal restricts access to within the current assembly.
The keyword indicates who is allowed to create instances (objects) from this class. Private would be used if you have classes nested inside each other, and you don't won't it accessible from outside the class.
From MSDN
The class keyword is preceded by the access level. Because public is used in this case, anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behaviour and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

Static global Variable in C# has file scope?

in C++ you can define the scope of a global variable with the static keyword to be at "file scope" Is it the same in C#?
thanks!
C# does not have a concept of file scope. Something similar can be achieved by internal that allows you to restrict the visibility to the declaring assembly.
The static keyword: In C++, static can be used both to declare class-level entities and to declare types that are specific to a module. In C#, static is only used to declare class-level entities.
Useful link for you,
C# for C++ Developers
I don't know what is the file scope but you can define your variable in the class level and you can access it inside of your class whenever you want.
public class MyClass
{
public static object SomeVariable;
...
}
That is the largest scope for a variable in C#.
if you mean to make a class called Varriables and then call it each time like: Varriables.myNewVarriable then all you need to do is make a class called Varriables and then use: public static
public class Varriables
{
public static int myNewVarriable = 14;
}
then just call it from another class:
if (Varriables.myNewVarriable == 14)
{
Console.Write("True");
}
>>>True
Static member fields can be only public, internal (visible only in current assembly and declared friend assemblies) or private.
Additionnaly you can considere nested classes (even a public static field of a private nested class isn't accessible outside the outer class).
Another way to protect "Hot" shared members (not directly static, but that can be a member of a static instance) is to define an interface (that may be internal) giving access to this member. Then to implement the interface explicitly (specifying the interface name as dotted prefix of the member name) in the class of your static instance. To access this member the authorized code have to first cast the static instance to the interface.
Generally you have to considere using only internal access. Assuming that code in your current assembly will behave well concerning internal access members or types.
Maybe you can be more explicit on your needs and we can find an optimal solution.

Does C# need the private keyword?

(inspired by this comment)
Is there ever a situation in which you need to use the private keyword?
(In other words, a situation in which omitting the keyword would result in different behavior)
public class Foo
{
public int Bar { get; private set; }
}
Omitting the word 'private' would change the accessibility.
a situation in which omitting the keyword [private] would result in different behavior
David Yaw's answer gave the most usual situation. Here is another one:
In Account_generated.cs:
// Generated file. Do not edit!
public partial class Account
{
...
private partial class Helper
{
...
}
...
}
In AccountHandCoded.cs:
public partial class Account
{
...
public partial class Helper
{
...
}
...
}
The above code will not compile. The first "part" of Account requires the nested class Helper to be private. Therefore the attempt by the hand-coder to make Helper public must fail!
However, had the first part of the class simply omitted the private keyword, all would compile.
So for partial classes (and structs, interfaces), the access-level-free declaration
partial class Name
means "the other 'parts' of this class are allowed to decide what the accessibility should be".
Whereas explicitly giving the default accessibility (which is internal for non-nested types and private for nested ones) means "this class must have the most restricted access possible, and the other 'parts' cannot change that fact".
private isn't about the runtime behaviour. It's to make your application maintainable. What's hidden by private can only ever affect the code outside its class through the public or protected members.
So the answer is 'no' for runtime behaviour, 'yes' for developer behaviour!
In C# version 7.2 and later.
The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private-protected

Categories