Clean code which version is correct? - c#

When the only code in there is for a class how would you code it adding public to the default class like so
namespace HW2_2_Spaceship
{
public class Spaceship //added public to the default class
{
static void Main(string[] args)
{
}
or
namespace HW2_1_Book
{
class Book
{
static void Main(string[] args)
{
}
public class Book // added a new class with in the default class
{

In general, each class should have their own file.
Main should be in Program.cs
There are usecases where you can use Inner classes, see Using Inner classes in C#.

//Program.cs, if u use visual studio then ensure you add
// the public access modifier yourself
namespace HW2_2_Spaceship
{
public class Program
{
public static void Main(string[] args)
{
//Do something here
}
}
}
//Book.cs, add the public modifier to the class
namespace HW2_2_Spaceship
{
public class Book
{
//add method and properties here
}
}

Related

Why use static methods in program.cs C#? [duplicate]

This question already has answers here:
When to use static methods
(24 answers)
Closed 2 years ago.
I always knew that I have to use static methods but I wonder why?
As you can see below I have to make "MigrateDatabase" Static
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
MigrateDatabase();
}
private static void MigrateDatabase()
{
//.....
}
}
}
Let's just be clear, the only reason MigrateDatabase has to be static in this case is because you're calling it from a static method (Main). If instead MigrateDatabase was an instance method on a class, you could instantiate that class and call it
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var migration = new Migration();
migration.MigrateDatabase();
}
}
public class Migration
{
private void MigrateDatabase()
{
//.....
}
}
}
You could also put it as a instance method on Program if you're instantiating an instance of that class
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var program = new Program();
program.MigrateDatabase();
}
private void MigrateDatabase()
{
//.....
}
}
}

Access class that attribute is affecting from said attribute

Whenever the TestAttribute attribute is called on a class, I would like to add that type to a list. How can I do that?
Example:
using System;
using System.Collections.Generic;
[AttributeUsage(AttributeTargets.Class)]
public class TestAttribute : Attribute {
public TestAttribute() {
// Add type to list
}
}
public static class TypeHolder {
public static List<Type> Types = new List<Type>();
}
[Test]
public static class MainClass {
public static void Main(string[] args) {
Console.WriteLine(TypeHolder.Types.Count);
}
}

Why object a is not in current context?

using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{ }
}
public class A
{
public void method1()
{ }
}
public class B : A
{
public void method2()
{ }
}
public class Test
{
A a = new A();
a.method1();
}
}
Please paste this code in VS and Please explain me why it is not in current context?
Inside a class is not the right place to call most functions:
public class Test
{
A a = new A();
a.method1();
}
Just put stuff into the main function, wich is there specifically for that part of the programming:
static void Main(string[] args)
{
A a = new A();
a.method1();
}
you can not have statements directly in class.
public class Test
{
A a = new A();
a.method1(); // this is not possible.
}
please modify your class as below:
public class Test
{
public void InvokeMethodOnA()
{
A a = new A();
a.method1();
}
}
or like this;
public class Test
{
A a = new A();
public void InvokeMethodOnA()
{
a.method1();
}
}
You can call method in body of method (sounds strange, but I don't have a better explanation).
In your case you mix a definition of the class Test with a context of function. I hope code with comments will be more descriptive:
public class Test
{
// this is not a local variable, this is a definition of field with initialization
A a = new A();
// you try call method on field, but in context of class definition, which is prohibited
a.method1();
}

How can be call the method from another class inherited

How can be call the method from another class inherited
let's see my code please:
class Program : classA
{
static void Main(string[] args)
{
// how i can call method ToDo without create an instance like below
//classA c = new classA();
//c.ToDo();
Console.ReadLine();
}
}
class Program2 : classB
{
static void Main(string[] args)
{
// how i can call method ToDo
//ToDo()
Console.ReadLine();
}
}
public abstract class classB
{
public void ToDo()
{
Console.WriteLine("classB");
}
}
public class classA
{
public void ToDo()
{
Console.WriteLine("classA");
}
}
how i can call the method in Either way, please help me.
There are a couple ways to do what you want to do (they're kind of similar or even the same).
One way is to create a class with a static method:
public class classA
{
public static void ToDo()
{
Console.WriteLine("classA");
}
}
then call it like:
classA.ToDo();
Another way is to add another static method to the class that contains Main:
class Program2 : classB
{
static void Main(string[] args)
{
ToDo()
Console.ReadLine();
}
static void Todo()
{
// do stuff here
}
}
If u want to call ToDo() function into [class Program : classA] and [class Program : classB]
Without creating Instance.
U have to Define ToDo() function as static, then u can call this method with class name in anywhere.
public static void ToDo(){}

How do I start an Activity from a non-Activity class?

How can I start an Activity from a non-Activity in Xamarin.Forms?
In the main startup project, MyApp.Droid, I have MyActivity.cs.
In the PCL project, MyApp.Plugin.Android, I have a non-activity class, MyClass.cs.
In MyClass.cs I need to run some code in MyActivity.cs.
How can I do that?
Thanks.
MyActivity.cs:
public void MyMethod()
{
// [Working Code...]
}
MyClass.cs:
public void OnSuccess
{
// What should I put here to call MyMethod?
}
something like this should do the trick! (I think, it been a while...)
MyClass.cs:
using System;
namespace MyClass {
public static class Program {
public static void MyMethod()
{
// [Working Code...]
}
}
}
MyActivity.cs:
using System;
using MyClass; //get your other file by the namespace
namespace MyActivity {
public static class Program {
public static void OnSuccess()
{
Program.MyMethod(); //[class name]..MyMethod();
}
}
}

Categories

Resources