Click here to Skip to main content
15,890,897 members
Home / Discussions / C#
   

C#

 
QuestionTemporal Nulls in C# Pin
RandyBuchholz27-Oct-20 3:18
RandyBuchholz27-Oct-20 3:18 
AnswerRe: Temporal Nulls in C# Pin
Gerry Schmitz27-Oct-20 4:26
mveGerry Schmitz27-Oct-20 4:26 
AnswerRe: Temporal Nulls in C# Pin
Pete O'Hanlon27-Oct-20 9:55
mvePete O'Hanlon27-Oct-20 9:55 
AnswerRe: Temporal Nulls in C# Pin
BillWoodruff27-Oct-20 10:17
professionalBillWoodruff27-Oct-20 10:17 
QuestionProgramming C # Pin
Immanuel Hitila26-Oct-20 12:10
Immanuel Hitila26-Oct-20 12:10 
AnswerRe: Programming C # Pin
Pete O'Hanlon26-Oct-20 12:16
mvePete O'Hanlon26-Oct-20 12:16 
GeneralRe: Programming C # Pin
Immanuel Hitila26-Oct-20 12:21
Immanuel Hitila26-Oct-20 12:21 
AnswerRe: Programming C # Pin
Richard Andrew x6426-Oct-20 14:06
professionalRichard Andrew x6426-Oct-20 14:06 
AnswerRe: Programming C # Pin
OriginalGriff26-Oct-20 21:14
mveOriginalGriff26-Oct-20 21:14 
QuestionHow to launch a form only once Pin
Ismael_199926-Oct-20 7:57
Ismael_199926-Oct-20 7:57 
AnswerRe: How to launch a form only once Pin
OriginalGriff26-Oct-20 8:35
mveOriginalGriff26-Oct-20 8:35 
GeneralRe: How to launch a form only once Pin
Ismael_199926-Oct-20 10:09
Ismael_199926-Oct-20 10:09 
GeneralRe: How to launch a form only once Pin
OriginalGriff26-Oct-20 21:15
mveOriginalGriff26-Oct-20 21:15 
GeneralRe: How to launch a form only once Pin
Ismael_199927-Oct-20 10:40
Ismael_199927-Oct-20 10:40 
GeneralRe: How to launch a form only once Pin
OriginalGriff27-Oct-20 10:42
mveOriginalGriff27-Oct-20 10:42 
AnswerRe: How to launch a form only once Pin
Gerry Schmitz26-Oct-20 14:50
mveGerry Schmitz26-Oct-20 14:50 
AnswerRe: How to launch a form only once Pin
Richard Deeming26-Oct-20 22:10
mveRichard Deeming26-Oct-20 22:10 
QuestionDesign an elegant solution for choosing a class whose method gets called Pin
Member 1497555126-Oct-20 6:13
Member 1497555126-Oct-20 6:13 
AnswerRe: Design an elegant solution for choosing a class whose method gets called Pin
Gerry Schmitz26-Oct-20 14:58
mveGerry Schmitz26-Oct-20 14:58 
Questionan apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff25-Oct-20 22:18
professionalBillWoodruff25-Oct-20 22:18 
I am baffled by how the code in the 'Human and 'Cat classes can successfully call a generic static method (SetCache<T>(T value)) without supplying the usual <Type> adornment.

Here's a sample usage of the code:
BeingCache cache = BeingCache.CacheInstance;

Human ahuman = new Human("joe", "male");
Cat acat = new Cat("Meow", true);

IBeing b1 = cache[1]; // works

IBeing<Cat> b2 = (IBeing<Cat>) b1; // works

Cat cat = b2.IBInstance; // works

bool st0 = b2 is Cat; // false

bool st1 = b2 is Being<Cat>; // true

bool st2 = b2 is IBeing<Cat>; // true

Cat hepcat = BeingCache.GetBeing<Cat>(cache[1]); // works

IBeing<Cat> b3 = (IBeing<Cat>) cache[1]; // works
The example: note that classes 'Human and 'Cat do not inherit from anything.
using System;
using System.Collections.Generic;

namespace InterfaceInheritance_2020
{
    public class Human
    {
        public Human(string name, string gender)
        {
            Name = name;
            Gender = gender;

            BeingCache.SetCache(this); //  :wtf: 
        }

        public string Gender{ get;}
        public string Name { get; }
    }

    public class Cat
    {
        public Cat(string name, bool haswhiskers = true)
        {
            Name = name;
            HasWhiskers = haswhiskers;

            BeingCache.SetCache(this); //  :wtf: 
        }

        public bool HasWhiskers { get; }
        public string Name { get; }
    }

    public interface IBeing
    {
        Type BeingType { set; get; }
    }

    public interface IBeing<T> : IBeing
    {
        T IBInstance { set; get; }
    }

    public class Being<T> : IBeing<T>
    {
        public Being(T content)
        {
            IBInstance = content;
            BeingType = typeof(IBeing<T>);
        }

        public Type BeingType { get; set; }
        public T IBInstance { get; set; }
    }

    //singleton 
    public sealed class BeingCache : List<IBeing>
    {
        private BeingCache()
        {
        }

        public static BeingCache CacheInstance{ get; } = new BeingCache();

        public static void SetCache<T>(T value)
        {
            CacheInstance.AddBeing(new Being<T>(value), typeof(T));
        }

        public void AddBeing(IBeing being, Type btype)
        {
            being.BeingType = btype;
            CacheInstance.Add(being);
        }

        public static T GetBeing<T>(IBeing being)
        {
            return (T) ((IBeing<T>) being).IBInstance;
        }
    }
}

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

AnswerRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox25-Oct-20 22:58
professionalpkfox25-Oct-20 22:58 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff25-Oct-20 23:15
professionalBillWoodruff25-Oct-20 23:15 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox25-Oct-20 23:59
professionalpkfox25-Oct-20 23:59 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 2:13
professionalBillWoodruff26-Oct-20 2:13 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox26-Oct-20 2:48
professionalpkfox26-Oct-20 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.