How to define a method following top-level statements - c#

I recently updated Visual Studio and found out about this new feature (to me it is new) of top-level statements.
As I understand it, the compiler completes the definitions for the Program class and Main method, without you having to explicitly type it up.
This is useful, but I'm having trouble when defining a new method. I would like a method in the Program class. And call this with a top-level statement. Here is some example code:
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();
public static void ThisShouldBeAMethodOfProgramClass()
{
Console.WriteLine("Static in Program class");
}
This is giving me build errors, because the public static modifiers are not valid. I think it interprets this as a local function in Main. I can remove the modifiers, but this is just example code, my real code has more methods and classes.
How can I do this? Should I not use top-level for this?
I would like this to effectively be the same as:
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();
}
public static void ThisShouldBeAMethodOfProgramClass()
{
Console.WriteLine("Static in Program class");
}
}

You can keep using top-level statements and append additional members with a partial Program class.
using System;
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();
public static partial class Program
{
public static void ThisShouldBeAMethodOfProgramClass()
{
Console.WriteLine("Static in Program class");
}
}

Or just remove the access modifier: method without access modifier
using System;
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();
static void ThisShouldBeAMethodOfProgramClass()
{
Console.WriteLine("Static in Program class");
}

Related

Define global alias for static classes

I have a static ExceptionHelper that looks like this:
public static class ExceptionHelper
{
public static async void ShowDialog(string message)
{
// Show message
}
}
Whenever I want to call this method I do it like this at the moment:
ExceptionHelper.ShowDialog("This is a message.");
I now thought of defining an alias for the ExceptionHelper to not having to write the whole word each time I want to use it.
I know I can achieve it with using:
using Ex = MyNamespaces.ExceptionHelper;
But then I'd have to define it in each file I want to use the method. Is there a way I can define the alias globally without changing the name of the class? Or is there any attribute I can set above the class declaration?
Extension Method
You could make it an extension method on string.
public static class ExceptionHelper
{
public static async void ShowDialog(this string message)
{
// Show message
}
}
Then you would use it like so:
using WhateverNamespaceExceptionHelperLivesIn;
public class TestClass
{
public void TestMethod()
{
"This is a message".ShowDialog();
}
}
This makes your question moot - you don't have to define an alias at all.
Static imports
An alternative approach is to import the class statically. You won't need an alias, because you can reference the ShowDialog method directly. This will require C#6/Visual Studio 2015.
using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;
public class TestClass
{
public void TestMethod()
{
ShowDialog("This is a message");
}
}
In C# 6.0 you can use static usings:
using static MyNamespace.ExceptionHelper;
Of course not globally, that works only for defines. But in a file where you use this line, you can use the members of the ExceptionHelper without any prefix.
As of C# 10, you can now define gloabl usings.
// GlobalUsing.cs
global using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;
And it will now be available globally, without having to define the class name, or the namespace, at the top of each class.
// Available Globally in the Project
public class TestClass
{
public void TestMethod()
{
ShowDialog("This is a message");
}
}
This might apply, even though you are using a method. You could use an ENUM type instead that lies outside of any namespace and access globals values that way. Place the enum in a file outside of any namespace. You can access it globally that way, or if you have trouble, using the "global" keyword below if you have any trouble referencing it:
enum Size
{
SMALL = 1,
MEDIUM = 5,
LARGE = 10
}
class Test {
int mysize1 = (int)Size.SMALL;
int mysize2 = (int)global::Size.MEDIUM;
}

How to call an extension method without using

using System;
class Runner
{
static void Main()
{
A a = new A();
// how to say a.PrintStuff() without a 'using'
Console.Read();
}
}
class A { }
namespace ExtensionMethod
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
How would I call the extension method without a 'using'? And not ExtensionMethod.AExtensions.PrintStuff(a), since that doesn't make use of extension method.
It is possible to directly call your extension like so since it is simply a static method, passing the instance it will act on as the first this parameter:
A a = new A();
ExtensionMethod.AExtensions.PrintStuff(a);
This might be confusing to other developers who happen across this code if you followed this pattern for more commonly used extension methods. It would also make chaining extension calls such as LINQ appear more like a functional language because you would be nesting each call instead of chaining them.
that is possible if Extension Method and class A in same namespace,
If you have to use different namespaces then you have to use using, i don't think there is a way to do this without using. But you may reduce the number of using by putting all the extensions in one namespace like for Linq (System.Linq.Extensions)
Note : You can remove the namespace for Extension methods, then it will make them globally available
It needs the using to know where the function lives.
One example of this in Linq. Without the System.Linq using - you won't have linq enabled for any of your IEnumerable<T>'s
However, you can define the extension method in the same namespace as the caller to avoid putting in a using. This approach will however not work if it's needed in many namespaces
This makes me feel dirty, but you can put your extension methods in the System namespace.
This namespace is included by default in your question
using System;
class Runner
{
static void Main()
{
A a = new A();
// how to say a.PrintStuff() without a 'using'
Console.Read();
}
}
class A { }
namespace System
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
Ruminations on the creation of extension methods for a type ExtendableType:
Name the class ExtendableTypeExtensions
Declare the extension class partial so that clients can add extension methods following the same pattern; and
Put the extension methods in the same namespace as the base type
unless you have a very good reason to follow a model like that of LINQ:
A substantial family of extension methods,
That all apply to multiple base classes.
As of C# v6.0 (circa 2015) you can use using static to access a specific class's static members without including it's whole namespace.
An example, using your code, would be:
using System;
using static ExtensionMethod.AExtensions;
class Runner
{
static void Main()
{
A a = new A();
a.PrintStuff();
Console.Read();
}
}
class A { }
namespace ExtensionMethod
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
You can add extensions method without namespace.
This will affect the whole systems which is not recommended.
public static class StringExtensions
{
public static void HelloWorld(this String s)
{
Console.Write("Hello World");
}
}
string str = "s";
str.HelloWorld();
In our projects extensions are placed in the same namespace as class extension for. Your example:
A.cs:
using System;
namespace ANamespace
{
class A { }
}
AExtensions.cs:
namespace ANamespace
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
Now when you add using for ANamespace for using the A class, all extensions for A class will be included too.

Error - the program is not giving output while calling the print method

This is a simple program I created - one table class, one main class. In the table class I created a print method which simply outputs my name. From the main class I am calling the print method but not getting the output.
namespace ConsoleApplication3
{
class table
{
public static void print()
{
Console.WriteLine("My name is prithvi-raj chouhan");
}
}
class Program
{
public static void Main()
{
table t = new table();
t.print(); // Error the program is not giving output while calling the print method
}
}
}
Since the function you are calling is static.
Use this syntax
public static void Main()
{
table.print();
}
Quote from MSDN:-
A static method, field, property, or event is callable on a class even
when no instance of the class has been created. If any instances of
the class are created, they cannot be used to access the static
member. Only one copy of static fields and events exists, and static
methods and properties can only access static fields and static
events. Static members are often used to represent data or
calculations that do not change in response to object state; for
instance, a math library might contain static methods for calculating
sine and cosine.
print is a static method, so call it as a static method:
public static void Main()
{
table.print();
}
try this:
class Program
{
public static void Main()
{
table.Print();
}
}
Print(); is a static method so you dont need to instantiate a new Table object in order to access it's methods
You are calling print() as an instance method but it is static. Try to remove the static keyword from the method.
Try to add a Console.ReadLine(); after table.print();.
UPDATE:
Missed the part with static, now corrected.

OOP - how to call a function before the class constructor

I have a job interview tomorrow and I'm trying to answer this question:
There is a class named C and method m in this class, this class have also empty constructor:
Main ()
{
C c = new c();
}
Class C {
public c {
//empty constructor
}
public m {
//does something - doesnt mind
}
}
And what you have to do is to change the code so that in a creation of an instance class C, the method m would be called before the class constructor.
You have to do this without changing the main (edit only the class code).
Thanks in advance!
Like the other answers have said, you can make the method static. But then you need to explicitly call it. If you make a static class constructor, that will get called once automatically (you don't need to call it), the first time the class is referenced (like when you construct the first instance). You can't exactly control when it executes, but it will execute before the first instance is constructed. Based on the way they've worded the question (you can't change the Main method), I think static class constructor is the answer they're looking for.
http://msdn.microsoft.com/en-us/library/k9x6w0hc%28v=vs.80%29.aspx
Static constructors have the following properties:
A static constructor does not take access modifiers or have parameters.
A static constructor is called automatically to initialize the class before the first instance is created or any static members are
referenced.
A static constructor cannot be called directly.
The user has no control on when the static constructor is executed in the program.
Java doesn't have static class constructors, but they do have static initialization blocks..
static {
// code in here
}
To call a class's method before its constructor gets called you either have to turn this method into static so you don't need an instance of that class to call it, or (in C#) you can use FormatterServices.GetUninitializedObject Method to get an instance of your class without running the constructor (which of course may not be a wise thing to do).
In JAVA:
make method static and call your method in static block.
class C{
static{
m();
}
public C() {
System.out.println("Constructor Called..");
}
public static void m() {
System.out.println("m() is called.");
}
}
Main call
public static void main(String[] args) {
new C();
}
In both Java and C# you can use, base class constructors, static constructors (Edit: static initializer block in Java), and field initializers, to call code before the C class's constructor executes without modifying Main.
An example using a field initializer block in Java:
class C {
{ m(); }
public C() {
System.out.println("cons");
}
public void m() {
System.out.println("m");
}
}
This prints "m", then "cons". Note that m is called every time a C is constructed. A static initializer block would only be called once for the JVM.
Its basic OOP. You have to make a public static method and call it. That method can then call the constructor, or you can call the constructor directly from main.
Before you call the constructor, the object don't exist, therefore no instance methods exist, therefore nothing tied to the instance/object can be called. The only things that do exist before the constructor is called is the static methods.
Following way seems to achieve what is required. Without using static methods/variables
namespace FnCallBeforeConstructor
{
static void Main(string[] args)
{
MyClass s = new MyClass();
Console.ReadKey();
}
partial class MyClass: Master
{
public override void Func()
{
Console.WriteLine("I am a function");
}
public MyClass()
: base()
{
Console.WriteLine("I am a constructor");
}
}
class Master
{
public virtual void Func() { Console.WriteLine("Not called"); }
public Master()
{
Func();
}
}
}
Output is:
I am a function
I am a constructor

Methods inside namespace c#

Is there any way to call a function that is inside of a namespace without declaring the class inside c#.
For Example, if I had 2 methods that are the exact same and should be used in all of my C# projects, is there any way to just take those functions and make it into a dll and just say 'Using myTwoMethods' on top and start using the methods without declaring the class?
Right now, I do:
MyClass.MyMethod();
I want to do:
MyMethod();
Thanks,
Rohit
Update for 2015:
No you cannot create "free functions" in C#, but starting with C# 6 you'll be able to call static functions without mentioning the class name. C# 6 will have the "using static" feature allowing this syntax:
static class MyClass {
public static void MyMethod();
}
SomeOtherFile.cs:
using static MyClass;
void SomeMethod() {
MyMethod();
}
You can't declare methods outside of a class, but you can do this using a static helper class in a Class Library Project.
public static class HelperClass
{
public static void HelperMethod() {
// ...
}
}
Usage (after adding a reference to your Class Library).
HelperClass.HelperMethod();
Depends on what type of method we are talking, you could look into extension methods:
http://msdn.microsoft.com/en-us/library/bb383977.aspx
This allows you to easily add extra functionality to existing objects.
Following on from the suggestion to use extension methods, you could make the method an extension method off of System.Object, from which all classes derive. I would not advocate this, but pertaining to your question this may be an answer.
namespace SomeNamespace
{
public static class Extensions
{
public static void MyMethod(this System.Object o)
{
// Do something here.
}
}
}
You could now write code like MyMethod(); anywhere you have a using SomeNamespace;, unless you are in a static method (then you would have to do Extensions.MyMethod(null)).

Categories