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 9252327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:55:02+00:00 2026-06-18T10:55:02+00:00

I have a class in my program called SortableObservableCollection<T> that inherits from ObservableCollection<T> .

  • 0

I have a class in my program called SortableObservableCollection<T> that inherits from ObservableCollection<T>. It has a function called Sort:

public void Sort<TKey>( Func<T, TKey> keySelector, int skip = 0 ) 
{
   // . . .
}

I want to declare a property called Key1 of type Func<T, TKey>. I have tried:

public Func<T, TKey> Key1<T, TKey> { get; set; }

But I am getting a syntax error on the left curly brace. The error indicates that the compiler is expecting a left parentheses. I’ve tried making it a field declaration:

public Func<T, TKey> Key1<T, TKey>;

But then the compiler gives me the same message on the semi-colon.

What’s the right syntax for declaring this property?

  • 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-18T10:55:03+00:00Added an answer on June 18, 2026 at 10:55 am

    While everyone’s answers will work, they weren’t exactly where I was trying to get with this. Originally, I had a method of the class called Sort:

    public void Sort<TKey>( Func<T, TKey> keySelector, . . . ) {
        // . . .
    }
    

    This worked well, but then I found a bug in my UI that was related to calling the Sort method more than once. The sort itself worked, that wasn’t the problem. The bug is convoluted and difficult to explain, and I’d rather not go into it. But the easiest solution was to make sure that the collection was sorted only once, on the correct key. The thing is that the keys differed depending on the particular drop down that the collection was bound to. So I needed a way to encapsulate the sort criteria in the collection so it would do the proper sort the first and only time.

    As I already had the Sort method mentioned above, I figured the easiest thing to do was to put the keySelector parameter to it into a property of the class. It turns out that the language doesn’t let you do that, and now I know and understand why.

    What I ended up doing to get this to work was replace the Func property with a reference to an object that implements IComparer<T>. I have no trouble declaring this property and it’s easy to declare a class that implements it for each type passed as the T parameter in a collection declaration. I just initialize the property after allocating the collection object and I rewrote the Sort method so it looks like this:

    public void Sort() {
        if ( Comparer != null ) {
            InternalSort( Items.Skip( RowsToSkip ).OrderBy( item => item, Comparer ) );
        } else { 
            InternalSort( Items.Skip( RowsToSkip ).OrderBy( item => item ) );
        }
    }
    

    RowsToSkip is an int property of the class which specifies how many rows at the start of the collection are to be skipped. This lets me add an entry like “Pick a choice” at the start of the list and not have the sort move it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called ModelView which inherits from NSOpenGLView. When my program runs
I have the following code: import java.lang.*; public class Program { public static void
I have these lines of code. class Program { public delegate void printer(); public
I have a class that can be called either from a GUI or form
I have a custom authorize program called CustomAuthorize that inherits AuthorizeAttribute that simply restricts
I have a class called Program which hold a grid. It has two methods
I have a C++ program called myArchive that calls routines from a static C
I have below code: class Program { static void Main(string[] args) { Task[] tasks
I have the following code: class Program { static void Main(string[] args) { new
I have a little console C# program like Class Program { static void main(string

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.