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

Questions by aarruunnaa   answers by aarruunnaa

Showing Answers 1 - 36 of 36 Answers

star2k279

  • Dec 6th, 2007
 


"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.

  Was this answer useful?  Yes

star2k279

  • Dec 18th, 2007
 


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.

  Was this answer useful?  Yes

iramley

  • Jan 29th, 2008
 


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.

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.

  Was this answer useful?  Yes

PeterVH

  • Aug 19th, 2011
 

Classes and methods can be sealed:
- Sealed classes => can no longer be used to derive from
- Sealed methods => can no longer be overridden

Example with mankind, where the "CivilizedMan" decides it never wants to kill animals again, so the function is sealed. Finally, ModernMan will never evolve, so we make the class sealed to end evolution. :-)

Code
  1.     class CaveMan

  2.     {

  3.         protected virtual void HuntForFood()

  4.         {

  5.             Debug.WriteLine("CaveMan hunts down animals in the wild");

  6.         }

  7.         protected virtual void CookFood()

  8.         {

  9.             Debug.WriteLine("CaveMan doesn't cook his meat");

  10.         }

  11.     }

  12.     class MoreCivilized : CaveMan

  13.     {

  14.         // animal cruelty no longer allowed ;-)

  15.         sealed protected override void HuntForFood()

  16.         {

  17.             Debug.WriteLine("MoreCivilized just buys his meat. will never change again");

  18.         }

  19.         protected override void CookFood()

  20.         {

  21.             Debug.WriteLine("MoreCivilized roasts his meat above fire");

  22.         }

  23.     }

  24.     sealed class ModernMan : MoreCivilized

  25.     {

  26.         // Attempting to override HuntForFood causes compiler error CS0239.

  27.         // protected override void HuntForFood() { Debug.WriteLine("Wants to go hunting again"); }

  28.  

  29.         // Overriding CookFood is allowed.

  30.         protected override void CookFood()

  31.         {

  32.             Debug.WriteLine("ModernMan: BBQ time!! woohoo");

  33.         }

  34.     }

  35.     /*

  36.     // Will get the error: 'FutureMan' cannot inherit from sealed class 'ModernMan'.

  37.     class FutureMan : ModernMan

  38.     {}

  39.     */

  Was this answer useful?  Yes

sriramk

  • Aug 26th, 2011
 

why can't you use private?

  Was this answer useful?  Yes

Rajeeva

  • Sep 4th, 2011
 


To restrict users from inheriting the class by sealing the class using "sealed" keyword i.e. no class can be drieved from a sealed class. both Class and Method can be sealed, in that case Method cannot be overridden.

  Was this answer useful?  Yes

Sivavt

  • Mar 29th, 2012
 

Keyword sealed can be used with class declaration to prevent it from being sub-classed.
Keyword sealed can be used in method declaration to prevent it from being overridden.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions