Cannot specify constructor arguments in declaration in C Sharp - c#

This is the Question I am trying to solve in C Sharp.
I am getting an error:
Error Expected ; or = (cannot specify constructor arguments in declaration)
Can anyone help me to solve this or guide me to solve this?
namespace program
{
public class Integer
{
private int intvar;
public Integer()
{
intvar = 0;
}
public Integer(int x)
{
intvar = x;
}
public void display()
{
Console.Write(intvar);
Console.Write("\n");
}
public void add(Integer x, Integer y)
{
intvar = x.intvar + y.intvar;
}
}
class Program
{
static void Main(string[] args)
{
Integer a(5),b(45);
Integer c;
c.add(a,b);
c.display();
Console.ReadLine();
}
}
}

You cannot create objects like that in C#. Im assuming you come from C++ where this syntax is possible.
In C# you have to create objects using new:
Integer foo = new Integer(45);

To create a new instance of a type, you have to invoke one of the constructors of that type using the new operator. For example:
class Program
{
static void Main(string[] args)
{
var a = new Integer(5);
var b = new Integer(45);
var c = new Integer(); //result instance
c.add(a, b);
c.display();
Console.ReadLine();
}
}

Related

C# - Unable to invoke Action Delegate

I am not able to invoke a delegate when passing an anonymous function.
Here is the code.
public class MethodCollection
{
public static void Print(Action<int, int> printNumbers)
{
}
}
public class Program
{
static void Main(string[] args)
{
MethodCollection.Print((p, q) => { p = q = 3; Console.WriteLine(p + q); });
Console.ReadLine();
}
}
The output is a blank screen.
The program does not print the expected output, i.e. 6.
Just call printNumbers.Invoke(3,3); or so in your Print method.
You should remove the p = q = 3; in your action because as Jon mentioned this will make your action ignore the numbers you pass the action.
Or it can be something like this... and I think you should see delegates basics first
helpful link to learn basics of delegates
public class MethodCollection
{
public static void Print(Action<int, int> printNumbers)
{
printNumbers.Invoke(0,0);
}
}
public class Program
{
static void Main(string[] args)
{
MethodCollection.Print((p, q) => { Console.WriteLine((p=3) + (q=3)); });
Console.ReadLine();
}
}

Storing an unknown function from another class to execute later on itself

I have a class that parameters there can change, i'm creating a class that sorts groups of the first class, by the given parameter it needed to be sorted by.
class class1
{
private int p1;
private int p2;
//and etc depending on the need of it
}
for each parameter there is a get function.
i tried to do the following:
class class2<T>
{
private Func<T> getValue;
public class2(Func<T> getValue)
{
this.getValue = getValue;
}
public void Add(class1 cs)
{
//here is where i want to execute getValue on cs itself to compare them to one another.
}
}
I have tried doing the following in the main class:
class Program
{
static void Main(string[] args)
{
class1 c1 = new class1(1, 2);
class2<int> c2 = new class2<int>(c1.getp1)
}
}
but it only stores the the function for c1, and i cant execute it as i need it.
Is there a way to do it?
Thanks ahead for the help and sorry for the dumb explanation.
Instead of Func<T>, use Func<class1,T> (msdn docs).
class class2<T>
{
private Func<class1, T> getValue;
public class2(Func<class1, T> getValue)
{
this.getValue = getValue;
}
public void Add(class1 cs)
{
// Something like this
var val = this.getValue(class1);
// do something with val
}
}
class Program
{
static void Main(string[] args)
{
class1 c1 = new class1(1, 2);
// create the Func as a lambda and pass to the constructor.
class2<int> c2 = new class2<int>(c => c.getp1)
}
}
I haven't ran/compiled this.
Your question is a little confusing, but I think what you want is this:
class class1
{
private int _p1;
private int _p2;
//and etc depending on the need of it
public class1(int p1, int p2)
{
_p1 = p1;
_p2 = p2;
}
public int getp1()
{
return 1;
}
}
class class2<T>
{
private Func<T> getValue;
public class2(Func<T> getValue)
{
this.getValue = getValue;
}
public void Add(class1 cs)
{
//here is where i want to execute getValue on cs itself to compare them to one another.
getValue();
}
}
class Program
{
static void Main(string[] args)
{
class1 c1 = new class1(1, 2);
Func<int> getp1 = c1.getp1;
class2<int> c2 = new class2<int>(getp1);
c2.Add(c1);
}
}

while overidding base methods, why default arguments are not working well? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# optional parameters on overridden methods
using System;
namespace Apple
{
class A
{
public virtual void Func(int a=4){
Console.WriteLine(" A Class: "+a);
}
}
class B : A
{
public override void Func(int a = 12)
{
Console.WriteLine(" B Class: "+ a);
}
}
public class Program
{
static void Main(string[] args)
{
A ob = new B();
ob.Func();
Console.ReadLine();
}
}
}
// Output: B Class: 4
Default parameters are filled at compile time, and the code references a variable ob through the base class. The virtual method override works at run time, as expected. You could achieve the desired effect by using method overload:
class A
{
public void Func(int value)
{
}
public virtual void Func()
{
Func(4);
}
}
class B: A
{
public override void Func()
{
Func(12);
}
}
The compiler places the the default parameter value based on the type of the object and is done during the compile time.
Hence the compiled code would look like:
using System;
namespace Apple
{
public class Program
{
private static void Main(string[] args)
{
A ob = new B();
ob.Func(4);
Console.ReadLine();
}
}
}
You could get the desired result by doing this:
public class Program
{
static void Main(string[] args)
{
A ob = new B();
((B)ob).Func();
Console.ReadLine();
}
}
Because you creating instance of Class A which is referring to the address of class B.
A ob = new B();
Since the instance is of class A, the method you calling is pointing to method in class A.
You can check this by putting debug and then execute the code.
instead if you create instance of class B ie
B ob = new B();
it will call the method Fun() from class B and will display output as
" B Class: 12"
the default parameter value is Static binding.

C# compile time error if Int?

I'm trying to recreate this in C#.
The problem i get is if i use constructors i MUST use new MyInt which i DO NOT WANT (to verbose). The way around it is to use the implicit/explicit operators. However they MUST be public... How the heck do i implement this in feature in C#?
The short question is i'd like to pass byte/short into a function but not int. Passing int should get me a compile error. I know i can easily get runtime with a public implicit int operator. The code below shows that int is automatically converted to char
Running sample shows true
using System;
class Program
{
public static void Write(MyInt v)
{
Console.WriteLine("{0}", v.v is byte);
}
static void Main(string[] args)
{
Write(2);
}
}
public struct MyInt
{
public object v;
public MyInt(byte vv) { v = vv; }
public MyInt(short vv) { v = vv; }
public MyInt(byte[] vv) { v = vv; }
public static implicit operator MyInt(byte vv) { return new MyInt { v = vv }; }
//public static extern implicit operator MyInt(int vv);
}
Heres more code which is useless. It implements MyInt2/MyInt which isn't required in the C++ solution
Just declare that your function takes short. Byte would be implicitly converted to short, but there is no implicit conversion from int to short, so int just won't pass
public class Class1
{
public static void Aaa(short a)
{
}
public void Bbb()
{
int i = 5;
byte b = 1;
short c = 1;
Class1.Aaa(i); // Gives error
Class1.Aaa(b); // Ok
Class1.Aaa(c); // ok
}
}

Is there an equivalent to static of C in C#?

In C I can do
void foo() {
static int c = 0;
printf("%d,", c);
c ++;
}
foo();
foo();
foo();
foo();
it should print 0,1,2,3
Is there an equivalent in C#?
While some have suggested as static member variable, this is not the same due to visibility. As an alternative to the answer by aquinas, if closures are accepted, then this can be done:
(Note that Foo is a a property and not a method and that c is "per instance".)
class F {
public readonly Action Foo;
public F () {
int c = 0; // closured
Foo = () => {
Console.WriteLine(c);
c++;
};
}
}
var f = new F();
f.Foo(); // 0
f.Foo(); // 1
However, C# has no direct equivalent to a static variable in C.
Happy coding.
Something like:
class C
{
private static int c = 0;
public void foo()
{
Console.WriteLine(c);
c++;
}
}
No there is no way to achieve the same behaviour as the static c function variable ...
There are no globals in C#, however, you can create a static field within your class.
public class Foo{
private static int c = 0;
void Bar(){
Console.WriteLine(c++);
}
}
You can't do it at a method level. The closest you can do at a method level is something like this, and this isn't really that close. In particular, it only works if you have a reference to the enumerator. If someone else calls this method, they won't see your changes.
class Program {
static IEnumerable<int> Foo() {
int c = 0;
while (true) {
c++;
yield return c;
}
}
static void Main(string[] args) {
var x = Foo().GetEnumerator();
Console.WriteLine(x.Current); //0
x.MoveNext();
Console.WriteLine(x.Current); //1
x.MoveNext();
Console.WriteLine(x.Current); //2
Console.ReadLine();
}
}
What interesting is that VB.NET does support static local variables: http://weblogs.asp.net/psteele/pages/7717.aspx. As this page notes, .NET itself doesn't support this, but the VB.NET compiler fakes it by adding a static class level variable.

Categories