GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 401 of 436    Print  
C# sealed variables
I have taken a windows application and a button control in it.
I want to declare a variable as a sealed and use that variable with in same class as we cannot use in other class.

class test
{
public sealed int x = 5;
public void abc()
{

MessageBox.Show(x.ToString());

}
}
class best :test
{
// public sealed int y = 10;// error..the modifer sealed is Not valid for this item
//private sealed int y = 10; // ""
//protected sealed int y = 10; // ""
public void print()
{
//MessageBox.Show(y.ToString()); // even when Iam commenting this line I am getting the same above error
//MessageBox.Show(x.ToString());
}

}
private void button1_Click(object sender, EventArgs e)
{
test t = new test();
t.abc();
best b = new best();
b.print();
}

Even Iam commenting the derived class also Iam getting the same error.
Error The modifier 'sealed' is not valid for this item



  
Total Answers and Comments: 8 Last Update: July 15, 2008     Asked by: aarruunnaa 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
December 06, 2007 04:42:31   #1  
star2k279 Member Since: December 2007   Contribution: 2    

RE: C# sealed variables

"sealed" word can only be used with the class to prevent it from inheritance i.e. no class can be drived from a sealed class.

"sealed cannot be used with the variables.

you can declare the variable as private to restrict the usage inside its own class.

 
Is this answer useful? Yes | No
December 18, 2007 02:51:57   #2  
chauhansumit Member Since: December 2007   Contribution: 2    

RE: C# sealed variables
sealed can be used for methods only....those you dont want to be overridden by a derived class
 
Is this answer useful? Yes | No
December 18, 2007 23:43:14   #3  
star2k279 Member Since: December 2007   Contribution: 2    

RE: C# sealed variables

As far as i tested it sealed word cannot be used with methods. it could be used with the class name only.

If you are positive please give me an example and i will be thankful to you to add a bit.

 
Is this answer useful? Yes | No
January 29, 2008 03:47:19   #4  
iramley Member Since: January 2008   Contribution: 2    

RE: C# sealed variables

You can use sealing to limit the ways in which others can extend the framework.

When you seal a class other classes cannot inherit from it
.

When you seal a member derived classes cannot override the implementation of the member
.

You should not seal types and members by default.

Sealing prevents customization of library types and members and impacts the perception of usability for other developers
.

In addition extensibility is one of the fundamental benefits of using an OO framework. So you should carefully weigh decisions that restrict this benefit.

 
Is this answer useful? Yes | No
February 03, 2008 09:03:21   #5  
sarnam Member Since: February 2007   Contribution: 14    

RE: C# sealed variables
Variables can not be declred as sealed this is valid only for class that means if a class declared as a sealed class we can't inherit that class.
Thanks.

 
Is this answer useful? Yes | No
May 27, 2008 05:26:47   #6  
nikhilrawat23 Member Since: May 2008   Contribution: 15    

RE: C# sealed variables
sealed word is always use with class .and when u make any class sealed thn it is impossible to access that class member.
second thing u could not make any method sealed in sealed class.
bt u can declare method sealed after overidding it.by this u make that method further in accessible.

 
Is this answer useful? Yes | No
July 02, 2008 11:33:32   #7  
John Jiang Member Since: June 2008   Contribution: 8    

RE: C# sealed variables
sealed can be used for class and method. when used for method also the method has to use override modifier.
 
Is this answer useful? Yes | No
July 15, 2008 05:12:53   #8  
ramppm Member Since: July 2008   Contribution: 1    

RE: C# sealed variables
Classes can only be sealed. Methods Properties and variables cannot be sealed.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape