Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:17:31+00:00 2026-06-13T23:17:31+00:00

I have created a generic type to act as a pointer so that I

  • 0

I have created a generic type to act as a pointer so that I can pass by reference. (Perhaps there is a much more simple way of doing this but I want to stress that I am doing this to learn more about generics and passing by reference, not the most efficient way of completing the task, if that makes sense.)

Here is the code I wrote for the generic type

class GenericPointer<T> {
    public T item;
    public void setItem(T i){ item = i; }
    public T getItem(){ return item; }
}

In my program I have created an instance of this type called ‘intPointer’. The value 143 is arbitrary.

GenericPointer<int> intPointer = new GenericPointer<int>();
intPointer.setItem(143);
Console.WriteLine(intPointer.getItem());

The above code runs properly, setting and returning the value 143.

I now want to pass this ‘intPointer’ to a method that increments it and then prints the value again.

So I wrote a method called addone()

public void addone(int i) { i ++; }

Now I want to make the following calls (remembering that I already set the value to 143):

Console.WriteLine(intPointer.getItem());
addone(intPointer);
Console.WriteLine(intPointer.getItem());

What I was expecting to see was 143 then 144 however I get the following errors:

The best overloaded method match for 'Notes.Program.addone(int)' has some invalid arguments

and:

cannot convert from 'Notes.GenericPointer<int>' to 'int'

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:17:32+00:00Added an answer on June 13, 2026 at 11:17 pm

    I’ll begin by correcting some of your terminology: you’re not using pointers. C# does support pointers, but using the unsafe keyword, and they are real pointers (as in, integer memory addresses you can directly manipulate). The code you written is just an example of a boxed object.

    .NET supports boxing already, by casting to Object; however it isn’t recommended nor needed because the ref keyword solves the problem you’re trying to “fix”.

    Use the ref keyword to describe a value-type parameter that should be passed by-reference instead of by-value. All other semantics remain the same, like so:

    void Foo() {
        int x = 123;
        Bar(ref x);
        Console.Write( x ); // prints "124".
    }
    void Bar(ref int x) {
        x++;
    }
    

    I have a few other notes:

    1. C# and .NET conventions dictate that all public members (methods, properties, fields, etc) should have TitleCase, not camelCase (i.e. ensure the first letter is capitalised).
    2. Trivial getter and setter methods are discouraged, used Properties instead (though I note you cannot use ref arguments with properties).
    3. You’re getting your error because the type of intPointer is not int, but your class GenericPointer<int>.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created the type IntPtr<T> to act like a generic pointer that c/c++ have:
I have created a simple project where everyone can create one or more Blog.
I have created a generic method that enables clients to specify both the concrete
I have a class User. I have created a generic CSV exporter that works
I have created a subclass of a generic list so that I could implement
I have created a generic behavior that encapsulates tcp/ip functionality. All the user of
I have created a universalrepository that takes the type passed to it and when
I've created a generic class that needs to instantiate its implementation type, so the
Hi i have created a Generic Array that works fine for Int,String, Float or
I have created a container for generic, weak-type data which is accessible through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.