473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

.Net Type Saftey violation? or just confusion...

Greetings all,

I'm currently reading the excellent applied .Net Framework programming
by Jeffrey Ritcher. In the Type Fundamentals chapter a brief description of
the System.Object methods is given.

Against the GetType member it says: This method is nonvirtual preventing
classes from overriding the method and lying about its type, violating type
safety.

This all seems to make prefect sense. But... you could 'Shadow' the GetType
function in a class and return a completely different type. Does this not
violate type safety? If not could someone please explain?

Many Thanks

Andy Read.
Jul 19 '05 #1
1 1398
> I'm currently reading the excellent applied .Net Framework programming
by Jeffrey Ritcher. In the Type Fundamentals chapter a brief description of
the System.Object methods is given.

Against the GetType member it says: This method is nonvirtual preventing
classes from overriding the method and lying about its type, violating type
safety.

This all seems to make prefect sense. But... you could 'Shadow' the GetType
function in a class and return a completely different type. Does this not
violate type safety? If not could someone please explain?


You could declare a new method with the same signature as GetType, but it
would only ever get called when you did:

MyType bleh = new MyType();
Type t = bleh.GetType();

If you were to do the following:

object bleh = new MyType();
Type t = bleh.GetType();

The GetType method inherited from object will be called.

If you have a variable declared as MyType, it's fairly obvious what type it
really is, and it doesn't really matter if you've hidden the GetType that was
inherited from object. The real GetType method will be called when an instance
of your class is referenced as an object.

--
Iain Simpson
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
1
by: Andy Read | last post by:
Greetings all, I'm currently reading the excellent applied .Net Framework programming by Jeffrey Ritcher. In the Type Fundamentals chapter a brief description of the System.Object methods is...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
5
by: Med | last post by:
Hi, Could someone please tell me if I undrestand the concept of "Thread Saftey" correctly in Multi-threaded web application (asp.net 2) using following examples: 1. Class A is NOT a thread...
4
nomad
by: nomad | last post by:
Hello Everyone... I have rewrite my code and I ran into some problems. I have two problems. 1. What does this mean "Type safety: The method add(Object) belongs to the raw type ArrayList....
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.