C# console app closes itself after I run it [duplicate] - c#

This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 5 years ago.
When I run this simple application it closes itself without reason. How to prevent that?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = a * b;
Console.WriteLine(c);
}
}
}

Just add Console.ReadKey(); after printing the value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = a * b;
Console.WriteLine(c);
Console.ReadKey();
}
}
}

Related

Trying to call a namespace in another class in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Namespaces
{
public Namespaces()
{
}
}
namespace NameSpace1
{
public class MyClass
{
public static int add(int a, int b)
{
return a + b;
}
}
namespace NameSpace2
{
public class MyClass
{ public static int add (int a, int b, int c)
{ return a + b + c; }
}
}
}
}`
```
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace2.MyClass.add(1, 2, 3); //Attempting to call Namespace 2 from here
}
}
Previously, I tried rebuilding, checking for spelling mistakes or differences between my namespaces(didn't find any), checking for formatting mistakes or differences between the two original Namespace methods(the only one I could find really didn't make any difference).
NameSpace2 is within NameSpace1. Change it to int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3); and it should work:
namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3);
}
}
}
(Fiddle).

Why my Console.WriteLine(); does not work on referenced external project?

I'm getting an error on Console.WriteLine();
HelperLibrary Project I already reference it to my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathClasses
{
public class MyMaths
{
public void Divisibility(int number, int divby)
{
int divisibility = number % divby;
if (divisibility == 0)
{
Console.WriteLine($"{number} is divisible by {divby}");
}
else
{
Console.WriteLine($"{number} is not divisible by {divby}");
}
}
}
}
and here is my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathClasses;
using MiscClasses;
using PrinterClasses;
namespace ChallengeOne
{
class Program
{
static void Main(string[] args)
{
MyMaths myMaths = new MyMaths();
int number = Convert.ToInt32(Console.ReadLine());
int divby = Convert.ToInt32(Console.ReadLine());
myMaths.Divisibility(number, divby);
}
}
}
Note:
my instructor says that my 2nd project doesn't recognize my using System;
the error says
The name 'Console' does not exist in the current context

Why is my C# method not visible when I attempt to call from VBA

I have two methods that I am trying to call from VBA throught the COM interop. The only issue is that the ArraySort method is giving me an error of: "Member or method not available."
The strange thing is the RemoveDuplicates function is able to be called.
Can anyone help? Source code is bellow.
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
namespace ExpandExcel
{
[ProgId("ExpandExcelStandard")]
[ComVisible(true)]
public class ExpandExcelStandard
{
public object[] RemoveDuplicates(object[] arr) => new HashSet<object>(arr).ToArray();
public object[] ArraySort(object[] arr, int iIndex = 0, int iLength = 0) => Quicksort(arr, iIndex, iLength);
private object[] Quicksort(object[] arr, int iIndex, int iLength)
{
Array.Sort(arr, iIndex, iLength);
return arr;
}
}
}

Bankteller console application

I'm making a simple application to simulate the bankteller problem.
What I'm trying to simulate is:
You have 4 counters in a store. 1 counter is open. Customers start coming in and enter the line for the first counter.
When the fourth customer enters the line for the first counter, another counter should open. The line should be equally divided between the 2 counters.When the customer at the second counter is helped and no new customers enter the line, the counter should close. Basically 4 is too many.
I can't seem to figure it out. I know I need to use a queue. But how? Could someone give me an example in console application? Preferable C#.
Thanks in advance.
Here is what I tried so far,
register class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Register
{
private int customerCount;
public Queue<Customer> Line = new Queue<Customer>();
public Register()
{
customerCount = 2;
}
public Register(int customerCount)
{
this.customerCount = customerCount;
}
public int getCustomers()
{
return customerCount;
}
}
}
Customer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Customer
{
private int checkoutTime;
public Customer()
{
checkoutTime = 3;
}
public Customer(int checkoutTime)
{
this.checkoutTime = checkoutTime;
}
public int GetCheckoutTime()
{
return checkoutTime;
}
}
}
Register manager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class RegisterManager
{
public List<Register> registers = new List<Register>();
Register r1 = new Register();
Customer c1 = new Customer();
public RegisterManager()
{
registers.Add(r1);
}
public void ManageCustomers()
{
for (int i = 0; i < registers.Count; i++)
{
registers.Insert(i, new Register());
if (i / 4 <= registers..Line.Count)
{
}
}
}
}
}

singleton implementation problem in C#

--ConsoleApplication 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class MsgService
{
private static CreateConnectionToA _instanceA;
private static CreateConnectionToB _instanceB;
protected MsgService()
{
}
public static MsgService GetInstanceA(string paramA, string paramB)
{
if (_instanceA != null)
{
return _instanceA;
}
return _instanceA = new CreateConnectionToA("p1","p2");
}
public static MsgService GetInstanceB(string paramA, string paramB)
{
if (_instanceB != null)
{
return _instanceB;
}
return _instanceB = new CreateConnectionToB("p1", "p2");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToB : MsgService
{
public CreateConnectionToB(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToA : MsgService
{
public CreateConnectionToA(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
--ConsoleApplication 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press");
Console.Read();
ConsoleApplication2.MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
I am trying to Make simgleton implementation but something is wrong with my approach. It always creates new instance of _instanceA and _instanceB from each console application.
Can someone please point me out what needs to be done here?
You would need named Mutexes for inter-process synchronization.
Sharing an object instance between two applications is kinda hard, since they run in separate appdomains, by default. To accomplish what I think you're trying to do, you'll need to either
marshal across appdomain boundaries with, or
run the two processes in a shared appdomain. Write a 3rd process — a shell — that's responsible for spawning/hosting the other two processes in a shared appdomain.
http://www.codeproject.com/KB/dotnet/AppDomainMemImprovement.aspx
Sharing data between AppDomains

Categories